codeparrot/xlcost-text-to-code huggingface dataset:
Minimize a binary string by repeatedly removing even length substrings of same characters | Recursive function to print stack elements from bottom to top without changing their order ; If stack is empty ; Pop top element of the stack ; Recursively call the function PrintStack ; Print the stack element from the bottom ; Push the same element onto the stack to preserve the order ; Function to minimize binary string by removing substrings consisting of same character ; Declare a stack of characters ; Push the first character of the string into the stack ; Traverse the string s ; If Stack is empty ; Push current character into the stack ; Check if the current character is same as the top of the stack ; If true , pop the top of the stack ; Otherwise , push the current element ; Print stack from bottom to top ; Driver Code
function PrintStack ( s ) { if ( s . length == 0 ) return ; let x = s [ s . length - 1 ] ; s . pop ( ) ; PrintStack ( s ) ; document . write ( x ) ; s . push ( x ) ; } function minString ( s ) { let Stack = [ ] ; Stack . push ( s [ 0 ] ) ; for ( let i = 1 ; i < s . length ; i ++ ) { if ( Stack . length == 0 ) { Stack . push ( s [ i ] ) ; } else { if ( Stack [ Stack . length - 1 ] == s [ i ] ) { Stack . pop ( ) ; } else { Stack . push ( s [ i ] ) ; } } } PrintStack ( Stack ) ; } let str = " " ; minString ( str ) ;
Probability of collision between two trucks | Function to calculate total number of accidents ; String size ; Function to calculate count of all possible collision ; Stores the count of collisions ; Total number of truck in lane b ; Count total number of collisions while traversing the String a ; Function to calculate the probability of collisions ; Evaluate total outcome that is all the possible accident ; Evaluate favourable outcome i . e . , count of collision of trucks ; Print desired probability ; Driver Code ; Function Call
function count_of_accident ( a , b ) { let n = a . length , m = b . length ; if ( n > m ) return ( m * ( m + 1 ) ) / 2 ; else return ( n * ( n + 1 ) ) / 2 + ( m - n ) * n ; } function count_of_collision ( a , b ) { let n = a . length , m = b . length ; let answer = 0 ; let count_of_truck_in_lane_b = 0 ; for ( let i = 0 ; i < m ; i ++ ) if ( b [ i ] == ' ' ) count_of_truck_in_lane_b ++ ; for ( let i = 0 ; i < n && i < m ; i ++ ) { if ( a [ i ] == ' ' ) answer += count_of_truck_in_lane_b ; if ( b [ i ] == ' ' ) count_of_truck_in_lane_b -- ; } return answer ; } function findProbability ( a , b ) { let total_outcome = count_of_accident ( a , b ) ; let favourable_outcome = count_of_collision ( a , b ) ; document . write ( favourable_outcome / total_outcome ) ; } let S = " " , T = " " ; findProbability ( S , T ) ;
Count alphanumeric palindromes of length N | Function to calculate ( x ^ y ) mod p ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Return the final result ; Given N ; Base Case ; Check whether n is even or odd ; Function Call ; Function Call
function power ( x , y , p ) { let res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } let N = 3 ; let flag , k , m = 0 ; if ( ( N == 1 ) || ( N == 2 ) ) document . write ( 62 ) ; else m = 1000000000 + 7 ; if ( N % 2 == 0 ) { k = N / 2 ; flag = 1 ; } else { k = ( N - 1 ) / 2 ; flag = 0 ; } if ( flag != 0 ) { let a = power ( 62 , k , m ) ; document . write ( a ) ; } else { let a = power ( 62 , ( k + 1 ) , m ) ; document . write ( a ) ; }
Length of the longest substring with every character appearing even number of times | Function to find length of the longest substring with each element occurring even number of times ; Initialize unordered_map ; Stores the length of the longest required substring ; Traverse the string ; Stores the value of the digit present at current index ; Bitwise XOR of the mask with 1 left - shifted by val ; Check if the value of mask is already present in ind or not ; Update the final answer ; Otherwise ; Return the answer ; Given string ; Length of the given string ; Function Call
function lenOfLongestReqSubstr ( s , N ) { var ind = new Map ( ) ; var mask = 0 ; ind [ 0 ] = - 1 ; var ans = 0 ; for ( var i = 0 ; i < N ; i ++ ) { var val = s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; mask ^= ( 1 << val ) ; if ( ind . has ( mask ) ) { ans = Math . max ( ans , i - ind . get ( mask ) ) ; } else ind . set ( mask , i ) ; } return ans ; } var s = " " ; var N = s . length ; document . write ( lenOfLongestReqSubstr ( s , N ) ) ;
Count distinct strings possible by replacing each character by its Morse code | Function to count unique array elements by replacing each character by its Morse code ; Stores Morse code of all lowercase characters ; Stores distinct elements of string by replacing each character by Morse code ; Stores length of arr [ ] array ; Traverse the array ; Stores the Morse code of arr [ i ] ; Stores length of current string ; Update temp ; Insert temp into st ; Return count of elements in the set ; Driver code
function uniqueMorseRep ( arr ) { var morseCode = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; var st = new Set ( ) ; var N = arr . length ; for ( var i = 0 ; i < N ; i ++ ) { var temp = " " ; var M = arr [ i ] . length ; for ( var j = 0 ; j < M ; j ++ ) { temp += morseCode [ arr [ i ] [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; } st . add ( temp ) ; } return st . size ; } var arr = [ " " , " " , " " , " " ] ; document . write ( uniqueMorseRep ( arr ) ) ;
Maximum repeating character for every index in given String | Function to print the maximum repeating character at each index of the String ; Stores frequency of each distinct character ; Stores frequency of maximum repeating character ; Stores the character having maximum frequency ; Traverse the String ; Stores current character ; Update the frequency of str [ i ] ; If frequency of current character exceeds max ; Update max ; Update charMax ; Print the required output ; Driver Code ; Stores length of str
function findFreq ( str , N ) { let freq = new Array ( 256 ) . fill ( 0 ) ; let max = 0 ; let charMax = ' ' ; for ( let i = 0 ; i < N ; i ++ ) { let ch = str [ i ] . charCodeAt ( 0 ) ; freq [ ch ] ++ ; if ( freq [ ch ] >= max ) { max = freq [ ch ] ; charMax = ch ; } document . write ( String . fromCharCode ( charMax ) + " " + max + " " ) ; } } let str = " " ; let N = str . length ; findFreq ( str , N ) ;
Minimize cost to convert given string to a palindrome | Function to find the minimum cost to convert the string into a palindrome ; Length of the string ; If iointer is in the second half ; Reverse the string ; Pointing to initial position ; Find the farthest index needed to change on left side ; Find the farthest index needed to change on right side ; Changing the variable to make string palindrome ; min distance to travel to make string palindrome ; Total cost ; Return the minimum cost ; Given string S ; Given pointer P ; Function Call
function findMinCost ( str , pos ) { var n = str . length ; if ( pos >= n / 2 ) { str . split ( ' ' ) . reverse ( ) . fill ( ' ' ) ; pos = n - pos - 1 ; } var left , right ; left = right = pos ; for ( var i = pos ; i >= 0 ; -- i ) { if ( str [ i ] != str [ n - i - 1 ] ) { left = i ; } } for ( var i = pos ; i < n / 2 ; ++ i ) { if ( str [ i ] != str [ n - i - 1 ] ) { right = i ; } } var ans = 0 ; for ( var i = left ; i <= right ; ++ i ) { if ( str [ i ] != str [ n - i - 1 ] ) ans += 1 ; } var dis = Math . min ( ( 2 * ( pos - left ) + ( right - pos ) ) , ( 2 * ( right - pos ) + ( pos - left ) ) ) ; ans = ans + dis ; return ans ; } var S = " " ; var P = 3 ; document . write ( findMinCost ( S , P ) ) ;
Minimize replacements by previous or next alphabet required to make all characters of a string the same | Function to find the minimum count of operations to make all characters of the string same ; Set min to some large value ; Find minimum operations for each character ; Initialize cnt ; Add the value to cnt ; Update minValue ; Return minValue ; Given string str ; Function Call
function minCost ( s , n ) { var minValue = 100000000 ; for ( var i = 0 ; i <= 25 ; i ++ ) { var cnt = 0 ; for ( var j = 0 ; j < n ; j ++ ) { cnt += Math . min ( Math . abs ( i - ( s [ j ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) , 26 - Math . abs ( i - ( s [ j ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) ) ; } minValue = Math . min ( minValue , cnt ) ; } return minValue ; } var str = " " ; var N = str . length ; document . write ( minCost ( str , N ) ) ;
Longest palindromic string possible by concatenating strings from a given array | Javascript program for the above approach ; Stores the distinct Strings from the given array ; Insert the Strings into set ; Stores the left and right subStrings of the given String ; Stores the middle subString ; Traverse the array of Strings ; Reverse the current String ; Checking if the String is itself a palindrome or not ; Check if the reverse of the String is present or not ; Append to the left subString ; Append to the right subString ; Erase both the Strings from the set ; Print the left subString ; Print the middle subString ; Print the right subString ; Driver Code ; Function Call
function reverse ( input ) { let a = input . split ( " " ) ; a . reverse ( ) ; return a . join ( " " ) ; } function max_len ( s , N , M ) { let set_str = new Set ( ) ; for ( let i = 0 ; i < N ; i ++ ) { set_str . add ( s [ i ] ) ; } let left_ans = [ ] ; let right_ans = [ ] ; let mid = " " ; for ( let i = 0 ; i < N ; i ++ ) { let t = s [ i ] ; t = reverse ( t ) ; if ( t == s [ i ] ) { mid = t ; } else if ( set_str . has ( t ) ) { left_ans . push ( s [ i ] ) ; right_ans . push ( t ) ; set_str . delete ( s [ i ] ) ; set_str . delete ( t ) ; } } for ( let x = 0 ; x < left_ans . length ; x ++ ) { document . write ( left_ans [ x ] ) ; } document . write ( mid ) ; ( right_ans ) . reverse ( ) ; for ( let x = 0 ; x < right_ans . length ; x ++ ) { document . write ( right_ans [ x ] ) ; } } let N = 4 , M = 3 ; let s = [ " " , " " , " " , " " ] ; max_len ( s , N , M ) ;
Minimize steps defined by a string required to reach the destination from a given source | Function to find the minimum length of string required to reach from source to destination ; Size of the string ; Stores the index of the four directions E , W , N , S ; If destination reached ; Iterate over the string ; Move east ; Change x1 according to direction E ; Move west ; Change x1 according to direction W ; Move north ; Change y1 according to direction N ; Move south ; Change y1 according to direction S ; Store the max of all positions ; Print the minimum length of string required ; Otherwise , it is impossible ; Given string ; Given source and destination ; Function Call
function minimum_length ( x1 , y1 , x2 , y2 , str ) { var n = str . length ; var pos1 , pos2 , pos3 , pos4 ; pos1 = - 1 ; pos2 = - 1 ; pos3 = - 1 ; pos4 = - 1 ; if ( x1 == x2 && y1 == y2 ) { document . write ( 0 + " " ) ; } else { for ( var i = 0 ; i < n ; i ++ ) { if ( x2 > x1 ) { if ( str [ i ] == ' ' ) { x1 = x1 + 1 ; if ( x1 == x2 ) { pos1 = i ; } } } if ( x2 < x1 ) { if ( str [ i ] == ' ' ) { x1 = x1 - 1 ; if ( x1 == x2 ) { pos2 = i ; } } } if ( y2 > y1 ) { if ( str [ i ] == ' ' ) { y1 = y1 + 1 ; if ( y1 == y2 ) { pos3 = i ; } } } if ( y2 < y1 ) { if ( str [ i ] == ' ' ) { y1 = y1 - 1 ; if ( y1 == y2 ) { pos4 = i ; } } } } var z ; z = Math . max ( Math . max ( Math . max ( pos1 , pos2 ) , pos3 ) , pos4 ) ; if ( x1 == x2 && y1 == y2 ) { document . write ( z + 1 + " " ) ; } else { document . write ( " " ) ; ; } } } var str = " " ; var x1 = 0 , x2 = 1 , y1 = 0 , y2 = 1 ; minimum_length ( x1 , y1 , x2 , y2 , str ) ;
Remove all duplicate adjacent characters from a string using Stack | Function to remove adjacent duplicate elements ; Store the string without duplicate elements ; Store the index of str ; Traverse the string str ; Checks if stack is empty or top of the stack is not equal to current character ; If top element of the stack is equal to the current character ; If stack is empty ; If stack is not Empty ; Driver Code
function ShortenString ( str1 ) { var st = [ ] ; var i = 0 ; while ( i < str1 . length ) { if ( st . length == 0 str1 [ i ] != st [ st . length - 1 ] ) { st . push ( str1 [ i ] ) ; i ++ ; } else { st . pop ( ) ; i ++ ; } } if ( st . length == 0 ) { return ( " " ) ; } else { var short_string = " " ; while ( st . length != 0 ) { short_string = st [ st . length - 1 ] + short_string ; st . pop ( ) ; } return ( short_string ) ; } } var str1 = " " ; document . write ( ShortenString ( str1 ) ) ;
Count ways to place all the characters of two given strings alternately | Function to get the factorial of N ; Function to get the total number of distinct ways ; Length of str1 ; Length of str2 ; If both strings have equal length ; If both strings do not have equal length ; Driver code
function fact ( n ) { var res = 1 ; for ( i = 1 ; i <= n ; i ++ ) { res = res * i ; } return res ; } function distinctWays ( str1 , str2 ) { var n = str1 . length ; var m = str2 . length ; if ( n == m ) { return 2 * fact ( n ) * fact ( m ) ; } return fact ( n ) * fact ( m ) ; } var str1 = " " ; var str2 = " " ; document . write ( distinctWays ( str1 , str2 ) ) ;
Smallest string without any multiplication sign that represents the product of two given numbers | Function to find the string which evaluates to the product of A and B ; Stores the result ; 2 ^ log <= B && 2 ^ ( log + 1 ) > B ; Update res to res += A X 2 ^ log ; Update res to res += A X 2 ^ 0 ; Find the remainder ; If remainder is not equal to 0 ; Return the resultant string ; Function to find the minimum length representation of A * B ; Find representation of form A << k1 + A << k2 + ... + A << kn ; Find representation of form B << k1 + B << k2 + ... + B << kn ; Compare the length of the representations ; Product A X B ; Function Call
function len ( A , B ) { let res = " " ; let Log = 0 ; do { Log = Math . floor ( Math . log ( B ) / Math . log ( 2 ) ) ; if ( Log != 0 ) { res = res + String ( A ) + " " + String ( Log ) ; } else { res += A ; break ; } B = B - Math . pow ( 2 , Log ) ; if ( B != 0 ) { res += " " ; } else break ; } while ( Log != 0 ) ; return res ; } function minimumString ( A , B ) { let res1 = len ( A , B ) ; let res2 = len ( B , A ) ; if ( res1 . length > res2 . length ) { document . write ( res2 + " " ) ; } else { document . write ( res1 + " " ) ; } } let A = 6 ; let B = 10 ; minimumString ( A , B ) ;
Count substrings of a given string whose anagram is a palindrome | Function to print count of subStrings whose anagrams are palindromic ; Stores the answer ; Iterate over the String ; Set the current character ; Parity update ; Print the final count ; Driver Code ; Function Call
function countSubString ( s ) { let res = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { let x = 0 ; for ( let j = i ; j < s . length ; j ++ ) { let temp = 1 << s [ j ] - ' ' ; x ^= temp ; if ( ( x & ( x - 1 ) ) == 0 ) res ++ ; } } document . write ( res ) ; } let str = " " ; countSubString ( str ) ;
Count substrings of a given string whose anagram is a palindrome | Function to get the count of substrings whose anagrams are palindromic ; Store the answer ; Map to store the freq of masks ; Set frequency for mask 00. . .00 to 1 ; Store mask in x from 0 to i ; Update answer ; Update frequency ; Print the answer ; Driver Code ; Function Call
function countSubstring ( s ) { var answer = 0 ; var m = new Map ( ) ; m . set ( 0 , 1 ) ; var x = 0 ; for ( var j = 0 ; j < s . length ; j ++ ) { x ^= 1 << ( s [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; answer += m . has ( x ) ? m . get ( x ) : 0 ; for ( var i = 0 ; i < 26 ; ++ i ) { answer += m . has ( x ^ ( 1 << i ) ) ? m . get ( x ^ ( 1 << i ) ) : 0 ; } if ( m . has ( x ) ) m . set ( x , m . get ( x ) + 1 ) else m . set ( x , 1 ) } document . write ( answer ) ; } var str = " " ; countSubstring ( str ) ;
Sum of an array of large numbers | Function to print the result of the summation of numbers having K - digit ; Reverse the array to obtain the result ; Print every digit of the answer ; Function to calculate the total sum ; Stores the array of large numbers in integer format ; Convert each element from character to integer ; Stores the carry ; Stores the result of summation ; Initialize the sum ; Calculate sum ; Update the sum by adding existing carry ; Store the number of digits ; Increase count of digits ; If the number exceeds 9 , Store the unit digit in carry ; Store the rest of the sum ; Append digit by digit into result array ; Append result until carry is 0 ; Print the result ; Driver Code ; Given N array of large numbers
function printResult ( result ) { result . reverse ( ) ; let i = 0 ; while ( i < result . length ) { document . write ( result [ i ] ) ; i ++ ; } } function sumOfLargeNumbers ( v , k , N ) { let x = [ ] ; for ( let i = 0 ; i < k ; i ++ ) x . push ( [ ] ) ; for ( let i = 0 ; i < k ; i ++ ) { for ( let j = 0 ; j < N ; j ++ ) { x [ i ] . push ( v [ i ] [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; } } let carry = 0 ; let result = [ ] ; for ( let i = N - 1 ; i >= 0 ; i -- ) { let sum = 0 ; for ( let j = 0 ; j < k ; j ++ ) sum += x [ j ] [ i ] ; sum += carry ; let temp = sum ; let count = 0 ; while ( temp > 9 ) { temp = temp % 10 ; count ++ ; } let l = Math . pow ( 10 , count ) ; if ( l != 1 ) carry = Math . floor ( sum / l ) ; sum = sum % 10 ; result . push ( sum ) ; } while ( carry != 0 ) { let a = carry % 10 ; result . push ( a ) ; carry = Math . floor ( carry / 10 ) ; } printResult ( result ) ; } let K = 10 ; let N = 5 ; let arr = [ " " , " " , " " , " " , " " ] ; sumOfLargeNumbers ( arr , N , K ) ;
Reverse words in a given string | Set 2 | Function to reverse the words of a given String ; Stack to store each word of the String ; Store the whole String in String stream ; Push each word of the String into the stack ; Print the String in reverse order of the words ; Driver Code
function printRev ( str ) { let st = [ ] ; let ss = str . split ( " " ) ; for ( let temp = 0 ; temp < ss . length ; temp ++ ) { st . push ( ss [ temp ] ) ; } while ( st . length != 0 ) { document . write ( st . pop ( ) + " " ) ; } } let str ; str = " " ; printRev ( str ) ;
Count of substrings of a Binary string containing only 1 s | Function to find the total number of substring having only ones ; Driver Code
function countOfSubstringWithOnlyOnes ( s ) { var res = 0 , count = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { count = s [ i ] == ' ' ? count + 1 : 0 ; res = ( res + count ) ; } return res ; } var s = " " ; document . write ( countOfSubstringWithOnlyOnes ( s ) ) ;
Count of Distinct strings possible by inserting K characters in the original string | JavaScript program for the above approach ; Function to calculate and return x ^ n in log ( n ) time using Binary Exponentiation ; Function to calculate the factorial of a number ; Function to calculate combination ; nCi = ( n ! ) / ( ( n - i ) ! * i ! ) ; Using Euler 's theorem of Modular  multiplicative inverse to find  the inverse of a number.  (1/a)%mod=a^(m?2)%mod ; Function to find the count of possible strings ; Number of ways to form all possible strings ; Number of ways to form strings that don 't contain the input  string as a subsequence ; Checking for all prefix length from 0 to | S | - 1. ; To calculate nCi ; Select the remaining characters 25 ^ ( N - i ) ; Add the answer for this prefix length to the final answer ; Answer is the difference of allWays and noWays ; Print the answer ; Driver code
var mod = 10007 ; function binExp ( base , power ) { var x = 1 ; while ( power != 0 ) { if ( power % 2 == 1 ) x = ( ( ( x % mod ) * ( base % mod ) ) % mod ) ; base = ( ( ( base % mod ) * ( base % mod ) ) % mod ) ; power = parseInt ( power / 2 ) ; } return x ; } function fact ( num ) { var result = 1 ; for ( var i = 1 ; i <= num ; ++ i ) { result = ( ( ( result % mod ) * ( i % mod ) ) % mod ) ; } return result ; } function calculate_nCi ( N , i ) { var nfact = fact ( N ) ; var ifact = fact ( i ) ; var dfact = fact ( N - i ) ; var inv_ifact = binExp ( ifact , mod - 2 ) ; var inv_dfact = binExp ( dfact , mod - 2 ) ; var denm = ( ( ( inv_ifact % mod ) * ( inv_dfact % mod ) ) % mod ) ; var answer = ( ( ( nfact % mod ) * ( denm % mod ) ) % mod ) ; return answer ; } function countSubstring ( N , s , k ) { var allWays = binExp ( 26 , N ) ; var noWays = 0 ; for ( var i = 0 ; i < s ; ++ i ) { var nCi = calculate_nCi ( N , i ) ; var remaining = binExp ( 25 , N - i ) ; var multiply = ( ( ( nCi % mod ) * ( remaining % mod ) ) % mod ) ; noWays = ( ( ( noWays % mod ) + ( multiply % mod ) ) % mod ) ; } var answer = ( ( ( allWays % mod ) - ( noWays % mod ) ) % mod ) ; if ( answer < 0 ) answer += mod ; document . write ( answer ) ; } var str = " " ; var k = 2 ; var s = str . length ; var N = s + k ; countSubstring ( N , s , k ) ;
Calculate Sum of ratio of special characters to length of substrings of the given string | Javascript program to implement the above approach ; Stores frequency of special characters in the array ; Stores prefix sum ; Function to check whether a character is special or not ; If current character is special ; Otherwise ; Function to find sum of ratio of count of special characters and length of substrings ; Calculate the prefix sum of special nodes ; Generate prefix sum array ; Calculate ratio for substring ; Driver Code ;
var N = 1000005 ; var prefix = Array ( N ) . fill ( 0 ) ; var sum = Array ( N ) . fill ( 0 ) ; function isSpecial ( c , special ) { var ans = false ; special . forEach ( i => { if ( i == c ) ans = true ; } ) ; return ans ; } function countRatio ( s , special ) { var n = s . length ; for ( var i = 0 ; i < n ; i ++ ) { prefix [ i ] = ( isSpecial ( s [ i ] , special ) ) ; if ( i > 0 ) prefix [ i ] += prefix [ i - 1 ] ; } for ( var i = 0 ; i < n ; i ++ ) { sum [ i ] = prefix [ i ] ; if ( i > 0 ) sum [ i ] += sum [ i - 1 ] ; } var ans = 0 ; for ( var i = 1 ; i <= n ; i ++ ) { var count = sum [ n - 1 ] - ( ( i > 1 ) ? sum [ i - 2 ] : 0 ) ; count -= ( ( i < n ) ? sum [ n - i - 1 ] : 0 ) ; ans += ( ( count ) / ( i ) ) ; } return ans ; } var s = " " ; var special = [ ' ' , ' ' ] ; var ans = countRatio ( s . split ( ' ' ) , special ) ; document . write ( ans . toFixed ( 6 ) ) ;
Minimum replacements in a string to make adjacent characters unequal | Function which counts the minimum number of required operations ; n stores the length of the string s ; ans will store the required ans ; i is the current index in the string ; Move j until characters s [ i ] & s [ j ] are equal or the end of the string is reached ; diff stores the length of the substring such that all the characters are equal in it ; We need atleast diff / 2 operations for this substring ; Driver code
function count_minimum ( s ) { var n = s . length ; var ans = 0 ; var i = 0 ; while ( i < n ) { var j = i ; while ( s [ j ] === s [ i ] && j < n ) { j ++ ; } var diff = j - i ; ans += parseInt ( diff / 2 ) ; i = j ; } document . write ( ans + " " ) ; } var str = " " ; count_minimum ( str ) ;