Find sub-string with given power
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.
Simple Approach:
- Calculate powers of all substrings using nested for loops.
- If the power of any substring equals the given power then print the indexes of the substring.
- 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.
- For each element check if curr_power – power exists in the map or not.
- 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.
- If all characters of the string are processed and we didn’t find any substring with given power, then substring doesn’t exist.
Implementation:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
void findSubstring(string str, ll power)
{
ll i;
unordered_map<ll, ll> map;
int curr_power = 0;
int len = str.length();
for (i = 0; i < len; i++) {
curr_power = curr_power + (str[i] - 'a' + 1);
if (curr_power == power) {
cout << "Substring from index " << 0 << " to "
<< i << " has power " << power << endl;
return ;
}
if (map.find(curr_power - power) != map.end()) {
cout << "Substring from index "
<< map[curr_power - power] + 1
<< " to " << i << " has power " <<power << endl;
return ;
}
map[curr_power] = i;
}
cout << "No substring with given power exists." ;
}
int main()
{
string str = "geeksforgeeks" ;
ll power = 36;
findSubstring(str, power);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findSubstring(String str, int power)
{
HashMap<Integer,
Integer> map = new HashMap<>();
int curr_power = 0 ;
int len = str.length();
for ( int i = 0 ; i < len; i++)
{
curr_power = curr_power + (str.charAt(i) - 'a' + 1 );
if (curr_power == power)
{
System.out.println( "Substring from index 0" +
" to " + i + " has power " + power);
return ;
}
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);
}
System.out.println( "No substring with given power exists." );
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
int power = 36 ;
findSubstring(str, power);
}
}
|
Python3
def findSubstring( Str ,power):
map = {}
curr_power = 0
Len = len ( Str )
for i in range ( Len ):
curr_power = curr_power + ( ord ( Str [i]) - ord ( 'a' ) + 1 )
if (curr_power = = power):
print ( "Substring from index 0 to " + str (i) + " has power " + str (power))
return
if (curr_power - power in map ):
print ( "Substring from index " + str ( map [curr_power - power] + 1 ) + " to " + str (i) + " has power " + str (power))
return
map [curr_power] = i
print ( "No substring with given power exists." )
Str = "geeksforgeeks"
power = 36
findSubstring( Str , power)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void findSubstring(String str,
int power)
{
Dictionary< int ,
int > map = new Dictionary< int ,
int >();
int curr_power = 0;
int len = str.Length;
for ( int i = 0; i < len; i++)
{
curr_power = curr_power +
(str[i] - 'a' + 1);
if (curr_power == power)
{
Console.WriteLine( "Substring from index 0" +
" to " + i +
" has power " + power);
return ;
}
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;
}
Console.WriteLine( "No substring with " +
"given power exists" );
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
int power = 36;
findSubstring(str, power);
}
}
|
Javascript
<script>
function findSubstring(str,power)
{
let map = new Map();
let curr_power = 0;
let len = str.length;
for (let i = 0; i < len; i++)
{
curr_power = curr_power + (str[i].charCodeAt(0) - 'a' .charCodeAt(0) + 1);
if (curr_power == power)
{
document.write( "Substring from index 0" +
" to " + i + " has power " + power+ "<br>" );
return ;
}
if (map.has(curr_power - power))
{
document.write( "Substring from index " +
(map.get(curr_power - power) + 1) +
" to " + i + " has power " + power+ "<br>" );
return ;
}
map.set(curr_power, i);
}
document.write( "No substring with given power exists.<br>" );
}
let str = "geeksforgeeks" ;
let power = 36;
findSubstring(str, power);
</script>
|
Output
Substring from index 3 to 5 has power 36
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n)
Last Updated :
25 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...