Related Articles
Find sub-string with given power
• Difficulty Level : Medium
• Last Updated : 02 Jan, 2020

Given a string of lowercase alphabets, the task is to find indexes of a substring with given power. Assume power of a to be 1, b to be 2, c to be 3 and so on. Here the power of a substring means the sum of the powers of all the characters in that particular substring.

Examples:

Input: str = “geeksforgeeks” power = 36
Output: Substring from index 3 to 5 has power 36
Explanation: k = 11, s = 19, f = 6 i.e. k + s + e = 36.

Input: str = “aditya” power = 2
Output: No substring with given power exists.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Simple Approach:
1. Calculate powers of all substrings using nested for loops.
2. If the power of any substring equals the given power then print the indexes of the substring.
3. If no such substring exists then print “No substring with given power exists”.

Time Complexity: O (n ^ 2).

Efficient Approach: Use map to store the powers.

1. For each element check if curr_power – power exists in the map or not.
2. If it exists in the map it means that we have a substring present with given power, else we insert curr_power into the map and proceed to the next character.
3. If all characters of the string are processed and we didn’t find any substring with given power, then substring doesn’t exist.

## C++

 `// C++ program to find substring with given power``#include ``#define ll long long int``using` `namespace` `std;`` ` `// Function to print indexes of substring with``// power as given power.``void` `findSubstring(string str, ll power)``{``    ``ll i;``    ``// Create an empty map``    ``unordered_map map;`` ` `    ``// Maintains sum of powers of characters so far.``    ``int` `curr_power = 0;``    ``int` `len = str.length();`` ` `    ``for` `(i = 0; i < len; i++) {``        ``// Add current character power to curr_power.``        ``curr_power = curr_power + (str[i] - ``'a'` `+ 1);`` ` `        ``// If curr_power is equal to target power``        ``// we found a substring starting from index 0``        ``// and ending at index i.``        ``if` `(curr_power == power) {``            ``cout << ``"Substring from index "` `<< 0 << ``" to "``                 ``<< i << ``" has power "` `<< power << endl;``            ``return``;``        ``}`` ` `        ``// If curr_power - power already exists in map``        ``// then we have found a subarray with target power.``        ``if` `(map.find(curr_power - power) != map.end()) {``            ``cout << ``"Substring from index "``                 ``<< map[curr_power - power] + 1``                 ``<< ``" to "` `<< i <<``" has power "` `<

## Java

 `// Java program to find substring with given power``import` `java.util.*;`` ` `class` `GFG ``{`` ` `    ``// Function to print indexes of substring with``    ``// power as given power.``    ``static` `void` `findSubstring(String str, ``int` `power)``    ``{`` ` `        ``// Create an empty map``        ``HashMap map = ``new` `HashMap<>();`` ` `        ``// Maintains sum of powers of characters so far.``        ``int` `curr_power = ``0``;``        ``int` `len = str.length();`` ` `        ``for` `(``int` `i = ``0``; i < len; i++) ``        ``{`` ` `            ``// Add current character power to curr_power.``            ``curr_power = curr_power + (str.charAt(i) - ``'a'` `+ ``1``);`` ` `            ``// If curr_power is equal to target power``            ``// we found a substring starting from index 0``            ``// and ending at index i.``            ``if` `(curr_power == power)``            ``{``                ``System.out.println(``"Substring from index 0"` `+``                                   ``" to "` `+ i + ``" has power "` `+ power);``                ``return``;``            ``}`` ` `            ``// If curr_power - power already exists in map``            ``// then we have found a subarray with target power.``            ``if` `(map.containsKey(curr_power - power))``            ``{``                ``System.out.println(``"Substring from index "` `+ ``                                  ``(map.get(curr_power - power) + ``1``) +``                                 ``" to "` `+ i + ``" has power "` `+ power);``                ``return``;``            ``}`` ` `            ``map.put(curr_power, i);``        ``}`` ` `        ``// If we reach here, then no substring exists.``        ``System.out.println(``"No substring with given power exists."``);``    ``}`` ` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args) ``    ``{``        ``String str = ``"geeksforgeeks"``;``        ``int` `power = ``36``;`` ` `        ``findSubstring(str, power);``    ``}``}`` ` `// This code is contributed by``// sanjeev2552`

## C#

 `// C# program to find substring``// with given power``using` `System;``using` `System.Collections.Generic;`` ` `class` `GFG ``{`` ` `    ``// Function to print indexes of substring ``    ``// with power as given power.``    ``static` `void` `findSubstring(String str,``                              ``int` `power)``    ``{`` ` `        ``// Create an empty map``        ``Dictionary<``int``, ``                   ``int``> map = ``new` `Dictionary<``int``,``                                             ``int``>();`` ` `        ``// Maintains sum of powers ``        ``// of characters so far.``        ``int` `curr_power = 0;``        ``int` `len = str.Length;`` ` `        ``for` `(``int` `i = 0; i < len; i++) ``        ``{`` ` `            ``// Add current character power``            ``// to curr_power.``            ``curr_power = curr_power + ``                        ``(str[i] - ``'a'` `+ 1);`` ` `            ``// If curr_power is equal to target power``            ``// we found a substring starting from index 0``            ``// and ending at index i.``            ``if` `(curr_power == power)``            ``{``                ``Console.WriteLine(``"Substring from index 0"` `+``                                  ``" to "` `+ i + ``                                  ``" has power "` `+ power);``                ``return``;``            ``}`` ` `            ``// If curr_power - power already exists in map``            ``// then we have found a subarray with target power.``            ``if` `(map.ContainsKey(curr_power - power))``            ``{``                ``Console.WriteLine(``"Substring from index "` `+ ``                            ``(map[curr_power - power] + 1) +``                       ``" to "` `+ i + ``" has power "` `+ power);``                ``return``;``            ``}``             ` `            ``if` `(!map.ContainsKey(curr_power))``                    ``map.Add(curr_power, i);``            ``else``                ``map[curr_power] = i;``        ``}`` ` `        ``// If we reach here, then no substring exists.``        ``Console.WriteLine(``"No substring with "` `+ ``                          ``"given power exists"``);``    ``}`` ` `    ``// Driver Code``    ``public` `static` `void` `Main(String[] args) ``    ``{``        ``String str = ``"geeksforgeeks"``;``        ``int` `power = 36;`` ` `        ``findSubstring(str, power);``    ``}``}`` ` `// This code is contributed by 29AjayKumar`
Output:
```Substring from index 3 to 5 has power 36
```

Time Complexity: O (n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up