Find length of longest substring with at most K normal characters | Function to find maximum length of normal substrings ; keeps count of normal characters ; indexes of substring ; maintain length of longest substring with at most K normal characters ; get position of character ; check if current character is normal ; check if normal characters count exceeds K ; update answer with substring length ; get position of character ; check if character is normal then decrement count ; initialise the String
function maxNormalSubstring ( P , Q , K , N ) { if ( K == 0 ) return 0 ; var count = 0 ; var left = 0 , right = 0 ; var ans = 0 ; while ( right < N ) { while ( right < N && count <= K ) { var pos = P [ right ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( Q [ pos ] == ' ' ) { if ( count + 1 > K ) break ; else count ++ ; } right ++ ; if ( count <= K ) ans = Math . max ( ans , right - left ) ; } while ( left < right ) { var pos = P [ left ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; left ++ ; if ( Q [ pos ] == ' ' ) count -- ; if ( count < K ) break ; } } return ans ; } var P = " " , Q = " " ; var K = 2 ; var N = P . length ; document . write ( maxNormalSubstring ( P , Q , K , N ) ) ;