codeparrot/xlcost-text-to-code huggingface dataset:
Count of ways to empty given String by recursively removing all adjacent duplicates | Define the dp table globally ; Recursive function to calculate the dp values for range [ L , R ] ; The range is odd length ; The state is already calculated ; If the length is 2 ; Total answer for this state ; Variable to store the current answer . ; Remove characters s [ l ] and s [ i ] . ; Calculate all Combinations ; Driver Code
var dp = new Array ( 505 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 505 ) . fill ( - 1 ) ; } var choose = new Array ( 505 ) ; for ( var i = 0 ; i < choose . length ; i ++ ) { choose [ i ] = new Array ( 505 ) . fill ( 0 ) ; } function calc ( l , r , s ) { if ( Math . abs ( r - l ) % 2 == 0 ) { return 0 ; } if ( l > r ) { return dp [ l ] [ r ] = 1 ; } if ( dp [ l ] [ r ] != - 1 ) { return dp [ l ] [ r ] ; } if ( ( r - l ) == 1 ) { if ( s [ l ] == s [ r ] ) { dp [ l ] [ r ] = 1 ; } else { dp [ l ] [ r ] = 0 ; } return dp [ l ] [ r ] ; } let ans = 0 ; for ( let k = l + 1 ; k <= r ; k += 2 ) { let temp = 1 ; if ( s [ l ] == s [ k ] ) { temp = calc ( l + 1 , k - 1 , s ) * calc ( k + 1 , r , s ) * choose [ Math . floor ( ( r - l + 1 ) / 2 ) ] [ Math . floor ( ( r - k ) / 2 ) ] ; ans += temp ; } } return dp [ l ] [ r ] = ans ; } function waysToClearString ( S ) { let n = S . length ; choose [ 0 ] [ 0 ] = 1 ; for ( let i = 1 ; i <= Math . floor ( n / 2 ) ; ++ i ) { choose [ i ] [ 0 ] = 1 ; for ( let j = 1 ; j <= i ; ++ j ) { choose [ i ] [ j ] = ( choose [ i - 1 ] [ j ] + choose [ i - 1 ] [ j - 1 ] ) ; } } return calc ( 0 , n - 1 , S ) ; } let S = " " ; document . write ( waysToClearString ( S ) ) ;
Divide given numeric string into at most two increasing subsequences which form an increasing string upon concatenation | Function to check for valid subsequences ; Stores which element belongs to which subsequence ; Check for each pos if a possible subsequence exist or not ; Last member of 1 subsequence ; Last Member of 2 nd subsequence ; Check if current element can go to 2 nd subsequence ; Check if the current elements belongs to first subsequence ; If the current element does not belong to any subsequence ; Check if last digit of first subsequence is greater than pos ; If a subsequence is found , find the subsequences ; Stores the resulting subsequences ; Print the subsequence ; If no subsequence found , print - 1 ; Driver Code
function findSubsequence ( str ) { let n = str . length ; let res = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) res [ i ] = 0 ; for ( let pos = 0 ; pos <= 9 ; pos ++ ) { let lst1 = ' ' ; let flag = 1 ; let lst2 = String . fromCharCode ( pos + ' ' . charCodeAt ( 0 ) ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( lst2 . charCodeAt ( 0 ) <= str [ i ] . charCodeAt ( 0 ) ) { res [ i ] = ' ' ; lst2 = str [ i ] ; } else if ( lst1 . charCodeAt ( 0 ) <= str [ i ] . charCodeAt ( 0 ) ) { res [ i ] = ' ' ; lst1 = str [ i ] ; } else flag = 0 ; } if ( lst1 . charCodeAt ( 0 ) > pos + ' ' . charCodeAt ( 0 ) ) { flag = 0 ; } if ( flag ) { let S1 = " " ; let S2 = " " ; for ( let i = 0 ; i < n ; i ++ ) { if ( res [ i ] == ' ' ) { S1 += str [ i ] ; } else { S2 += str [ i ] ; } } document . write ( S1 + ' ' + S2 + ' ' ) ; return ; } } document . write ( " " ) ; } let S = " " ; findSubsequence ( S ) ; S = " " ; findSubsequence ( S ) ;
Find Kth lexicographical ordered numeric string of length N with distinct products of each substring | JavaScript program for the above approach ; Function to find the required String ; If the current length is equal to n exit from here only ; Iterate for all the characters ; Check if the product is present before ; If the current String is good then recurse for the next character ; Decrease all the products back to their original state ; Erase the last character ; Function to calculate kth ordered valid String ; Check for the base cases ; There are atmost 10 valid Strings for n = 1 ; Vector to keep a check on number of occurences of products ; Recursively construct the Strings ; Driver Code
var ans , s ; var K ; function getString ( curlen , N , prod ) { if ( curlen == N ) { K -- ; if ( K == 0 ) ans = s ; return ; } var ch ; var ok , t , i ; for ( ch = ' ' ; ch <= ' ' ; ch ++ ) { s += ch ; ok = 1 ; t = 1 ; for ( i = curlen ; i >= 0 && s . length > i ; i -- ) { t *= s . charAt ( i ) - 48 ; if ( prod [ t ] != 0 ) ok = 0 ; prod [ t ] ++ ; } if ( ok != 0 ) getString ( curlen + 1 , N , prod ) ; t = 1 ; for ( i = curlen ; i >= 0 && s . length > i ; i -- ) { t *= s . charAt ( i ) - 48 ; prod [ t ] -- ; } if ( s . length > 0 ) s = s . substring ( 0 , s . length - 1 ) ; } } function kthValidString ( N ) { if ( N > 10 ) { return " " ; } if ( N == 1 ) { if ( K > 10 ) { return " " ; } var s = " " ; K -- ; s += ( K + ' ' ) ; return s ; } var ans = " " ; var s = " " ; var prod = new Array ( 10005 ) ; getString ( 0 , N , prod ) ; return ans ; } var N = 3 ; var K = 4 ; document . write ( kthValidString ( N ) ) ;
Minimum swaps to balance the given brackets at any index | Function to balance the given bracket by swap ; To count the number of uunbalanced pairs ; if there is an opening bracket and we encounter closing bracket then it will decrement the count of unbalanced bracket . ; else it will increment unbalanced pair count ; Driver code
function BalancedStringBySwapping ( s ) { var unbalancedPair = 0 ; for ( var i = 0 ; i < s . length ; ++ i ) { if ( unbalancedPair > 0 && s [ i ] == ' ' ) { -- unbalancedPair ; } else if ( s [ i ] == ' ' ) { ++ unbalancedPair ; } } return ( unbalancedPair + 1 ) / 2 ; } var s = " " ; document . write ( BalancedStringBySwapping ( s ) ) ;
Count of palindromic strings of size upto N consisting of first K alphabets occurring at most twice | Function of return the number of palindromic strings of length N with first K alphabets possible ; If N is odd , half + 1 position can be filled to cope with the extra middle element ; K is reduced by one , because count of choices for the next position is reduced by 1 as a element can only once ; Return the possible count ; Function to find the count of palindromic string of first K characters according to the given criteria ; If N = 1 , then only K palindromic strings possible . ; If N = 2 , the 2 * K palindromic strings possible , K for N = 1 and K for N = 2 ; Initialize ans with the count of strings possible till N = 2 ; Return the possible count ; Driver Code
function lengthNPalindrome ( N , K ) { var half = N / 2 ; if ( N & 1 ) { half += 1 ; } var ans = 1 ; var i ; for ( i = 1 ; i <= half ; i ++ ) { ans *= K ; K -- ; } return ans ; } function palindromicStrings ( N , K ) { if ( N == 1 ) { return K ; } if ( N == 2 ) { return 2 * K ; } ans = 0 ; ans += ( 2 * K ) ; for ( i = 3 ; i <= N ; i ++ ) { ans += lengthNPalindrome ( i , K ) ; } return ans ; } var N = 4 , K = 3 ; document . write ( palindromicStrings ( N , K ) ) ;
Minimum number of characters required to be added to a String such that all lowercase alphabets occurs as a subsequence in increasing order | Function to find the LCS of strings S and string T ; Base Case ; Already Calculated State ; If the characters are the same ; Otherwise ; Function to find the minimum number of characters that needs to be appended in the string to get all lowercase alphabets as a subsequences ; String containing all the characters ; Stores the result of overlapping subproblems ; Return the minimum characters required ; Driver Code
function LCS ( S , N , T , M , dp ) { if ( N == 0 M == 0 ) return 0 ; if ( dp [ N ] [ M ] != 0 ) return dp [ N ] [ M ] ; if ( S . charAt ( N - 1 ) == T . charAt ( M - 1 ) ) { return dp [ N ] [ M ] = 1 + LCS ( S , N - 1 , T , M - 1 , dp ) ; } return dp [ N ] [ M ] = Math . max ( LCS ( S , N - 1 , T , M , dp ) , LCS ( S , N , T , M - 1 , dp ) ) ; } function minimumCharacter ( S ) { var T = " " ; var N = S . length , M = T . length ; var dp = Array ( N + 1 ) . fill ( 0 ) . map ( x => Array ( M + 1 ) . fill ( 0 ) ) ; return ( 26 - LCS ( S , N , T , M , dp ) ) ; } var S = " " ; document . write ( minimumCharacter ( S ) ) ;
Find the string present at the middle of a lexicographically increasing sequence of strings from S to T | Function to print the string at the middle of lexicographically increasing sequence of strings from S to T ; Stores the base 26 digits after addition ; Iterete from right to left and add carry to next position ; Reduce the number to find the middle string by dividing each position by 2 ; If current value is odd , carry 26 to the next index value ; Driver Code
function printMiddleString ( S , T , N ) { let a1 = new Array ( N + 1 ) ; for ( let i = 0 ; i < N ; i ++ ) { a1 [ i + 1 ] = S [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) + T [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ; } for ( let i = N ; i >= 1 ; i -- ) { a1 [ i - 1 ] += a1 [ i ] / 26 ; a1 [ i ] %= 26 ; } for ( let i = 0 ; i <= N ; i ++ ) { if ( a1 [ i ] & 1 ) { if ( i + 1 <= N ) { a1 [ i + 1 ] += 26 ; } } a1 [ i ] = Math . floor ( a1 [ i ] / 2 ) ; } for ( let i = 1 ; i <= N ; i ++ ) { document . write ( String . fromCharCode ( a1 [ i ] + " " . charCodeAt ( 0 ) ) ) ; } return 0 ; } let N = 5 ; let S = " " ; let T = " " ; printMiddleString ( S , T , N ) ;
Find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times | JavaScript program to solve the above approach ; initializing first and second variable as to store how many string ' s ' and string ' t ' will be appended ; storing tmp length ; if length of string tmp is greater than k , then we have reached our destination string now we can return character at index k ; appending s to tmp , f times ; appending t to tmp , s times ; extracting output character ; Driver code
function KthCharacter ( s , t , k ) { let f = 1 ; let ss = 2 ; let tmp = " " ; let len = tmp . length ; while ( len < k ) { let tf = f ; let ts = ss ; while ( tf -- != 0 ) { tmp += s ; } while ( ts -- != 0 ) { tmp += t ; } f += 2 ; ss += 2 ; len = tmp . length ; } let output = tmp [ k - 1 ] ; return output ; } let S1 = " " , S2 = " " ; let k = 4 ; let ans = KthCharacter ( S1 , S2 , k ) ; document . write ( ans ) ;
Find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times | javascript program to solve the above approach ; storing length ; variables for temporary strings of s and t ; final output variable ; if k is greater than even after adding string ' s ' ' first ' times ( let ' s ▁ call ▁ it ▁ x ) ▁ we ' ll subtract x from k ; incrementing first by 2 , as said in problem statement ; if k is greater than even after adding string ' t ' ' second ' times ( let ' s ▁ call ▁ it ▁ y ) ▁ we ' ll subtract y from k ; incrementing second by two as said in problem statement ; if k is smaller than y , then the character will be at position k % m . ; returning character at k index ; if k is smaller than x , then the character is at position k % n ; returning character at k index ; Driver code
function KthCharacter ( s , t , k ) { var n = s . length ; var m = t . length ; var first = 1 ; var second = 2 ; var output = ' ' ; while ( k > 0 ) { if ( k > first * n ) { var x = first * n ; k = k - x ; first = first + 2 ; if ( k > second * m ) { var y = second * m ; k = k - y ; second = second + 2 ; } else { var ind = k % m ; ind -- ; if ( ind < 0 ) ind = m - 1 ; output = t . charAt ( parseInt ( ind ) ) ; break ; } } else { var ind = k % n ; ind -- ; if ( ind < 0 ) ind = n - 1 ; output = s . charAt ( parseInt ( ind ) ) ; break ; } } return output ; } var S1 = " " , S2 = " " ; var k = 4 ; var ans = KthCharacter ( S1 , S2 , k ) ; document . write ( ans ) ;
Lexicographically smallest string with period K possible by replacing ' ? ' s from a given string | Function to modify the given string such that string S is a period of K ; Iterate over the range [ 0 , K ] ; Stores the frequency of the characters S [ i + j * K ] ; Iterate over the string with increment of K ; Print " - 1" ; Replace all characters of the string with ' ? ' to ' a ' to make it smallest ; Otherwise ; Find the character other than ' ? ' ; Replace all characters of the string with ' ? ' to character ch ; Clear the map M ; Return the modified string ; Driver Code
function modifyString ( S , K ) { let N = S . length ; for ( let i = 0 ; i < K ; i ++ ) { let M = new Map ( ) ; for ( let j = i ; j < N ; j += K ) { if ( M . has ( S [ j ] ) ) { M . set ( M . get ( S [ j ] ) , M . get ( S [ j ] ) + 1 ) ; } else { M . set ( S [ j ] , 1 ) ; } } if ( M . size > 2 ) { return " " ; } else if ( M . size == 1 ) { if ( M . has ( ' ' ) ) { for ( let j = i ; j < N ; j += K ) { S = S . replace ( S [ j ] , ' ' ) ; } } } else if ( M . size == 2 ) { let ch ; for ( let it of M . keys ( ) ) { if ( it != ' ' ) { ch = it ; } } for ( let j = i ; j < N ; j += K ) { S = S . replace ( S [ j ] , ch ) ; } } M . clear ( ) ; } return S ; } let S = " " ; let K = 2 ; document . write ( modifyString ( S , K ) ) ;
Count of ways to rearrange N digits and M alphabets keeping all alphabets together | Function to find the factorial of the given number ; Function to count ways to rearrange characters of the string such that all alphabets are adjacent . ; Stores factorial of ( N + 1 ) ; Stores factorial of ; Given a and b ; Function call
function fact ( n ) { var ans = 1 ; for ( var i = 2 ; i <= n ; i ++ ) ans = ans * i ; return ans ; } function findComb ( N , M ) { var x = fact ( N + 1 ) var y = fact ( M ) return ( x * y ) } var N = 2 var M = 2 document . write ( findComb ( N , M ) )
Decode a given string by removing duplicate occurrences | Function to count the appearances of each character ; If the character is lower case ; If the character is uppercase ; If the character is a punctuation mark ; Function to decode the given encoded string ; Iterate the given string str ; Find the index of the next character to be printed ; Driver Code
function findRepitition ( a ) { if ( a . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) && a . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) ) { return a . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; } else if ( a <= ' ' && a >= ' ' ) { return a . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; } return 0 ; } function decodeString ( str ) { let output = " " ; for ( let i = 0 ; i < str . length ; i ++ ) { output += ( str [ i ] ) ; i += findRepitition ( str [ i ] ) ; } document . write ( " " + output + " " + " " ) ; } let str = " " ; decodeString ( str ) ;
Check if a string consisting only of a , b , c can be made empty by removing substring " abc " recursively | Function to check if the given string S can be made empty ; Stores the characters of the string S ; Traverse the given string ; If the character is c ; If stack size is greater than 2 ; Pop from the stack ; Top two characters in the stack should be ' b ' and ' a ' respectively ; Otherwise , print No ; If character is ' a ' or ' b ' push to stack ; If stack is empty , then print Yes . Otherwise print No ; Driver Code
function canMadeEmpty ( s , n ) { let St = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { if ( St . length >= 2 ) { let b = St [ St . length - 1 ] ; St . pop ( ) ; let a = St [ St . length - 1 ] ; St . pop ( ) ; if ( a != ' ' b != ' ' ) return " " ; } else return " " ; } else St . push ( s [ i ] ) ; } if ( St . length == 0 ) { return " " ; } else { return " " ; } } let S = " " ; let N = S . length ; document . write ( canMadeEmpty ( S , N ) ) ;
Maximum count of “ 010. . ” subsequences that can be removed from given Binary String | Function to count the maximum number of operations performed on the string ; Size of the string ; Stores the maximum number of operations performed ; Stores the count of ' X ' occurred so far ; Traverse the string ; Check if current char is ' X ' ; Check if the value of cnt is greater than 0 ; Decrement the value of cnt ; Increment the value of ans ; Print the value of ans ; Driver Code
function countOperations ( S ) { let n = S . length ; let ans = 0 ; let cnt = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( S [ i ] == ' ' ) { cnt ++ ; } else { if ( cnt > 0 ) { cnt -- ; ans ++ ; } } } document . write ( ans ) ; } let S = " " ; countOperations ( S ) ;
Check if summation of two words is equal to target word | Function to check weather summation of two words equal to target word ; Store the length of each string ; Reverse the strings A , B and C ; Stores the remainder ; Iterate in the range [ 0 , max ( L , max ( M , N ) ) ] ; Stores the integer at ith position from the right in the sum of A and B ; If i is less than L ; If i is less than M ; Update rem and curr ; If i is less than N and curr is not equal to C [ i ] - ' a ' , return " No " ; If rem is greater than 0 , return " No " ; Otherwise , return " Yes " ; Given Input ; Function Call
function isSumEqual ( A , B , C ) { let L = A . length ; let M = B . length ; let N = A . length ; A . split ( " " ) . reverse ( ) . join ( " " ) ; B . split ( " " ) . reverse ( ) . join ( " " ) ; C . split ( " " ) . reverse ( ) . join ( " " ) ; let rem = 0 ; for ( let i = 0 ; i < Math . max ( L , Math . max ( M , N ) ) ; i ++ ) { let curr = rem ; if ( i < L ) curr += A [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( i < M ) curr += B [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; rem = Math . floor ( curr / 10 ) ; curr %= 10 ; if ( i < N && curr != C [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) { return " " ; } } if ( rem ) return " " ; else return " " ; } let A = " " , B = " " , C = " " ; document . write ( isSumEqual ( A , B , C ) ) ;
Minimum K such that every substring of length at least K contains a character c | Set | Function to find minimum value of K such that there exist atleast one K amazing character ; Stores the answer ; Update the string S ; Iterate over the characters in range [ a , z ] ; Stores the last index where c appears ; Stores the maximum possible length ; Update string S ; Iterate over characters of string S ; If S [ i ] is equal to c ; Stores the distance between positions of two same c ; Update max_len ; Update the value of prev ; Update the value of ans ; Return the answer ; Given Input ; Function Call
function MinimumLengthSubstring ( S , N ) { let ans = N ; S = " " + S + " " ; S = S . split ( " " ) for ( let c = ' ' . charCodeAt ( 0 ) ; c <= ' ' . charCodeAt ( 0 ) ; ++ c ) { let prev = 0 ; let max_len = 0 ; S [ 0 ] = String . fromCharCode ( c ) ; S [ N + 1 ] = String . fromCharCode ( c ) ; for ( let i = 1 ; i <= N + 1 ; ++ i ) { if ( S [ i ] == String . fromCharCode ( c ) ) { let len = i - prev ; max_len = Math . max ( max_len , len ) ; prev = i ; } } ans = Math . min ( ans , max_len ) ; } return ans ; } let S = " " ; let N = S . length ; document . write ( MinimumLengthSubstring ( S , N ) ) ;
Minimize hamming distance in Binary String by setting only one K size substring bits | Function to find minimum Hamming Distance after atmost one operation ; Store the size of the string ; Store the prefix sum of 1 s ; Create Prefix Sum array ; Initialize cnt as number of ones in string S ; Store the required result ; Traverse the string , S ; Store the number of 1 s in the substring S [ i , i + K - 1 ] ; Update the answer ; Return the result ; Given Input ; Function Call
function minimumHammingDistance ( S , K ) { let n = S . length ; let pref = new Array ( n ) ; pref [ 0 ] = S [ 0 ] - ' ' ; for ( let i = 1 ; i < n ; i ++ ) pref [ i ] = pref [ i - 1 ] + ( S [ i ] - ' ' ) ; let cnt = pref [ n - 1 ] ; let ans = cnt ; for ( let i = 0 ; i < n - K ; i ++ ) { let value = pref [ i + K - 1 ] - ( i - 1 >= 0 ? pref [ i - 1 ] : 0 ) ; ans = Math . min ( ans , cnt - value + ( K - value ) ) ; } return ans ; } let s = " " ; let K = 2 ; document . write ( minimumHammingDistance ( s , K ) ) ;
Check if a permutation of S2 can be obtained by adding or removing characters from S1 | Function to check if the given number is prime or not ; If the number is less than 2 ; If the number is 2 ; If N is a multiple of 2 ; Otherwise , check for the odds values ; Function to check if S1 can be a permutation of S2 by adding or removing characters from S1 ; Initialize a frequency array ; Decrement the frequency for occurrence in s1 ; Increment the frequency for occurence in s2 ; If frequency of current char is same in s1 and s2 ; Check the frequency for the current char is not prime ; Print the result ; Driver Code
function isPrime ( n ) { if ( n <= 1 ) return false ; else if ( n == 2 ) return true ; else if ( n % 2 == 0 ) return false ; for ( let i = 3 ; i <= Math . floor ( Math . sqrt ( n ) ) ; i += 2 ) { if ( n % i == 0 ) return false ; } return true ; } function checkPermutation ( s1 , s2 ) { let freq = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) freq [ i ] = 0 ; for ( let ch = 0 ; ch < s1 . length ; ch ++ ) { freq [ s1 [ ch ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -- ; } for ( let ch = 0 ; ch < s2 . length ; ch ++ ) { freq [ s2 [ ch ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } let isAllChangesPrime = true ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) { continue ; } else if ( ! isPrime ( Math . abs ( freq [ i ] ) ) ) { isAllChangesPrime = false ; break ; } } if ( isAllChangesPrime ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let S1 = " " ; let S2 = " " ; checkPermutation ( S1 , S2 ) ;
Check if String formed by first and last X characters of a String is a Palindrome | Function to check whether the first x characters of both string str and reversed string str are same or not ; Length of the string str ; Traverse over the string while first and last x characters are not equal ; If the current and n - k - 1 from last character are not equal ; Finally , print true ; Given Input ; Function Call
function isEqualSubstring ( str , x ) { let n = str . length ; let i = 0 ; while ( i < n && i < x ) { if ( str [ i ] !== str [ n - i - 1 ] ) { document . write ( " " ) ; return ; } i ++ ; } document . write ( " " ) ; } let str = " " ; let x = 3 ; isEqualSubstring ( str , x ) ;
Minimum number of replacement done of substring "01" with "110" to remove it completely | Function to find the minimum number of replacement of "01" with "110" s . t . S doesn 't contain substring "10" ; Stores the number of operations performed ; Stores the resultant count of substrings ; Traverse the string S from end ; If the current character is 0 ; If the current character is 1 ; Print the result ; Driver Code
function minimumOperations ( S , N ) { let ans = 0 ; let cntOne = 0 ; for ( let i = N - 1 ; i >= 0 ; i -- ) { if ( S [ i ] == ' ' ) { ans += cntOne ; cntOne *= 2 ; } else cntOne ++ ; } document . write ( ans ) ; } let S = " " ; let N = S . length ; minimumOperations ( S , N ) ;
Count different Bitwise OR values of equal length strings S1 and S2 by swapping exactly one pair of characters from the first string | Function to find the number of ways to obtain different Bitwise OR ; Stores the count of pairs t00 , t10 , t01 , t11 ; Traverse the characters of the string S1 and S2 ; Count the pair ( 0 , 0 ) ; Count the pair ( 1 , 0 ) ; Count the pair ( 1 , 1 ) ; Count the pair ( 0 , 1 ) ; Number of ways to calculate the different bitwise OR ; Print the result ; Driver Code
function differentBitwiseOR ( s1 , s2 ) { let n = s1 . length ; let t00 = 0 , t10 = 0 , t01 = 0 , t11 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s1 [ i ] == ' ' && s2 [ i ] == ' ' ) { t00 ++ ; } if ( s1 [ i ] == ' ' && s2 [ i ] == ' ' ) { t10 ++ ; } if ( s1 [ i ] == ' ' && s2 [ i ] == ' ' ) { t11 ++ ; } if ( s1 [ i ] == ' ' && s2 [ i ] == ' ' ) { t01 ++ ; } } let ans = t00 * t10 + t01 * t10 + t00 * t11 ; document . write ( ans ) ; } let S1 = " " ; let S2 = " " ; differentBitwiseOR ( S1 , S2 ) ;