codeparrot/xlcost-text-to-code huggingface dataset:
Minimize length of a given string by removing subsequences forming valid parenthesis | Function to remove all possible valid bracket subsequences ; Stores indexes of ' ( ' in valid subsequences ; Stores indexes of ' { ' in valid subsequences ; Stores indexes of ' [ ' in valid subsequences ; vis [ i ] : Check if character at i - th index is removed or not ; Mark vis [ i ] as not removed ; Iterate over the characters of string ; If current character is ' ( ' ; If current character is ' { ' ; If current character is ' [ ' ; If current character is ' ) ' and top element of A is ' ( ' ; Mark the top element of A as removed ; Mark current chacracter as removed ; If current character is ' } ' and top element of B is ' { ' ; Mark the top element of B as removed ; Mark current chacracter as removed ; If current character is ' ] ' and top element of B is ' [ ' ; Mark the top element of C as removed ; Mark current chacracter as removed ; Print the remaining characters which is not removed from S ; Given string ; Size of the string ; Function Call
function removeValidBracketSequences ( str , N ) { let A = [ ] ; let B = [ ] ; let C = [ ] ; let vis = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { vis [ i ] = true ; } for ( let i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ' ) { A . push ( i ) ; } else if ( str [ i ] == ' ' ) { B . push ( i ) ; } else if ( str [ i ] == ' ' ) { C . push ( i ) ; } else if ( str [ i ] == ' ' && ( A . length > 0 ) ) { vis [ A [ A . length - 1 ] ] = false ; A . pop ( ) ; vis [ i ] = false ; } else if ( str [ i ] == ' ' && ( B . length > 0 ) ) { vis [ B [ B . length - 1 ] ] = false ; B . pop ( ) ; vis [ i ] = false ; } else if ( str [ i ] == ' ' && ( C . length > 0 ) ) { vis [ C [ C . length - 1 ] ] = false ; C . pop ( ) ; vis [ i ] = false ; } } for ( let i = 0 ; i < N ; ++ i ) { if ( vis [ i ] ) document . write ( str [ i ] ) ; } } let str = " " ; let N = str . length ; removeValidBracketSequences ( str , N ) ;
Queries to calculate difference between the frequencies of the most and least occurring characters in specified substring | Function to update frequency of a character in Fenwick tree ; Update frequency of ( idx + ' a ' ) ; Update i ; Function to find the frequency of a character ( idx + ' a ' ) in range [ 1 , i ] ; Stores frequency of character , ( idx + ' a ' ) in range [ 1 , i ] ; Update ans ; Update i ; Function to find difference between maximum and minimum frequency of a character in given range ; BIT [ i ] [ j ] : Stores frequency of ( i + ' a ' ) If j is a power of 2 , then it stores the frequency ( i + ' a ' ) of from [ 1 , j ] ; Stores length of String ; Iterate over the characters of the String ; Update the frequency of s [ i ] in fenwick tree ; Stores count of queries ; Iterate over all the queries ; Stores maximum frequency of a character in range [ l , r ] ; Stores minimum frequency of a character in range [ l , r ] ; Iterate over all possible characters ; Stores frequency of ( j + ' a ' ) in range [ 1 , r ] ; Stores frequency of ( j + ' a ' ) in range [ 1 , l - 1 ] ; Update mx ; If a character ( i + ' a ' ) present in range [ l , r ] ; Update mn ; Print the difference between max and min freq ; Given String ; Given queries ; Function Call
function update ( BIT , idx , i , val ) { while ( i < 10005 ) { BIT [ idx ] [ i ] += val ; i = i + ( i & ( - i ) ) ; } } function query ( BIT , idx , i ) { let ans = 0 ; while ( i > 0 ) { ans += BIT [ idx ] [ i ] ; i = i - ( i & ( - i ) ) ; } return ans ; } function maxDiffFreq ( s , queries ) { let BIT = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { BIT [ i ] = new Array ( 10005 ) ; for ( let j = 0 ; j < 10005 ; j ++ ) { BIT [ i ] [ j ] = 0 ; } } let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { update ( BIT , s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) , i + 1 , 1 ) ; } let Q = queries . length ; for ( let i = 0 ; i < Q ; ++ i ) { let mx = 0 ; let mn = Number . MAX_VALUE ; let l = queries [ i ] [ 0 ] ; let r = queries [ i ] [ 1 ] ; for ( let j = 0 ; j < 26 ; j ++ ) { let p = query ( BIT , j , r ) ; let q = query ( BIT , j , l - 1 ) ; mx = Math . max ( mx , p - q ) ; if ( p > 0 ) { mn = Math . min ( mn , p - q ) ; } } document . write ( mx - mn + " " ) ; } } let S = " " ; let queries = [ [ 2 , 6 ] , [ 1 , 7 ] ] ; maxDiffFreq ( S , queries ) ;
Make all strings from a given array equal by replacing minimum number of characters | Function to find the minimum count of operations required to make all strings equal by replacing characters of strings ; Stores minimum count of operations required to make all strings equal ; Stores length of the string ; hash [ i ] [ j ] : Stores frequency of character i present at j - th index of all strings ; Traverse the array arr [ ] ; Iterate over characters of current string ; Update frequency of arr [ i ] [ j ] ; Traverse hash [ ] [ ] array ; Stores sum of i - th column ; Stores the largest element of i - th column ; Iterate over all possible characters ; Update Sum ; Update Max ; Update cntMinOP ; Driver Code ; Function call
function minOperation ( arr , N ) { var cntMinOP = 0 ; var M = arr [ 0 ] . length ; var hash = Array . from ( Array ( 256 ) , ( ) => Array ( M ) . fill ( 0 ) ) ; for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < M ; j ++ ) { hash [ arr [ i ] [ j ] . charCodeAt ( 0 ) ] [ j ] ++ ; } } for ( var i = 0 ; i < M ; i ++ ) { var Sum = 0 ; var Max = 0 ; for ( var j = 0 ; j < 256 ; j ++ ) { Sum += hash [ j ] [ i ] ; Max = Math . max ( Max , hash [ j ] [ i ] ) ; } cntMinOP += ( Sum - Max ) ; } return cntMinOP ; } var arr = [ " " , " " , " " ] ; var N = arr . length ; document . write ( minOperation ( arr , N ) + " " ) ;
Maximize count of distinct strings generated by replacing similar adjacent digits having sum K with K | Function to find the desired number of strings ; Store the count of strings ; Store the length of the string ; Initialize variable to indicate the start of the substring ; Store the starting index of the substring ; Traverse the string ; If sum of adjacent characters is K mark the starting index and set flag to 1 ; If sum of adjacent characters is not K and the flag variable is set , end the substring here ; Set flag to 0 denoting the end of substring ; Check if the length of the substring formed is odd ; Update the answer ; If flag is set and end of string is reached , mark the end of substring ; Update the answer ; Print the answer ; Driver Code ; Function Call
function countStrings ( s , k ) { var ans = 1 ; var len = s . length ; var flag = 0 ; var start_ind ; for ( var i = 0 ; i < len - 1 ; i ++ ) { if ( ( s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + s [ i + 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) == k && flag == 0 ) { flag = 1 ; start_ind = i ; } if ( flag == 1 && ( s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + s [ i + 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) != k ) { flag = 0 ; if ( ( i - start_ind + 1 ) % 2 != 0 ) ans *= ( i - start_ind + 1 - 1 ) / 2 + 1 ; } } if ( flag == 1 && ( len - start_ind ) % 2 != 0 ) ans *= parseInt ( ( len - start_ind ) / 2 ) + 1 ; document . write ( ans ) ; } var S = " " ; var K = 4 ; countStrings ( S , K ) ;
Smallest number whose product with N has sum of digits equal to that of N | Function to find the minimum integer having sum of digits of a number multiplied by n equal to sum of digits of n ; Initialize answer ; Find sum of digits of N ; Multiply N with x ; Sum of digits of the new number ; If condition satisfies ; Print answer ; Driver Code ; Function call
function find_num ( n ) { var ans = 0 ; var sumOfDigitsN = 0 ; for ( var c = 0 ; c < n . length ; c ++ ) { sumOfDigitsN += n - ' ' ; } var x = 11 ; while ( true ) { var newNum = x * parseInt ( n ) ; var tempSumDigits = 0 ; var temp = ( newNum . toString ( ) ) ; for ( var c = 0 ; c < temp . length ; c ++ ) { tempSumDigits += temp - ' ' ; } if ( tempSumDigits == sumOfDigitsN ) { ans = x ; break ; } x ++ ; } document . write ( ans ) ; } var N = " " ; find_num ( N ) ;
Print all distinct strings from a given array | Function to find the distinct strings from the given array ; Stores distinct strings from the given array ; Traverse the array ; If current string not present into the set ; Insert current string into the set ; Traverse the set DistString ; Print distinct string ; Driver Code ; Stores length of the array
function findDisStr ( arr , N ) { let DistString = new Set ( ) ; for ( let i = N - 1 ; i >= 0 ; i -- ) { if ( ! DistString . has ( arr [ i ] ) ) { DistString . add ( arr [ i ] ) ; } } for ( let String of DistString ) { document . write ( String + " " ) ; } } let arr = [ " " , " " , " " , " " , " " ] ; let N = arr . length ; findDisStr ( arr , N ) ;
Reverse substrings of given string according to specified array indices | Function to perform the reversal operation on the given String ; Size of String ; Stores the count of indices ; Count the positions where reversals will begin ; Store the count of reversals beginning at position i ; Check if the count [ i ] is odd the swap the character ; Return the updated String ; Given String str ; Given array of reversing index ; Function Call
function modifyString ( A , str , K ) { str = str . split ( " " ) let N = str . length ; let count = new Array ( N + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < K ; i ++ ) { count [ A [ i ] ] += 1 ; } for ( let i = 1 ; i <= N / 2 ; i ++ ) { count [ i ] = count [ i ] + count [ i - 1 ] ; if ( count [ i ] & 1 ) { let temp = str [ i - 1 ] ; str [ i - 1 ] = str [ N - i ] ; str [ N - i ] = temp } } return str . join ( " " ) ; } let str = " " ; let arr = [ 1 , 2 , 3 ] ; let K = arr . length ; document . write ( modifyString ( arr , str , K ) ) ;
Convert given Float value to equivalent Fraction | Recursive function to return gcd of a and b ; Function to convert the floating values into fraction ; Initialize variables ; Traverse the floating String ; Check if decimal part exist ; Check if recurrence sequence exist ; Retrieve decimal part and recurrence resquence ; Traverse the String ; Convert String to integer ; If no recurrence sequence exist ; Initialize numerator & denominator ; No reccuring term ; Print the result ; If reccuring term exist ; Convert reccuring term to integer ; reccu . size ( ) is num of digit in reccur term ; eq 2 - eq 1 ; Print the result ; Given String str ; Function Call
function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } function findFraction ( s ) { let be_deci = " " , af_deci = " " , reccu = " " ; let x = true , y = false , z = false ; for ( let i = 0 ; i < s . length ; ++ i ) { if ( s [ i ] == ' ' ) { x = false ; y = true ; continue ; } if ( s [ i ] == ' ' ) { z = true ; y = false ; continue ; } if ( x ) be_deci += s [ i ] ; if ( y ) af_deci += s [ i ] ; if ( z ) { for ( ; i < s . length && s [ i ] != ' ' ; ++ i ) reccu += s [ i ] ; break ; } } let num_be_deci = parseInt ( be_deci ) ; let num_af_deci = 0 ; if ( af_deci . length != 0 ) num_af_deci = parseInt ( af_deci ) ; let numr = num_be_deci * Math . pow ( 10 , af_deci . length ) + num_af_deci ; let deno = Math . pow ( 10 , af_deci . length ) ; if ( reccu . length == 0 ) { let gd = __gcd ( numr , deno ) ; document . write ( numr / gd + " " + deno / gd ) ; } else { let reccu_num = parseInt ( reccu ) ; let numr1 = numr * Math . pow ( 10 , reccu . length ) + reccu_num ; let deno1 = deno * Math . pow ( 10 , reccu . length ) ; let res_numr = numr1 - numr , res_deno = deno1 - deno ; let gd = __gcd ( res_numr , res_deno ) ; document . write ( res_numr / gd + " " + res_deno / gd ) ; } } let str = " " ; findFraction ( str ) ;
Lexicographically largest string possible for a given cost of appending characters | Function to find the lexicographically largest String possible ; If sum is less than 0 ; If sum is equal to 0 ; If sum is less than 0 ; Add current character ; Check if selecting current character generates lexicographically largest String ; Backtrack if solution not found ; Find the lexicographically largest String excluding the current character ; Function to print the lexicographically largest String generated ; Function call ; Stores the String ; Print the lexicographically largest String formed ; Cost of adding each alphabet ; Cost of generating the String
function lexi_largest_String ( a , i , sum , v ) { if ( sum < 0 ) return false ; if ( sum == 0 ) return true ; if ( i < 0 ) return false ; v . push ( i ) ; if ( lexi_largest_String ( a , i , sum - a [ i ] , v ) ) return true ; v . pop ( ) ; return lexi_largest_String ( a , i - 1 , sum , v ) ; } function generateString ( a , sum ) { let v = [ ] ; lexi_largest_String ( a , 25 , sum , v ) ; let s = " " ; for ( let j = 0 ; j < v . length ; j ++ ) s += String . fromCharCode ( v [ j ] + ' ' . charCodeAt ( ) ) ; document . write ( s + " " ) ; } let a = [ 1 , 1 , 2 , 33 , 4 , 6 , 9 , 7 , 36 , 32 , 58 , 32 , 28 , 904 , 22 , 255 , 47 , 69 , 558 , 544 , 21 , 36 , 48 , 85 , 48 , 58 ] ; let sum = 236 ; generateString ( a , sum ) ;
Count of distinct permutations of every possible length of given string | Function to find the factorial of a number ; Loop to find the factorial of the given number ; Function to find the number of permutations possible for a given string ; Function to find the total number of combinations possible ; Driver Code
function fact ( a ) { var i , f = 1 ; for ( i = 2 ; i <= a ; i ++ ) f = f * i ; return f ; } function permute ( n , r ) { var ans = 0 ; ans = ( fact ( n ) / fact ( n - r ) ) ; return ans ; } function findPermutations ( n ) { var sum = 0 , P ; for ( var r = 1 ; r <= n ; r ++ ) { P = permute ( n , r ) ; sum = sum + P ; } return sum ; } var str = " " ; var result , n ; n = str . length ; document . write ( findPermutations ( n ) ) ;
String hashing using Polynomial rolling hash function | Function to calculate the hash of a String ; P and M ; Loop to calculate the hash value by iterating over the elements of String ; Given Strings
function polynomialRollingHash ( str ) { let p = 31 ; let m = ( 1e9 + 9 ) ; let power_of_p = 1 ; let hash_val = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { hash_val = ( hash_val + ( str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) + 1 ) * power_of_p ) % m ; power_of_p = ( power_of_p * p ) % m ; } return hash_val ; } let str1 = " " ; let str2 = " " ; document . write ( " " + str1 + " " + polynomialRollingHash ( str1 ) ) ;
Find first non | Function to find the first non - repeating element of the given string using Linked List ; Driver Code ; Function Call
function firstNonRepElement ( str ) { let list = [ ] ; list . push ( str [ 0 ] ) ; for ( let i = 1 ; i < str . length ; i ++ ) { if ( list . includes ( str [ i ] ) ) list . splice ( list . indexOf ( str [ i ] ) , 1 ) ; else list . push ( str [ i ] ) ; } document . write ( list [ 0 ] ) ; } let str = " " ; firstNonRepElement ( str ) ;
DFA that begins with ' a ' but does not contain substring ' aab ' | Function for state A transition ; If at index 0 ' a ' if found then call stateB function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateQ function with passing n to it ; Function for transition state B ; length ( ) of String become 0 then print Accepted ; If at index 0 ' a ' if found then call stateC function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateD function with passing n [ 1 : ] to it ; Function for transition state C ; length ( ) of String become 0 then print Accepted ; If at index 0 ' a ' if found then call stateC function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateQ function with passing n to it ; Function for transition state D ; length ( ) of String become 0 then print Accepted ; If at index 0 ' a ' if found then call stateC function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateQ function with passing n to it ; Function for state Q transition ; In dead state it shows String not accepted ; Take String input ; Call stateA to check the input
function stateA ( n ) { if ( n [ 0 ] == ' ' ) { stateB ( n . substr ( 1 ) ) ; } else { stateQ ( n ) ; } } function stateB ( n ) { if ( n . length == 0 ) { document . write ( " " ) ; } else { if ( n [ 0 ] == ' ' ) stateC ( n . substr ( 1 ) ) ; else stateD ( n . substr ( 1 ) ) ; } } function stateC ( n ) { if ( n . length == 0 ) document . write ( " " ) ; else { if ( n [ 0 ] == ' ' ) stateC ( n . substr ( 1 ) ) ; else stateQ ( n ) ; } } function stateD ( n ) { if ( n . length ( ) == 0 ) document . write ( " " ) ; else { if ( n [ 0 ] == ' ' ) { stateB ( n . substr ( 1 ) ) ; } else { stateD ( n . substr ( 1 ) ) ; } } } function stateQ ( n ) { document . write ( " " ) ; } let n = " " ; stateA ( n ) ;
Count of substrings of length K with exactly K distinct characters | Function to return the required count of substrings ; Store the count ; Store the count of distinct characters in every window ; Store the frequency of the first K length substring ; Increase frequency of i - th character ; If K distinct characters exist ; Traverse the rest of the substring ; Increase the frequency of the last character of the current substring ; Decrease the frequency of the first character of the previous substring ; If the character is not present in the current substring ; If the count of distinct characters is 0 ; Return the count ; string str ; integer K ; Print the count of K length substrings with k distinct characters
function countSubstrings ( str , K ) { var N = str . length ; var answer = 0 ; var map = new Map ( ) ; for ( var i = 0 ; i < K ; i ++ ) { if ( map . has ( str [ i ] ) ) map . set ( str [ i ] , map . get ( str [ i ] ) + 1 ) else map . set ( str [ i ] , 1 ) } if ( map . size == K ) answer ++ ; for ( var i = K ; i < N ; i ++ ) { if ( map . has ( str [ i ] ) ) map . set ( str [ i ] , map . get ( str [ i ] ) + 1 ) else map . set ( str [ i ] , 1 ) if ( map . has ( str [ i - K ] ) ) map . set ( str [ i - K ] , map . get ( str [ i - K ] ) - 1 ) if ( map . has ( str [ i - K ] ) && map . get ( str [ i - K ] ) == 0 ) { map . delete ( str [ i - K ] ) ; } if ( map . size == K ) { answer ++ ; } } return answer ; } var str = " " ; var K = 3 ; document . write ( countSubstrings ( str , K ) ) ;
Check if a string is a scrambled form of another string | map declaration for storing key value pair means for storing subproblem result ; Strings of non - equal length cant ' be scramble strings ; Empty strings are scramble strings ; Equal strings are scramble strings ; Check for the condition of anagram ; make key of type string for search in map ; checking if both string are before calculated or not if calculated means find in map then return it 's  value ; declaring flag variable to store result ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ 0. . . i ] and if S2 [ i + 1. . . n ] is a scrambled string of S1 [ i + 1. . . n ] ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ n - i ... n ] and S2 [ i + 1. . . n ] is a scramble string of S1 [ 0. . . n - i - 1 ] ; add key & flag value to map ( store for future use ) so next time no required to calculate it again ; If none of the above conditions are satisfied ; Driver Code
let mp = new Map ( ) ; function isScramble ( S1 , S2 ) { if ( S1 . length != S2 . length ) { return false ; } let n = S1 . length ; if ( n == 0 ) { return true ; } if ( S1 == S2 ) { return true ; } let copy_S1 = S1 , copy_S2 = S2 ; let t1 = copy_S1 . split ( ' ' ) let t2 = copy_S2 . split ( ' ' ) t1 . sort ( ) ; t2 . sort ( ) ; copy_S1 = t1 . join ( " " ) ; copy_S2 = t2 . join ( " " ) ; if ( copy_S1 != copy_S2 ) { return false ; } let key = ( S1 + " " + S2 ) ; if ( mp . has ( key ) ) { return mp [ key ] ; } let flag = false ; for ( let i = 1 ; i < n ; i ++ ) { if ( isScramble ( S1 . substring ( 0 , i ) , S2 . substring ( 0 , i ) ) && isScramble ( S1 . substring ( i , n ) , S2 . substring ( i , n ) ) ) { flag = true ; return true ; } if ( isScramble ( S1 . substring ( 0 , i ) , S2 . substring ( n - i , n ) ) && isScramble ( S1 . substring ( i , n ) , S2 . substring ( 0 , n - i ) ) ) { flag = true ; return true ; } } mp [ key ] = flag ; return false ; } let S1 = " " ; let S2 = " " ; if ( isScramble ( S1 , S2 ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Print the longest palindromic prefix of a given string | Function to find the longest prefix which is palindromic ; Find the length of the given String ; For storing the length of longest prefix palindrome ; Loop to check the subString of all length from 1 to N which is palindrome ; String of length i ; To store the reversed of temp ; Reversing String temp2 ; If String temp is palindromic then update the length ; Print the palindromic String of max_len ; Given String ; Function Call
function LongestPalindromicPrefix ( s ) { let n = s . length ; let max_len = 0 ; for ( let len = 1 ; len <= n ; len ++ ) { let temp = s . substring ( 0 , len ) ; let temp2 = temp ; temp2 = reverse ( temp2 ) ; if ( temp == temp2 ) { max_len = len ; } } document . write ( s . substring ( 0 , max_len ) ) ; } function reverse ( input ) { let a = input . split ( ' ' ) ; let l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { let temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return a . join ( " " ) ; } let str = " " ; LongestPalindromicPrefix ( str ) ;
K | Javascript program to find the K - th lexicographical String of length N ; Initialize the array to store the base 26 representation of K with all zeroes , that is , the initial String consists of N a 's ; Start filling all the N slots for the base 26 representation of K ; Store the remainder ; Reduce K ; If K is greater than the possible number of Strings that can be represented by a String of length N ; Store the Kth lexicographical String ; Driver Code ; Reducing k value by 1 because our stored value starts from 0
function find_kth_String_of_n ( n , k ) { let d = new Array ( n ) ; d . fill ( 0 ) ; for ( let i = n - 1 ; i > - 1 ; i -- ) { d [ i ] = k % 26 ; k = parseInt ( k / 26 , 10 ) ; } if ( k > 0 ) return " " ; let s = " " ; for ( let i = 0 ; i < n ; i ++ ) s += String . fromCharCode ( d [ i ] + ( ' ' ) . charCodeAt ( ) ) ; return s ; } let n = 3 ; let k = 10 ; k -= 1 ; document . write ( find_kth_String_of_n ( n , k ) ) ;
Length of the smallest substring which contains all vowels | JavaScript Program to find the length of the smallest substring of which contains all vowels ; Map to store the frequency of vowels ; Store the indices which contains the vowels ; If all vowels are not present in the string ; If the frequency of the vowel at i - th index exceeds 1 ; Decrease the frequency of that vowel ; Move to the left ; Otherwise set flag1 ; If the frequency of the vowel at j - th index exceeds 1 ; Decrease the frequency of that vowel ; Move to the right ; Otherwise set flag2 ; If both flag1 and flag2 are set , break out of the loop as the substring length cannot be minimized ; Return the length of the substring ; Driver Code
function findMinLength ( s ) { var n = s . length ; var counts = new Map ( ) ; var indices = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' ) { if ( counts . has ( s [ i ] ) ) counts . set ( s [ i ] , counts . get ( s [ i ] ) + 1 ) else counts . set ( s [ i ] , 1 ) indices . push ( i ) ; } } if ( counts . size < 5 ) return - 1 ; var flag1 = 0 , flag2 = 0 ; var i = 0 ; var j = indices . length - 1 ; while ( ( j - i ) >= 4 ) { if ( ! flag1 && counts . get ( s [ indices [ i ] ] ) > 1 ) { if ( counts . has ( s [ indices [ i ] ] ) ) counts . set ( s [ indices [ i ] ] , counts . get ( s [ indices [ i ] ] ) - 1 ) i ++ ; } else flag1 = 1 ; if ( ! flag2 && counts . get ( s [ indices [ j ] ] ) > 1 ) { if ( counts . has ( s [ indices [ j ] ] ) ) counts . set ( s [ indices [ j ] ] , counts . get ( s [ indices [ j ] ] ) - 1 ) j -- ; } else flag2 = 1 ; if ( flag1 && flag2 ) break ; } return ( indices [ j ] - indices [ i ] + 1 ) ; } var s = " " ; document . write ( findMinLength ( s ) ) ;