codeparrot/xlcost-text-to-code huggingface dataset:
Print n 0 s and m 1 s such that no two 0 s and no three 1 s are together | Function to print the required pattern ; When condition fails ; When m = n - 1 ; Driver code
function printPattern ( n , m ) { if ( m > 2 * ( n + 1 ) m < n - 1 ) { document . write ( " " ) ; } else if ( Math . abs ( n - m ) <= 1 ) { while ( n > 0 && m > 0 ) { document . write ( " " ) ; n -- ; m -- ; } if ( n != 0 ) { document . write ( " " ) ; } if ( m != 0 ) { document . write ( " " ) ; } } else { while ( m - n > 1 && n > 0 ) { document . write ( " " ) ; m = m - 2 ; n = n - 1 ; } while ( n > 0 ) { document . write ( " " ) ; n -- ; m -- ; } while ( m > 0 ) { document . write ( " " ) ; m -- ; } } } var n = 4 , m = 8 ; printPattern ( n , m ) ;
Find the count of Strictly decreasing Subarrays | Function to count the number of strictly decreasing subarrays ; Initialize length of current decreasing subarray ; Traverse through the array ; If arr [ i + 1 ] is less than arr [ i ] , then increment length ; Else Update count and reset length ; If last length is more than 1 ; Driver code
function countDecreasing ( A , n ) { var len = 1 ; for ( var i = 0 ; i < n - 1 ; ++ i ) { if ( A [ i + 1 ] < A [ i ] ) len ++ ; else { cnt += parseInt ( ( ( len - 1 ) * len ) / 2 ) ; len = 1 ; } } if ( len > 1 ) cnt += parseInt ( ( ( len - 1 ) * len ) / 2 ) ; return cnt ; } var A = [ 100 , 3 , 1 , 13 ] ; var n = A . length ; document . write ( countDecreasing ( A , n ) ) ;
Minimum changes required to make first string substring of second string | Function to find the minimum number of characters to be replaced in string S2 , such that S1 is a substring of S2 ; Get the sizes of both strings ; Traverse the string S2 ; From every index in S2 , check the number of mis - matching characters in substring of length of S1 ; Take minimum of prev and current mis - match ; return answer ; Driver Code
function minimumChar ( S1 , S2 ) { let n = S1 . length ; let m = S2 . length ; let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < m - n + 1 ; i ++ ) { let minRemovedChar = 0 ; for ( let j = 0 ; j < n ; j ++ ) { if ( S1 [ j ] != S2 [ i + j ] ) { minRemovedChar ++ ; } } ans = Math . min ( minRemovedChar , ans ) ; } return ans ; } let S1 = " " ; let S2 = " " ; document . write ( minimumChar ( S1 , S2 ) ) ;
Frequency of a substring in a string | JavaScript program to count occurrences of pat in txt . ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; If pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Driver Code
let mod = 100000007 ; function countFreq ( pat , txt ) { let M = pat . length ; let N = txt . length ; let res = 0 ; for ( let i = 0 ; i <= N - M ; i ++ ) { let j ; for ( j = 0 ; j < M ; j ++ ) { if ( txt [ i + j ] != pat [ j ] ) { break ; } } if ( j == M ) { res ++ ; j = 0 ; } } return res ; } let txt = " " ; let pat = " " ; document . write ( countFreq ( pat , txt ) ) ;
Optimized Naive Algorithm for Pattern Searching | A modified Naive Pattern Searching algorithm that is optimized for the cases when all characters of pattern are different ; For current index i , check for pattern match ; If pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Slide the pattern by j ; Driver code
function search ( pat , txt ) { let M = pat . length ; let N = txt . length ; let i = 0 ; while ( i <= N - M ) { let j ; for ( j = 0 ; j < M ; j ++ ) if ( txt [ i + j ] != pat [ j ] ) break ; if ( j == M ) { document . write ( " " + i + " " ) ; i = i + M ; } else if ( j == 0 ) i = i + 1 ; else i = i + j ; } } let txt = " " ; let pat = " " ; search ( pat , txt ) ;
Find the missing digit in given product of large positive integers | Function to find the replaced digit in the product of a * b ; Keeps track of the sign of the current digit ; Stores the value of a % 11 ; Find the value of a mod 11 for large value of a as per the derived formula ; Stores the value of b % 11 ; Find the value of b mod 11 for large value of a as per the derived formula ; Stores the value of c % 11 ; Keeps track of the sign of x ; If the current digit is the missing digit , then keep the track of its sign ; Find the value of x using the derived equation ; Check if x has a negative sign ; Return positive equivaluent of x mod 11 ; Driver Code
function findMissingDigit ( a , b , c ) { let w = 1 ; let a_mod_11 = 0 ; for ( let i = a . length - 1 ; i >= 0 ; i -- ) { a_mod_11 = ( a_mod_11 + w * ( a [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) % 11 ; w = w * - 1 ; } let b_mod_11 = 0 ; w = 1 ; for ( let i = b . length - 1 ; i >= 0 ; i -- ) { b_mod_11 = ( b_mod_11 + w * ( b [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) % 11 ; w = w * - 1 ; } let c_mod_11 = 0 ; let xSignIsPositive = true ; w = 1 ; for ( let i = c . length - 1 ; i >= 0 ; i -- ) { if ( c [ i ] == ' ' ) { xSignIsPositive = ( w == 1 ) ; } else { c_mod_11 = ( c_mod_11 + w * ( c [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) % 11 ; } w = w * - 1 ; } let x = ( ( a_mod_11 * b_mod_11 ) - c_mod_11 ) % 11 ; if ( ! xSignIsPositive ) { x = - x ; } return ( x % 11 + 11 ) % 11 ; } let A = " " ; let B = " " ; let C = " " ; document . write ( findMissingDigit ( A , B , C ) ) ;
Check if a string can be made empty by repeatedly removing given subsequence | Function to check if a string can be made empty by removing all subsequences of the form " GFG " or not ; Driver Code
function findIfPossible ( N , str ) { let countG = 0 , countF = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ' ) countG ++ ; else countF ++ ; } if ( 2 * countF != countG ) { document . write ( " " ) ; } else { let id = 0 ; let flag = true ; for ( let i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ' ) { countG -- ; id ++ ; } else { countF -- ; id -- ; } if ( id < 0 ) { flag = false ; break ; } if ( countG < countF ) { flag = false ; break ; } } if ( flag ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } } let n = 6 ; let str = " " ; findIfPossible ( n , str ) ;
Check whether second string can be formed from characters of first string used any number of times | Function to check if str2 can be made by characters of str1 or not ; To store the occurrence of every character ; Length of the two strings ; Assume that it is possible to compose the string str2 from str1 ; Iterate over str1 ; Store the presence of every character ; Iterate over str2 ; Ignore the spaces ; Check for the presence of character in str1 ; If it is possible to make str2 from str1 ; Given strings ; Function Call
function isPossible ( str1 , str2 ) { let arr = new Array ( 256 ) . fill ( 0 ) ; let l1 = str1 . length ; let l2 = str2 . length ; let i , j ; let possible = true ; for ( i = 0 ; i < l1 ; i ++ ) { arr [ str1 [ i ] ] = 1 ; } for ( i = 0 ; i < l2 ; i ++ ) { if ( str2 [ i ] != " " ) { if ( arr [ str2 [ i ] ] == 1 ) continue ; else { possible = false ; break ; } } } if ( possible ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let str1 = " " ; let str2 = " " ; isPossible ( str1 , str2 ) ;
Minimum number of flipping adjacent bits required to make given Binary Strings equal | Function to find the minimum number of inversions required . ; Initializing the answer ; Iterate over the range ; If s1 [ i ] != s2 [ i ] , then inverse the characters at i snd ( i + 1 ) positions in s1 . ; Adding 1 to counter if characters are not same ; Driver Code
function find_Min_Inversion ( n , s1 , s2 ) { let count = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) { if ( s1 [ i ] = ' ' ) { s1 [ i ] = ' ' ; } else { s1 [ i ] = ' ' ; } if ( s1 [ i + 1 ] = ' ' ) { s1 [ i + 1 ] = ' ' ; } else { s1 [ i + 1 ] = ' ' ; } count ++ ; } } if ( s1 != s2 ) { return count ; } return - 1 ; } let n = 4 ; let s1 = " " ; let s2 = " " ; document . write ( find_Min_Inversion ( n , s1 , s2 ) ) ;
Longest subsequence with consecutive English alphabets | Function to find the length of subsequence starting with character ch ; Length of the string ; Stores the maximum length ; Traverse the given string ; If s [ i ] is required character ch ; Increment ans by 1 ; Increment character ch ; Return the current maximum length with character ch ; Function to find the maximum length of subsequence of consecutive characters ; Stores the maximum length of consecutive characters ; Update ans ; Return the maximum length of subsequence ; Input ; Function Call
function findSubsequence ( S , ch ) { let N = S . length ; let ans = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ch ) { ans ++ ; ch = String . fromCharCode ( ch . charCodeAt ( 0 ) + 1 ) ; } } return ans ; } function findMaxSubsequence ( S ) { let ans = 0 ; for ( let ch = ' ' . charCodeAt ( 0 ) ; ch <= ' ' . charCodeAt ( 0 ) ; ch ++ ) { ans = Math . max ( ans , findSubsequence ( S , String . fromCharCode ( ch ) ) ) ; } return ans ; } let S = " " ; document . write ( findMaxSubsequence ( S ) ) ;
Minimum number of alternate subsequences required to be removed to empty a Binary String | Function to find the minimum number of operations to empty a binary string ; Stores the resultant number of operations ; Stores the number of 0 s ; Stores the number of 1 s ; Traverse the given string ; To balance 0 with 1 if possible ; Increment the value of cn0 by 1 ; To balance 1 with 0 if possible ; Increment the value of cn1 ; Update the maximum number of unused 0 s and 1 s ; Print the resultant count ; Driver Code
function minOpsToEmptyString ( s ) { var ans = Number . MIN_VALUE ; var cn0 = 0 ; var cn1 = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s . charAt ( i ) == ' ' ) { if ( cn1 > 0 ) cn1 -- ; cn0 ++ ; } else { if ( cn0 > 0 ) cn0 -- ; cn1 ++ ; } ans = Math . max ( ans , Math . max ( cn0 , cn1 ) ) ; } document . write ( ans ) ; } var S = " " ; minOpsToEmptyString ( S ) ;
Smallest string obtained by removing all occurrences of 01 and 11 from Binary String | Set 2 | Function to find the length of the smallest string possible by removing substrings "01" and "11" ; Stores the length of the smallest string ; Traverse the string S ; If st is greater than 0 and S [ i ] is '1' ; Delete the last character and decrement st by 1 ; Otherwise ; Increment st by 1 ; Return the answer in st ; Input ; Function call
function shortestString ( S , N ) { let st = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( st > 0 && S [ i ] == ' ' ) { st -- ; } else { st ++ ; } } return st ; } let S = " " ; let N = S . length ; document . write ( shortestString ( S , N ) ) ;
Longest Non | Function to find the length of the longest non - increasing subsequence ; Stores the prefix and suffix count of 1 s and 0 s respectively ; Initialize the array ; Store the number of '1' s up to current index i in pre ; Find the prefix sum ; If the current element is '1' , update the pre [ i ] ; Store the number of '0' s over the range [ i , N - 1 ] ; Find the suffix sum ; If the current element is '0' , update post [ i ] ; Stores the maximum length ; Find the maximum value of pre [ i ] + post [ i ] ; Return the answer ; Driver Code
function findLength ( str , n ) { let pre = Array . from ( { length : n } , ( _ , i ) => 0 ) ; let post = Array . from ( { length : n } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { pre [ i ] = 0 ; post [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { if ( i != 0 ) { pre [ i ] += pre [ i - 1 ] ; } if ( str [ i ] == ' ' ) { pre [ i ] += 1 ; } } for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( i != n - 1 ) post [ i ] += post [ i + 1 ] ; if ( str [ i ] == ' ' ) post [ i ] += 1 ; } let ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { ans = Math . max ( ans , pre [ i ] + post [ i ] ) ; } return ans ; } let S = " " ; document . write ( findLength ( S , S . length ) ) ;
Number of substrings having an equal number of lowercase and uppercase letters | Function to find the count of substrings having an equal number of uppercase and lowercase characters ; Stores the count of prefixes having sum S considering uppercase and lowercase characters as 1 and - 1 ; Stores the count of substrings having equal number of lowercase and uppercase characters ; Stores the sum obtained so far ; If the character is uppercase ; Otherwise ; If currsum is o ; If the current sum exists in the HashMap prevSum ; Increment the resultant count by 1 ; Update the frequency of the current sum by 1 ; Return the resultant count of the subarrays ; Driver Code
function countSubstring ( S , N ) { var prevSum = new Map ( ) ; var res = 0 ; var currentSum = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( S [ i ] >= ' ' && S [ i ] <= ' ' ) { currentSum ++ ; } else currentSum -- ; if ( currentSum == 0 ) res ++ ; if ( prevSum . has ( currentSum ) ) { res += ( prevSum . get ( currentSum ) ) ; } if ( prevSum . has ( currentSum ) ) prevSum . set ( currentSum , prevSum . get ( currentSum ) + 1 ) else prevSum . set ( currentSum , 1 ) } return res ; } var S = " " ; document . write ( countSubstring ( S , S . length ) ) ;
Number of substrings with each character occurring even times | Function to count substrings having even frequency of each character ; Stores the total count of substrings ; Traverse the range [ 0 , N ] : ; Traverse the range [ i + 1 , N ] ; Stores the substring over the range of indices [ i , len ] ; Stores the frequency of characters ; Count frequency of each character ; Traverse the dictionary ; If any of the keys have odd count ; Otherwise ; Return count ; Driver Code
function subString ( s , n ) { var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var len = i + 1 ; len <= n ; len ++ ) { var test_str = s . substring ( i , len ) ; var res = { } ; var temp = test_str . split ( " " ) ; for ( const keys of temp ) { res [ keys ] = ( res [ keys ] ? res [ keys ] : 0 ) + 1 ; } var flag = 0 ; for ( const [ key , value ] of Object . entries ( res ) ) { if ( res [ key ] % 2 != 0 ) { flag = 1 ; break ; } } if ( flag == 0 ) count += 1 ; } } return count ; } var S = " " ; var N = S . length ; document . write ( subString ( S , N ) ) ;
Count new pairs of strings that can be obtained by swapping first characters of pairs of strings from given array | Function to count new pairs of strings that can be obtained by swapping first characters of any pair of strings ; Stores the count of pairs ; Generate all possible pairs of strings from the array arr [ ] ; Stores the current pair of strings ; Swap the first characters ; Check if they are already present in the array or not ; If both the strings are not present ; Increment the ans by 1 ; Print the resultant count ; Driver Code
function countStringPairs ( a , n ) { var ans = 0 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { var p = a [ i ] ; var q = a [ j ] ; if ( p [ 0 ] !== q [ 0 ] ) { p = p . split ( " " ) ; q = q . split ( " " ) ; var temp = p [ 0 ] ; p [ 0 ] = q [ 0 ] ; q [ 0 ] = temp ; p = p . join ( " " ) ; q = q . join ( " " ) ; var flag1 = 0 ; var flag2 = 0 ; for ( let k = 0 ; k < n ; k ++ ) { if ( a [ k ] === p ) flag1 = 1 ; if ( a [ k ] === q ) flag2 = 1 ; } if ( flag1 === 0 && flag2 === 0 ) { ans = ans + 1 ; } } } } document . write ( ans ) ; } var arr = [ " " , " " , " " ] ; var N = arr . length ; countStringPairs ( arr , N ) ;
Modify string by replacing characters by alphabets whose distance from that character is equal to its frequency | Function to modify string by replacing characters by the alphabet present at distance equal to frequency of the string ; Stores frequency of characters ; Stores length of the string ; Traverse the given string S ; Increment frequency of current character by 1 ; Traverse the string ; Store the value to be added to the current character ; Check if after adding the frequency , the character is less than ' z ' or not ; Otherwise , update the value of add so that s [ i ] doesn ' t ▁ exceed ▁ ' z ' ; Print the modified string ; Driver Code
function addFrequencyToCharacter ( s ) { var frequency = new Array ( 26 ) . fill ( 0 ) ; var n = s . length ; for ( var i = 0 ; i < n ; i ++ ) { frequency [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] += 1 ; } for ( var i = 0 ; i < n ; i ++ ) { var add = frequency [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] % 26 ; if ( s [ i ] . charCodeAt ( 0 ) + add <= " " . charCodeAt ( 0 ) ) s [ i ] = String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) + add ) ; else { add = s [ i ] . charCodeAt ( 0 ) + add - " " . charCodeAt ( 0 ) ; s [ i ] = String . fromCharCode ( " " . charCodeAt ( 0 ) + add - 1 ) ; } } document . write ( s . join ( " " ) + " " ) ; } var str = " " ; addFrequencyToCharacter ( str . split ( " " ) ) ;
Check if it is possible to reach any point on the circumference of a given circle from origin | Function to check if it is possible to reach any point on circumference of the given circle from ( 0 , 0 ) ; Stores the count of ' L ' , ' R ' ; Stores the count of ' U ' , ' D ' ; Traverse the string S ; Update the count of L ; Update the count of R ; Update the count of U ; Update the count of D ; Condition 1 for reaching the circumference ; Store the the value of ( i * i ) in the Map ; Check if ( r_square - i * i ) already present in HashMap ; If it is possible to reach the point ( mp [ r_square - i * i ] , i ) ; If it is possible to reach the point ( i , mp [ r_square - i * i ] ) ; If it is impossible to reach ; Driver Code
function isPossible ( S , R , N ) { var cntl = 0 , cntr = 0 ; var cntu = 0 , cntd = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' ' ) cntl ++ ; else if ( S [ i ] == ' ' ) cntr ++ ; else if ( S [ i ] == ' ' ) cntu ++ ; else cntd ++ ; } if ( Math . max ( Math . max ( cntl , cntr ) , Math . max ( cntu , cntd ) ) >= R ) return " " ; var mp = new Map ( ) ; var r_square = R * R ; for ( var i = 1 ; i * i <= r_square ; i ++ ) { mp . set ( i * i , i ) ; if ( mp . has ( r_square - i * i ) ) { if ( Math . max ( cntl , cntr ) >= mp . get ( r_square - i * i ) && Math . max ( cntu , cntd ) >= i ) return " " ; if ( Math . max ( cntl , cntr ) >= i && Math . max ( cntu , cntd ) >= mp . get ( r_square - i * i ) ) return " " ; } } return " " ; } var S = " " ; var R = 5 ; var N = S . length ; document . write ( isPossible ( S , R , N ) ) ;
Modify characters of a string by adding integer values of same | Function to modify a given string by adding ASCII value of characters from a string S to integer values of same indexed characters in string N ; Traverse the string ; Stores integer value of character in string N ; Stores ASCII value of character in string S ; If b exceeds 122 ; Replace the character ; Print resultant string ; Given strings ; Function call to modify string S by given operations
function addASCII ( S , N ) { var newStr = new Array ( S . length ) ; for ( var i = 0 ; i < S . length ; i ++ ) { var a = N [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ; var b = S [ i ] . charCodeAt ( 0 ) + a ; if ( b > 122 ) b -= 26 ; newStr [ i ] = String . fromCharCode ( b ) ; } document . write ( newStr . join ( " " ) ) ; } var S = " " , N = " " ; addASCII ( S , N ) ;
Minimum number of chairs required to ensure that every worker is seated at any instant | Function to find the minimum number of chairs required to ensure that every worker is seated at any time ; Stores the number of chairs required ; Pointer to iterate ; Stores minimum number of chairs required ; Iterate over every character ; If character is ' E ' ; Increase the count ; Otherwise ; Update maximum value of count obtained at any given time ; Return mini ; Given String ; Function call to find the minimum number of chairs
function findMinimumChairs ( s ) { var count = 0 ; var i = 0 ; var mini = - 2147483647 ; while ( i < s . length ) { if ( s [ i ] == ' ' ) count ++ ; else count -- ; mini = Math . max ( count , mini ) ; i ++ ; } return mini ; } var s = " " ; document . write ( findMinimumChairs ( s ) ) ;
Modify string by inserting characters such that every K | Function to replace all ' ? ' characters in a string such that the given conditions are satisfied ; Traverse the string to Map the characters with respective positions ; Traverse the string again and replace all unknown characters ; If i % k is not found in the Map M , then return - 1 ; Update S [ i ] ; Print the string S ; Driver code
function fillString ( str , k ) { var s = str . split ( " " ) ; var mp = { } ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] !== " " ) { mp [ i % k ] = s [ i ] ; } } for ( var i = 0 ; i < s . length ; i ++ ) { if ( ! mp . hasOwnProperty ( i % k ) ) { document . write ( - 1 ) ; return ; } s [ i ] = mp [ i % k ] ; } document . write ( s . join ( " " ) + " " ) ; } var S = " " ; var K = 4 ; fillString ( S , K ) ;
Rearrange a string S1 such that another given string S2 is not its subsequence | Function to rearrange characters in string S1 such that S2 is not a subsequence of it ; Store the frequencies of characters of string s2 ; Traverse the string s2 ; Update the frequency ; Find the number of unique characters in s2 ; Increment unique by 1 if the condition satisfies ; Check if the number of unique characters in string s2 is 1 ; Store the unique character frequency ; Store occurence of it in s1 ; Find count of that character in the string s1 ; Increment count by 1 if that unique character is same as current character ; If count count_in_s1 is less than count_in_s2 , then print s1 and return ; Otherwise , there is no possible arrangement ; Checks if any character in s2 is less than its next character ; Iterate the string , s2 ; If s [ i ] is greater than the s [ i + 1 ] ; Set inc to 0 ; If inc = 1 , print s1 in decreasing order ; Otherwise , print s1 in increasing order ; Driver code
function rearrangeString ( s1 , s2 ) { let cnt = new Array ( 26 ) ; cnt . fill ( 0 ) ; for ( let i = 0 ; i < s2 . length ; i ++ ) cnt [ s2 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; let unique = 0 ; for ( let i = 0 ; i < 26 ; i ++ ) if ( cnt [ i ] != 0 ) unique ++ ; if ( unique == 1 ) { let count_in_s2 = s2 . length ; let count_in_s1 = 0 ; for ( let i = 0 ; i < s1 . length ; i ++ ) if ( s1 [ i ] == s2 [ 0 ] ) count_in_s1 ++ ; if ( count_in_s1 < count_in_s2 ) { document . write ( s1 . join ( " " ) ) ; return ; } document . write ( - 1 ) ; } else { let inc = 1 ; for ( let i = 0 ; i < s2 . length - 1 ; i ++ ) if ( s2 [ i ] . charCodeAt ( ) > s2 [ i + 1 ] . charCodeAt ( ) ) inc = 0 ; if ( inc == 1 ) { s1 . sort ( ) ; s1 . reverse ( ) ; document . write ( s1 . join ( " " ) ) ; } else { s1 . sort ( ) ; document . write ( s1 . join ( " " ) ) ; } } } let s1 = " " . split ( ' ' ) ; let s2 = " " . split ( ' ' ) ; rearrangeString ( s1 , s2 ) ;
Check if a string can be emptied by removing all subsequences of the form "10" | Function to find if string is reducible to NULL ; Length of string ; Stack to store all 1 s ; Iterate over the characters of the string ; If current character is 1 ; Push it into the stack ; Pop from the stack ; If the stack is empty ; Driver code
function isReducible ( str ) { let N = str . length ; let s = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ' ) s . push ( str [ i ] ) ; else if ( s . length > 0 ) s . pop ( ) ; else return false ; } if ( s . length == 0 ) { return true ; } else { return false ; } } let str = " " ; if ( isReducible ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimize flips required such that string does not any pair of consecutive 0 s | Function to find minimum flips required such that a String does not contain any pair of consecutive 0 s ; Stores minimum count of flips ; Iterate over the characters of the String ; If two consecutive characters are equal to '0' ; Update S [ i + 1 ] ; Update cntOp ; Driver Code
function cntMinOperation ( S , N ) { let cntOp = 0 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( S [ i ] == ' ' && S [ i + 1 ] == ' ' ) { S [ i + 1 ] = ' ' ; cntOp += 1 ; } } return cntOp ; } let S = " " ; let N = S . length ; document . write ( cntMinOperation ( S . split ( ' ' ) , N ) ) ;