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.
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.
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.
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.
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Given two numbers as strings, find if one is a power of other
- Find max length odd parity substring
- Find the Substring with maximum product
- Find the String having each substring with exactly K distinct characters
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Find length of longest substring with at most K normal characters
- Find the longest substring with k unique characters in a given string
- Minimum length of substring whose rotation generates a palindromic substring
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Find length of longest subsequence of one string which is substring of another string
- Power Set in Lexicographic order
- Substring Sort
- DFA that begins with 'a' but does not contain substring 'aab'
- Longest Palindromic Substring | Set 2
- Longest Palindromic Substring | Set 1
- Longest substring with count of 1s more than 0s
- Largest substring with same Characters
- Palindrome Substring Queries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.