Find sub | 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
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 ) - ' ' . charCodeAt ( 0 ) + 1 ) ; if ( curr_power == power ) { document . write ( " " + " " + i + " " + power + " " ) ; return ; } if ( map . has ( curr_power - power ) ) { document . write ( " " + ( map . get ( curr_power - power ) + 1 ) + " " + i + " " + power + " " ) ; return ; } map . set ( curr_power , i ) ; } document . write ( " " ) ; } let str = " " ; let power = 36 ; findSubstring ( str , power ) ;