Find sub | C # program to find substring with given power ; Function to print indexes of substring with power as given power . ; Create an empty map ; Maintains sum of powers of characters so far . ; Add current character power to curr_power . ; 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 already exists in map then we have found a subarray with target power . ; If we reach here , then no substring exists . ; Driver Code
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 ) ; } }