codeparrot/xlcost-text-to-code huggingface dataset:
Maximum possible time that can be formed from four digits | Function to return the updated frequency map for the array passed as argument ; Function that returns true if the passed digit is present in the map after decrementing it 's frequency by 1 ; If map contains the digit ; Decrement the frequency of the digit by 1 ; True here indicates that the digit was found in the map ; Digit not found ; Function to return the maximum possible time in 24 - Hours format ; First digit of hours can be from the range [ 0 , 2 ] ; If no valid digit found ; If first digit of hours was chosen as 2 then the second digit of hours can be from the range [ 0 , 3 ] ; Else it can be from the range [ 0 , 9 ] ; Hours and minutes separator ; First digit of minutes can be from the range [ 0 , 5 ] ; Second digit of minutes can be from the range [ 0 , 9 ] ; Return the maximum possible time ; Driver code
function getFrequencyMap ( arr ) { var hashMap = { } ; for ( var i = 0 ; i < arr . length ; i ++ ) { if ( hashMap . hasOwnProperty ( arr [ i ] ) ) { hashMap [ arr [ i ] ] = hashMap [ arr [ i ] ] + 1 ; } else { hashMap [ arr [ i ] ] = 1 ; } } return hashMap ; } function hasDigit ( hashMap , digit ) { if ( hashMap . hasOwnProperty ( digit ) && hashMap [ digit ] > 0 ) { hashMap [ digit ] = hashMap [ digit ] - 1 ; return true ; } return false ; } function getMaxTime ( arr ) { var hashMap = getFrequencyMap ( arr ) ; var i ; var flag ; var time = " " ; flag = false ; for ( i = 2 ; i >= 0 ; i -- ) { if ( hasDigit ( hashMap , i ) ) { flag = true ; time += i ; break ; } } if ( ! flag ) { return " " ; } flag = false ; if ( time [ 0 ] === " " ) { for ( i = 3 ; i >= 0 ; i -- ) { if ( hasDigit ( hashMap , i ) ) { flag = true ; time += i ; break ; } } } else { for ( i = 9 ; i >= 0 ; i -- ) { if ( hasDigit ( hashMap , i ) ) { flag = true ; time += i ; break ; } } } if ( ! flag ) { return " " ; } time += " " ; flag = false ; for ( i = 5 ; i >= 0 ; i -- ) { if ( hasDigit ( hashMap , i ) ) { flag = true ; time += i ; break ; } } if ( ! flag ) { return " " ; } flag = false ; for ( i = 9 ; i >= 0 ; i -- ) { if ( hasDigit ( hashMap , i ) ) { flag = true ; time += i ; break ; } } if ( ! flag ) { return " " ; } return time ; } var arr = [ 0 , 0 , 0 , 9 ] ; document . write ( getMaxTime ( arr ) ) ;
Most frequent word in first String which is not present in second String | Function to return frequent word from S1 that isn 't  present in S2 ; create map of banned words ; find smallest and most frequent word ; check if word is not banned ; return answer ; Driver Code
function smallestFreq ( S1 , S2 ) { let banned = new Map ( ) ; for ( let i = 0 ; i < S2 . length ; i ++ ) { let s = " " ; while ( i < S2 . length && S2 [ i ] != ' ' ) s += S2 [ i ++ ] ; banned . set ( s , banned [ s ] == null ? 1 : banned . get ( s ) + 1 ) ; } let result = new Map ( ) ; let ans = " " ; let freq = 0 ; for ( let i = 0 ; i < S1 . length ; i ++ ) { let s = " " ; while ( i < S1 . length && S1 [ i ] != ' ' ) s += S1 [ i ++ ] ; if ( banned . get ( s ) == null ) { result . set ( s , result . get ( s ) == null ? 1 : result . get ( s ) + 1 ) ; if ( result . get ( s ) > freq || ( result . get ( s ) == freq && s < ( ans ) ) ) { ans = s ; freq = result . get ( s ) ; } } } return ans ; } let S1 = " " ; let S2 = " " ; document . write ( smallestFreq ( S1 , S2 ) ) ;
Check if a string is substring of another | Returns true if s1 is substring of s2 ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; Driver code
function isSubstring ( s1 , s2 ) { var M = s1 . length ; var N = s2 . length ; for ( var i = 0 ; i <= N - M ; i ++ ) { var j ; for ( j = 0 ; j < M ; j ++ ) if ( s2 [ i + j ] != s1 [ j ] ) break ; if ( j == M ) return i ; } return - 1 ; } var s1 = " " ; var s2 = " " ; var res = isSubstring ( s1 , s2 ) ; if ( res == - 1 ) document . write ( " " ) ; else document . write ( " " + res ) ;
Find minimum shift for longest common prefix | function for KMP search ; preprocessing of longest proper prefix ; find out the longest prefix and position ; for new position with longer prefix in str2 update pos and len ; print result ; driver function
function KMP ( m , n , str2 , str1 ) { var pos = 0 , len = 0 ; var p = Array ( m + 1 ) . fill ( 0 ) ; var k = 0 ; p [ 1 ] = 0 ; for ( var i = 2 ; i <= n ; i ++ ) { while ( k > 0 && str1 [ k ] != str1 [ i - 1 ] ) k = p [ k ] ; if ( str1 [ k ] == str1 [ i - 1 ] ) ++ k ; p [ i ] = k ; } for ( var j = 0 , i = 0 ; i < m ; i ++ ) { while ( j > 0 && str1 [ j ] != str2 [ i ] ) j = p [ j ] ; if ( str1 [ j ] == str2 [ i ] ) j ++ ; if ( j > len ) { len = j ; pos = i - j + 1 ; } } document . write ( " " + pos + " " ) ; document . write ( " " + str1 . substr ( 0 , len ) ) ; } var str1 = " " ; var str2 = " " ; var n = str1 . length ; str2 = str2 + str2 ; KMP ( 2 * n , n , str2 , str1 ) ;
Find all the patterns of "1(0 + ) 1" in a given string | SET 1 ( General Approach ) | Function to count patterns ; Variable to store the last character ; We found 0 and last character was '1' , state change ; After the stream of 0 ' s , ▁ we ▁ ▁ got ▁ a ▁ ' 1 ',counter incremented ; Last character stored ; Driver Code
function patternCount ( str ) { var last = str . charAt ( 0 ) ; var i = 1 , counter = 0 ; while ( i < str . length ) { if ( str . charAt ( i ) == ' ' && last == ' ' ) { while ( str . charAt ( i ) == ' ' ) i ++ ; if ( str . charAt ( i ) == ' ' ) counter ++ ; } last = str . charAt ( i ) ; i ++ ; } return counter ; } var str = " " ; document . write ( patternCount ( str ) ) ;
Maximum length prefix of one string that occurs as subsequence in another | Return the maximum length prefix which is subsequence . ; Iterating string T . ; If end of string S . ; If character match , increment counter . ; Driver Code
function maxPrefix ( s , t ) { let count = 0 ; for ( let i = 0 ; i < t . length ; i ++ ) { if ( count == s . length ) break ; if ( t [ i ] == s [ count ] ) count ++ ; } return count ; } let S = " " ; let T = " " ; document . write ( maxPrefix ( S , T ) ) ;
Replace all occurrences of string AB with C without using extra space | Javascript program to replace all occurrences of " AB " with " C " ; Start traversing from second character ; If previous character is ' A ' and current character is 'B" ; Replace previous character with ' C ' and move all subsequent characters one position back ; Driver code
function translate ( str ) { for ( let i = 1 ; i < str . length ; i ++ ) { if ( str [ i - 1 ] == ' ' && str [ i ] == ' ' ) { str [ i - 1 ] = ' ' ; let j ; for ( j = i ; j < str . length - 1 ; j ++ ) str [ j ] = str [ j + 1 ] ; str [ j ] = ' ' ; } } return ; } let st = " " ; let str = st . split ( " " ) ; translate ( str ) ; document . write ( " " ) ; document . write ( str . join ( " " ) ) ;
Find all occurrences of a given word in a matrix | Javascript Program to find all occurrences of the word in a matrix ; check whether given cell ( row , col ) is a valid cell or not . ; return true if row number and column number is in range ; These arrays are used to get row and column numbers of 8 neighboursof a given cell ; A utility function to do DFS for a 2D boolean matrix . It only considers the 8 neighbours as adjacent vertices ; return if current character doesn 't match with  the next character in the word ; append current character position to path ; current character matches with the last character in the word ; Recur for all connected neighbours ; The main function to find all occurrences of the word in a matrix ; traverse through the all cells of given matrix ; occurrence of first character in matrix ; check and print if path exists ; Driver code
let ROW = 3 ; let COL = 5 ; function isvalid ( row , col , prevRow , prevCol ) { return ( row >= 0 ) && ( row < ROW ) && ( col >= 0 ) && ( col < COL ) && ! ( row == prevRow && col == prevCol ) ; } let rowNum = [ - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 ] ; let colNum = [ - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 ] ; function DFS ( mat , row , col , prevRow , prevCol , word , path , index , n ) { if ( index > n mat [ row ] [ col ] != word [ index ] ) return ; path += ( word [ index ] ) + " " + ( row ) . toString ( ) + " " + ( col ) . toString ( ) + " " ; if ( index == n ) { document . write ( path + " " ) ; return ; } for ( let k = 0 ; k < 8 ; ++ k ) if ( isvalid ( row + rowNum [ k ] , col + colNum [ k ] , prevRow , prevCol ) ) DFS ( mat , row + rowNum [ k ] , col + colNum [ k ] , row , col , word , path , index + 1 , n ) ; } function findWords ( mat , word , n ) { for ( let i = 0 ; i < ROW ; ++ i ) for ( let j = 0 ; j < COL ; ++ j ) if ( mat [ i ] [ j ] == word [ 0 ] ) DFS ( mat , i , j , - 1 , - 1 , word , " " , 0 , n ) ; } let mat = [ [ ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' , ' ' ] ] ; let word = " " . split ( " " ) ; findWords ( mat , word , word . length - 1 ) ;
Generate N Random Hexadecimal Numbers | Maximum length of the random integer ; Function to generate N Hexadecimal integers ; Stores all the possible charcters in the Hexadecimal notation ; Loop to print N integers ; Randomly select length of the int in the range [ 1 , maxSize ] ; Print len charcters ; Print a randomly selected character ; Driver Code
const maxSize = 10 ; function randomHexInt ( N ) { let hexChar = [ ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' , ' ' ] ; for ( let i = 0 ; i < N ; i ++ ) { let len = Math . random ( ) * ( maxSize - 1 ) + 1 ; for ( let j = 0 ; j < len ; j ++ ) { document . write ( hexChar [ Math . floor ( Math . random ( ) * ( 16 ) ) ] ) ; } document . write ( " " ) } } let N = 3 ; randomHexInt ( N ) ;
Check if two array of string are equal by performing swapping operations | Function to check whether brr [ ] can be made equal to arr [ ] by doing swapping operations ; siz variable to store siz of string ; iterate till N to sort strings ; sort each string in arr [ ] ; sort each string in brr [ ] ; Sort both the vectors so that all the comparable strings will be arranged ; iterate till N to compare string in arr [ ] and brr [ ] ; Compare each string ; Return false becaues if atleast one string is not equal then brr [ ] cannot be converted to arr [ ] ; All the strings are compared so at last return true . ; Driver code ; Store the answer in variable
function checkEqualArrays ( arr , brr , N ) { let siz = arr [ 0 ] . length ; for ( let i = 0 ; i < N ; i ++ ) { arr [ i ] = arr [ i ] . split ( " " ) . sort ( ) . join ( " " ) brr [ i ] = brr [ i ] . split ( " " ) . sort ( ) . join ( " " ) } arr . sort ( ) brr . sort ( ) for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != brr [ i ] ) { return false ; } } return true ; } let N = 2 ; let arr = [ " " , " " ] ; let brr = [ " " , " " ] ; let ans = checkEqualArrays ( arr , brr , N ) ; if ( ans ) document . write ( " " ) ; else document . write ( " " ) ;
Maximum length of string formed by concatenation having even frequency of each character | Javascript implementation of the above approach ; Function to check the string ; Count the frequency of the string ; Check the frequency of the string ; Store the length of the new String ; Function to find the longest concatenated string having every character of even frequency ; Checking the string ; Dont Include the string ; Include the string ; Driver code ; Call the function ; Print the answer
let maxi = 0 ; let ans1 = " " ; function calculate ( ans ) { let dp = new Array ( 26 ) . fill ( 0 ) ; for ( let i = 0 ; i < ans . length ; ++ i ) { dp [ ans [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } for ( let i = 0 ; i < 26 ; ++ i ) { if ( dp [ i ] % 2 == 1 ) { return ; } } if ( maxi < ans . length ) { maxi = ans . length ; ans1 = ans ; } } function longestString ( arr , index , str ) { if ( index == arr . length ) { return ; } longestString ( arr , index + 1 , str ) ; str += arr [ index ] ; calculate ( str ) ; longestString ( arr , index + 1 , str ) ; } let A = [ " " , " " , " " , " " , " " ] ; longestString ( A , 0 , " " ) ; document . write ( ans1 + " " + ans1 . length ) ;
Find the next number by adding natural numbers in order on alternating indices from last | Function to generate the resultant number using the given criteria ; Storing end result ; Find the length of numeric string ; Traverse the string ; Storing digit at ith position ; Checking that the number would be added or not ; Logic for summing the digits if the digit is greater than 10 ; Storing the result ; Returning the result ; Driver Code
function generateNumber ( number ) { let temp = 0 , adding_number = 0 ; let result = " " ; let len = number . length ; for ( let i = len - 1 ; i >= 0 ; i -- ) { let digit = parseInt ( number [ i ] ) ; if ( temp % 2 == 0 ) { adding_number += 1 ; digit += adding_number ; if ( digit >= 10 ) { digit %= 9 ; if ( digit == 0 ) digit = 9 ; } } result = ( digit ) . toString ( ) + result ; temp += 1 ; } return result ; } let S = " " ; document . write ( generateNumber ( S ) ) ;
Find the single digit sum of alphabetical values of a string | Javascript program for the above approach ; Traverse the given string ; If character is an alphabet ; Stores the sum of order of values ; Find the score ; Find the single digit sum ; Return the resultant sum ; Driver Code
function findTheSum ( str ) { let alpha = [ ] ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( ( str [ i ] . charCodeAt ( 0 ) >= " " . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) || ( str [ i ] . charCodeAt ( 0 ) >= " " . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) ) alpha . push ( str [ i ] ) ; } let score = 0 , n = 0 ; for ( let i = 0 ; i < alpha . length ; i ++ ) { if ( alpha [ i ] . charCodeAt ( 0 ) >= " " . charCodeAt ( 0 ) && alpha [ i ] . charCodeAt ( 0 ) <= " " . charCodeAt ( 0 ) ) score += alpha [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) + 1 ; else score += alpha [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) + 1 ; } while ( score > 0 n > 9 ) { if ( score == 0 ) { score = n ; n = 0 ; } n += score % 10 ; score = Math . floor ( score / 10 ) ; } return n ; } let S = " " ; document . write ( findTheSum ( S ) ) ;
Minimum value of K such that each substring of size K has the given character | Function to find the minimum value of K such that char c occurs in all K sized substrings of string S ; Store the string length ; Store difference of lengths of segments of every two consecutive occurrences of c ; Stores the maximum difference ; Store the previous occurence of char c ; Check if the current character is c or not ; Stores the difference of consecutive occurrences of c ; Update previous occurrence of c with current occurence ; Comparing diff with max ; If string doesn 't contain c ; Return max ; Driver Code
function findK ( s , c ) { let n = s . length ; let diff ; let max = 0 ; let prev = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == c ) { diff = i - prev ; prev = i ; if ( diff > max ) { max = diff ; } } } if ( max == 0 ) return - 1 ; return max ; } let S = " " ; let ch = " " ; document . write ( findK ( S , ch ) ) ;
Count subsequences 01 in string generated by concatenation of given numeric string K times | Function to calculate the number of subsequences of "01" ; Store count of 0 ' s ▁ and ▁ 1' s ; Count of subsequences without concatenation ; Case 1 ; Case 2 ; Return the total count ; Driver Code
function countSubsequence ( S , N , K ) { let C = 0 , C1 = 0 , C0 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == " " ) C1 ++ ; else if ( S [ i ] == " " ) C0 ++ ; } let B1 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == " " ) B1 ++ ; else if ( S [ i ] == " " ) C = C + ( C1 - B1 ) ; } let ans = C * K ; ans += C1 * C0 * ( ( K * ( K - 1 ) ) / 2 ) ; return ans ; } let S = " " ; let K = 2 ; let N = S . length ; document . write ( countSubsequence ( S , N , K ) ) ;
Make Palindrome binary string with exactly a 0 s and b 1 s by replacing wild card ? | Function to convert the given string to a palindrome with a 0 ' s ▁ and ▁ b ▁ 1' s ; Stores the size of the string ; Loop to iterate over the string ; If one of S [ i ] or S [ N - i - 1 ] is equal to ' ? ' , replace it with corresponding character ; Subtract the count of 0 from the required number of zeroes ; Subtract the count of 1 from required number of ones ; Traverse the string ; If both S [ i ] and S [ N - i - 1 ] are ' ? ' ; If a is greater than b ; Update S [ i ] and S [ N - i - 1 ] to '0' ; Update the value of a ; Update S [ i ] and S [ N - i - 1 ] to '1' ; Update the value of b ; Case with middle character ' ? ' in case of odd length string ; If a is greater than b ; Update middle character with '0' ; Update middle character by '1' ; Return Answer ; Driver Code
const convertString = ( S , a , b ) => { let N = S . length ; for ( let i = 0 ; i < parseInt ( N / 2 ) ; ++ i ) { if ( S [ i ] == ' ' && S [ N - i - 1 ] != ' ' ) { S [ i ] = S [ N - i - 1 ] ; } else if ( S [ i ] != ' ' && S [ N - i - 1 ] == ' ' ) { S [ N - i - 1 ] = S [ i ] ; } } let cnt_0 = 0 ; for ( let i = 0 ; i < N ; ++ i ) { if ( S [ i ] == ' ' ) cnt_0 ++ ; } a = a - cnt_0 ; let cnt_1 = 0 ; for ( let i = 0 ; i < N ; ++ i ) { if ( S [ i ] == ' ' ) cnt_1 ++ ; } b = b - cnt_1 ; for ( let i = 0 ; i < parseInt ( N / 2 ) ; ++ i ) { if ( S [ i ] == ' ' && S [ N - i - 1 ] == ' ' ) { if ( a > b ) { S [ i ] = S [ N - i - 1 ] = ' ' ; a -= 2 ; } else { S [ i ] = S [ N - i - 1 ] = ' ' ; b -= 2 ; } } } if ( S [ parseInt ( N / 2 ) ] == ' ' ) { if ( a > b ) { S [ parseInt ( N / 2 ) ] = ' ' ; a -- ; } else { S [ parseInt ( N / 2 ) ] = ' ' ; b -- ; } } if ( a == 0 && b == 0 ) { return S ; } else { return " " ; } } let S = " " ; S = S . split ( ' ' ) ; let a = 4 , b = 4 ; document . write ( convertString ( S , a , b ) . join ( ' ' ) ) ;
Minimum adjacent swaps required to make a binary string alternating | Function to find the minimum number of adjacent swaps to make the string alternating ; Count the no of zeros and ones ; Base Case ; Store no of min swaps when string starts with "1" ; Keep track of the odd positions ; Checking for when the string starts with "1" ; Adding the no of swaps to fix "1" at odd positions ; Store no of min swaps when string starts with "0" ; Keep track of the odd positions ; Checking for when the string starts with "0" ; Adding the no of swaps to fix "1" at odd positions ; Returning the answer based on the conditions when string length is even ; When string length is odd ; When no of ones is greater than no of zeros ; When no of ones is greater than no of zeros ; Driver Code
function minSwaps ( s ) { let ones = 0 , zeros = 0 ; let N = s . length ; for ( let i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == ' ' ) ones ++ ; else zeros ++ ; } if ( ( N % 2 == 0 && ones != zeros ) || ( N % 2 == 1 && Math . abs ( ones - zeros ) != 1 ) ) { return - 1 ; } let ans_1 = 0 ; let j = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == ' ' ) { ans_1 += Math . abs ( j - i ) ; j += 2 ; } } let ans_0 = 0 ; let k = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == ' ' ) { ans_0 += Math . abs ( k - i ) ; k += 2 ; } } if ( N % 2 == 0 ) return Math . min ( ans_1 , ans_0 ) ; else { if ( ones > zeros ) return ans_1 ; else return ans_0 ; } } let S = " " ; document . write ( minSwaps ( S ) ) ;
Print all ways to reach the Nth stair with the jump of 1 or 2 units at a time | Function to find all the ways to reach Nth stair using one or two jumps ; Base Cases ; Recur for jump1 and jump2 ; Stores the total possible jumps ; Add "1" with every element present in jump1 ; Add "2" with every element present in jump2 ; Driver Code
const TotalPossibleJumps = ( N ) => { if ( ( N - 1 ) == 0 ) { let newvec = [ ] ; newvec . push ( " " ) ; return newvec ; } else { if ( N < 0 ) { let newvec = [ ] ; return newvec ; } } let jump1 = TotalPossibleJumps ( N - 1 ) ; let jump2 = TotalPossibleJumps ( N - 2 ) ; let totaljumps = [ ] ; for ( let s in jump1 ) { totaljumps . push ( " " + jump1 [ s ] ) ; } for ( let s in jump2 ) { totaljumps . push ( " " + jump2 [ s ] ) ; } return totaljumps ; } let N = 3 ; let Ans = TotalPossibleJumps ( N ) ; for ( let it in Ans ) document . write ( ` ${ Ans [ it ] } ` ) ;
Find any permutation of Binary String of given size not present in Array | Function to find a binary string of N bits that does not occur in the givrn array arr [ ] ; Stores the resultant string ; Loop to iterate over all the given strings in a diagonal order ; Append the complement of element at current index into ans ; Return Answer ; Driver code
function findString ( arr , N ) { let ans = " " ; for ( let i = 0 ; i < N ; i ++ ) { ans += arr [ i ] [ i ] == ' ' ? ' ' : ' ' ; } return ans ; } let arr = [ " " , " " , " " ] ; let N = arr . length ; document . write ( findString ( arr , N ) ) ;
Check if the string has a reversible equal substring at the ends | Function to print longest substring that appears at beginning of string and also at end in reverse order ; Stores the resultant string ; If the characters are same ; Otherwise , break ; If the string can 't be formed ; Otherwise print resultant string ; Driver Code
function commonSubstring ( s ) { let n = s . length ; let i = 0 ; let j = n - 1 ; let ans = " " ; while ( j >= 0 ) { if ( s [ i ] == s [ j ] ) { ans += s [ i ] ; i ++ ; j -- ; } else { break ; } } if ( ans . length == 0 ) document . write ( " " ) ; else { document . write ( " " + " " + ans ) ; } } let S = " " ; commonSubstring ( S ) ;
Length of Smallest Non Prime Subsequence in given numeric String | Function to find the smallest length of resultant subsequence ; Check for a subsequence of size 1 ; Check for a subsequence of size 2 ; If none of the above check is successful then subsequence must be of size 3 ; Never executed ; Driver Code
function findMinimumSubsequence ( S ) { let flag = false ; for ( let j = 0 ; j < S . length ; j ++ ) { if ( S [ j ] != ' ' && S [ j ] != ' ' && S [ j ] != ' ' && S [ j ] != ' ' ) { document . write ( 1 ) ; flag = true ; break ; } } if ( flag == false ) { for ( let j = 0 ; j < S . length ; j ++ ) { for ( let j1 = j + 1 ; j1 < S . length ; j1 ++ ) { let dummy = S [ j ] + S [ j1 ] ; if ( dummy != " " && dummy != " " && dummy != " " && dummy != " " ) { document . write ( 2 ) ; } if ( flag == true ) break ; else flag = true ; } if ( flag == true ) break ; } } if ( flag == false ) { if ( S . length >= 3 ) { document . write ( 3 ) ; } else { document . write ( - 1 ) ; } } } let S = " " ; findMinimumSubsequence ( S ) ;
Minimize changes to make all characters equal by changing vowel to consonant and vice versa | Function to find the minimum number of steps to make all characters equal ; Initializing the variables ; Store the frequency ; Iterate over the string ; Calculate the total number of vowels ; Storing frequency of each vowel ; Count the consonants ; Storing the frequency of each consonant ; Iterate over the 2 maps ; Maximum frequency of consonant ; Maximum frequency of vowel ; Find the result ; Driver Code
function operations ( s ) { let ans = 0 ; let vowels = 0 , consonants = 0 ; let max_vowels = Number . MIN_VALUE ; let max_consonants = Number . MIN_VALUE ; let freq_consonants = new Map ( ) ; let freq_vowels = new Map ( ) ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s . charAt ( i ) == ' ' || s . charAt ( i ) == ' ' || s . charAt ( i ) == ' ' || s . charAt ( i ) == ' ' || s . charAt ( i ) == ' ' ) { vowels += 1 ; if ( freq_vowels . has ( s . charAt ( i ) ) ) { freq_vowels . set ( s . charAt ( i ) , freq_vowels . get ( s . charAt ( i ) ) + 1 ) } else { freq_vowels . set ( s . charAt ( i ) , 1 ) ; } } else { consonants += 1 ; if ( freq_consonants . has ( s . charAt ( i ) ) ) { freq_consonants . set ( s . charAt ( i ) , freq_consonants . get ( s . charAt ( i ) ) + 1 ) } else { freq_consonants . set ( s . charAt ( i ) , 1 ) ; } } } for ( let [ key , value ] of freq_consonants ) { max_consonants = Math . max ( max_consonants , value ) ; } for ( let [ key1 , value1 ] of freq_vowels ) { max_vowels = Math . max ( max_vowels , value1 ) ; } ans = Math . min ( ( 2 * ( vowels - max_vowels ) + consonants ) , ( 2 * ( consonants - max_vowels ) + consonants ) ) ; return ans ; } let S = " " ; document . write ( operations ( S ) ) ;