codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of removals required such that no subsequence of length 2 occurs more than once | Function to remove the minimum count of characters from the string such that no subsequence of length 2 repeats ; Initialize the final string ; Stores if any character occurs in the final string or not ; Store the index of the last character added in the string ; Traverse the string ; Add all the unique characters ; Check if S [ 0 ] appears in the range [ pos + 1 , N - 1 ] ; If the characters are the same ; Print the resultant string ; Driver Code
function RemoveCharacters ( s ) { var ans = " " ; var c = new Array ( 26 ) ; var i ; for ( i = 0 ; i < 26 ; i ++ ) c [ i ] = 0 ; var pos = 0 ; for ( i = 0 ; i < s . length ; i ++ ) { if ( c [ s [ i ] . charCodeAt ( ) - 97 ] == 0 ) { c [ s [ i ] . charCodeAt ( ) - 97 ] = 1 ; pos = i ; ans += s [ i ] ; } } for ( i = pos + 1 ; i < s . length ; i ++ ) { if ( s [ i ] == s [ 0 ] ) { ans += s [ i ] ; break ; } } document . write ( ans ) ; } var S = " " ; RemoveCharacters ( S ) ;
Check if a numeric string can be split into substrings having difference between consecutive numbers equal to K | Function to check if a numeric string can be split into substrings such that the difference between the consecutive substrings is K ; Stores the size of the string ; Iterate over the range [ 1 , N ] and try each possible starting number ; Convert the number to string ; Build up a sequence starting with the number ; Compare it with the original string s ; Prlet the result ; Driver Code
function isPossible ( s , K ) { let valid = false ; let firstx = - 1 ; let n = s . length ; for ( let i = 1 ; i <= n / 2 ; ++ i ) { let x = ( s . substr ( 0 , i ) ) ; firstx = x ; let test = x . toString ( ) ; while ( test . length < s . length ) { x -= K ; test += x . toString ( ) ; } if ( test == s ) { valid = true ; break ; } } document . write ( ( valid == true ) ? " " : " " ) ; } let S = " " ; let K = 2 ; isPossible ( S , K ) ;
Convert given Binary string S to all 1 s by changing all 0 s to 1 s in range [ i + 1 , i + K ] if S [ i ] is 1 | Function to check whether all 0 s in the string can be changed into 1 s ; Store the count of 0 s converted for the last occurrence of 1 ; Declere a stack ; Traverse the string , S ; If stack is empty ; There is no 1 that can change this 0 to 1 ; Push 1 into the stack ; The last 1 has reached its limit ; New 1 has been found which can now change at most K 0 s ; If flag is 1 , print " YES " else print " NO " ; Given Input ; Function call
function changeCharacters ( S , N , K ) { let flag = 1 ; let count = 0 ; let st = new Array ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( st . length == 0 ) { if ( S [ i ] == ' ' ) { flag = 0 ; break ; } count = 0 ; st . push ( S [ i ] ) ; } else { if ( S [ i ] == ' ' ) { count ++ ; if ( count == K ) { st . pop ( ) ; count = 0 ; } } else { count = 0 ; } } } if ( flag == 1 ) document . write ( " " ) ; else document . write ( " " ) ; } let S = " " ; let N = S . Length ; let K = 2 ; changeCharacters ( S , N , K ) ;
Check if all prefixes of a number is divisible by remaining count of digits | Function to check if all prefixes of a number is divisible by remaining count of digits or not ; Traverse and check divisibility for each updated number ; Update the original number ; Given Input ; Function Call
function prefixDivisble ( n ) { let i = 1 ; while ( n > 0 ) { if ( n % i != 0 ) return false ; n = parseInt ( n / 10 ) ; i ++ ; } return true ; } let n = 52248 ; if ( prefixDivisble ( n ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Minimize cost to make all characters of a Binary String equal to '1' by reversing or flipping characters of substrings | Function to calculate minimum cost to convert all the characters of S to '1' ; Stores the result ; Stores the number of groups that have 0 as characters ; Traverse the string S ; If current character is '0' ; If count is greater than 0 ; Set the count to 0 ; If the last few consecutive characters are '0' ; If string contains all characters as '1' ; Minimum Cost ; Given Input ; Function Call
function MinimumCost ( S , A , B ) { let count = 0 ; let group = 0 ; for ( let i = 0 ; i < S . length ; i ++ ) { if ( S [ i ] == ' ' ) { count += 1 ; } else { if ( count > 0 ) { group += 1 ; } count = 0 ; } } if ( count > 0 ) group += 1 ; if ( group == 0 ) { document . write ( 0 + " " ) ; } else { document . write ( Math . min ( A , B ) * ( group - 1 ) + B ) ; } } let A = 1 ; let B = 5 ; let S = " " ; MinimumCost ( S , A , B ) ;
Largest Prime Number possible from a subsequence of a Binary String | Function to check if a number is prime or not ; Return not prime ; If prime return true ; Function to find the largest prime number possible from a subsequence ; Stores pairs of subsequences and their respective decimal value ; Stores the answer ; Traverse the string ; Stores the size of the vector ; Traverse the vector ; Extract the current pair ; Get the binary string from the pair ; Stores equivalent decimal values ; If the current character is '1' ; Add the character to the subsequence ; Update the value by left shifting the current value and adding 1 to it ; If s [ i ] = = '0' ; Add the character to the subsequence ; Update the value by left shifting the current value and adding 0 to it ; Store the subsequence in the vector ; Check if the decimal representation of current subsequence is prime or not ; If prime ; Update the answer with the largest one ; If no prime number could be obtained ; Input String
function isPrime ( x ) { if ( x <= 1 ) return false ; for ( let i = 2 ; i * i <= x ; i ++ ) { if ( i * i > x ) { break } if ( x % i == 0 ) return false ; } return true ; } function largestPrime ( s ) { let vec = [ [ " " , 0 ] ] ; let ans = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { let n = vec . length ; for ( let j = 0 ; j < n ; j ++ ) { let temp = vec [ j ] ; let str = temp [ 0 ] ; let val = temp [ 1 ] ; if ( s [ i ] == ' ' ) { temp [ 0 ] = str + ' ' ; temp [ 1 ] = ( ( val << 1 ) + 1 ) ; } else { temp [ 0 ] = str + ' ' ; temp [ 1 ] = ( ( val << 1 ) + 0 ) ; } vec . push ( temp ) ; let check = temp [ 1 ] ; if ( isPrime ( check ) ) { ans = Math . max ( ans , check ) ; break } } } if ( ans == 0 ) document . write ( - 1 + " " ) ; else document . write ( ans + " " ) ; } let s = " " ; largestPrime ( s ) ;
Count strings having sum of ASCII values of characters equal to a Prime or Armstrong Number | Function to check if a number is prime number ; Define a flag variable ; Check for factors of num ; If factor is found , set flag to True and break out of loop ; Check if flag is True ; Function to calculate order of the number x ; Function to check whether the given number is Armstrong number or not ; If the condition satisfies ; Function to count Armstrong valued strings ; Stores the count of Armstrong valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it an Armstrong number ; Function to count prime valued strings ; Store the count of prime valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it is a Prime Number ; Driver code ; Function Call
function isPrime ( num ) { let flag = false ; if ( num > 1 ) { for ( let i = 2 ; i < num ; i ++ ) { if ( ( num % i ) == 0 ) { flag = true ; break ; } } } if ( flag ) return false ; else return true ; } function order ( x ) { let n = 0 ; while ( x != 0 ) { n = n + 1 ; x = x / 10 ; } return n ; } function isArmstrong ( x ) { let n = order ( x ) ; let temp = x ; let sum1 = 0 ; while ( temp != 0 ) { let r = temp % 10 ; sum1 = sum1 + Math . pow ( r , n ) ; temp = temp / 10 ; } return ( sum1 == x ) ; } function count_armstrong ( li ) { let c = 0 ; for ( let ele of li ) { let val = 0 ; for ( let che of ele ) val += che . charCodeAt ( 0 ) ; if ( isArmstrong ( val ) ) c += 1 ; } return c ; } function count_prime ( li ) { let c = 0 ; for ( let ele of li ) { let val = 0 ; for ( let che of ele ) val += che . charCodeAt ( 0 ) ; if ( isPrime ( val ) ) c += 1 ; } return c ; } let arr = [ " " , " " , " " , " " , " " , " " , " " ] ; document . write ( " " + count_armstrong ( arr ) + " " ) ; document . write ( " " + count_prime ( arr ) + " " ) ;
Minimize cost of flipping or swaps to make a Binary String balanced | Function to find the minimum cost to convert the given string into balanced string ; Stores count of 1 ' s ▁ and ▁ 0' s in the string ; Traverse the string ; Increment count1 ; Increment count 0 ; Stores absolute difference of counts of 0 ' s ▁ and ▁ 1' s ; If string consists of only 0 ' s ▁ and ▁ 1' s ; Print minimum cost ; Driver Code
function findMinimumCost ( s , N ) { let count_1 = 0 , count_0 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' ' ) count_1 ++ ; else count_0 ++ ; } let k = Math . abs ( count_0 - count_1 ) ; if ( count_1 == N count_0 == N ) document . write ( - 1 ) ; else document . write ( k / 2 ) ; } let S = " " ; let N = S . length ; findMinimumCost ( S , N ) ;
Minimum time required to complete all tasks with alteration of their order allowed | Function to find the minimum time required to complete the tasks if the order of tasks can be changed ; If there is no task , print 0 ; Store the maximum occurring character and its frequency ; Stores the frequency of each character ; Traverse the string S ; Increment the frequency of the current character ; Update maxfreq and maxchar ; Store the number of empty slots ; Traverse the hashmap , um ; Fill the empty slots ; Store the required result ; Print the result ; Driver Code
function findMinimumTime ( s , N , K ) { if ( N == 0 ) { document . write ( 0 ) ; return ; } let maxfreq = Number . MIN_SAFE_INTEGER ; let maxchar ; let um = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( um . has ( s [ i ] ) ) { um . set ( s [ i ] , um . get ( s [ i ] ) + 1 ) } else { um . set ( s [ i ] , 1 ) } if ( um . get ( S [ i ] ) > maxfreq ) { maxfreq = um . get ( S [ i ] ) ; maxchar = S [ i ] ; } } let emptySlots = ( maxfreq - 1 ) * K ; for ( let it of um ) { if ( it [ 0 ] == maxchar ) continue ; emptySlots -= Math . min ( it [ 1 ] , maxfreq - 1 ) ; } let ans = N + Math . max ( 0 , emptySlots ) ; document . write ( ans ) ; } let S = " " ; let K = 2 ; let N = S . length ; findMinimumTime ( S , N , K ) ;
Check if all characters of a string can be made equal by increments or decrements | Function to check if it is possible to make all characters of string S same or not ; Length of string ; Stores the sum of ASCII value ; Traverse the string S ; Update the weightOfString ; If the sum is divisible by N then print " Yes " ; Otherwise print " No " ; Driver Code
function canMakeEqual ( S ) { var N = S . length ; var weightOfString = 0 ; for ( var i = 0 ; i < N ; i ++ ) { weightOfString += S . charAt ( i ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 1 ; } if ( weightOfString % N == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } var S = " " ; canMakeEqual ( S ) ;
Modify a string by circularly shifting each character to the right by respective frequencies | Function to replace the characters by its frequency of character in it ; Stores frequencies of characters in the string S ; Traverse the string S ; Increment the frequency of each character by 1 ; Traverse the string S ; Find the frequency of the current character ; Update the character ; Print the resultant string ; Driver Code
function addFrequencyToCharacter ( Str ) { var frequency = Array . from ( { length : 26 } , ( _ , i ) => 0 ) ; var N = Str . length ; var S = Str . split ( ' ' ) ; 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 S = " " ; addFrequencyToCharacter ( S ) ;
Minimize length of a string by removing pairs of consecutive increasing or decreasing digits | Function to find the minimum length of the string possible after removing pairs of consecutive digits ; Initialize the stack st ; Traverse the string S ; If the stack is empty ; Otherwise ; Get the top character of the stack ; If cha and top are consecutive digits ; Otherwise , push the character ch ; Print the size of the stack ; Driver Code
function minLength ( S ) { var st = [ ] ; S . split ( ' ' ) . forEach ( ch => { if ( st . length == 0 ) st . push ( ch ) ; else { var top = st [ st . length - 1 ] ; if ( Math . abs ( ch - top ) == 1 ) st . pop ( ) ; else { st . push ( ch ) ; } } } ) ; return st . length ; } var S = " " ; document . write ( minLength ( S ) ) ;
Minimum time required to complete all tasks without altering their order | Function to find the minimum time required to complete tasks without changing their order ; Keeps track of the last time instant of each task ; Stores the required result ; Traverse the given string ; Check last time instant of task , if it exists before ; Increment the time if task is within the K units of time ; Update the time of the current task in the map ; Increment the time by 1 ; Print the result ; Driver Code
function findMinimumTime ( tasks , K ) { var map = new Map ( ) ; var curr_time = 0 ; tasks . split ( ' ' ) . forEach ( c => { if ( map . has ( c ) ) { if ( curr_time - map . get ( c ) <= K ) { curr_time += K - ( curr_time - map . get ( c ) ) + 1 ; } } map . set ( c , curr_time ) ; curr_time ++ ; } ) ; document . write ( curr_time ) ; } var S = " " ; var K = 2 ; findMinimumTime ( S , K ) ;
Check if a string is a subsequence of another string ( using Stacks ) | Function to check if target is a subsequence of string S ; Declare a stack ; Push the characters of target into the stack ; Traverse the string S in reverse ; If the stack is empty ; if S [ i ] is same as the top of the stack ; Pop the top of stack ; Stack s is empty ; Driver Code
function checkforSubsequence ( S , target ) { var s = [ ] ; for ( var i = 0 ; i < target . length ; i ++ ) { s . push ( target [ i ] ) ; } for ( var i = S . length - 1 ; i >= 0 ; i -- ) { if ( s . length == 0 ) { document . write ( " " ) ; return ; } if ( S [ i ] == s [ s . length - 1 ] ) { s . pop ( ) ; } } if ( s . length == 0 ) document . write ( " " ) ; else document . write ( " " ) ; } var S = " " ; var target = " " ; checkforSubsequence ( S , target ) ;
Program to construct a DFA to check if a given integer is unsigned or not | JavaScript program for the above approach ; Function to construct DFA as per the given conditions ; If at state 0 and a digit has occurred then set it to state 1 ; Similarly for all other states ; Function to build and connect the DFA states ; Connect all the states to the dead state ; Function call to make DFA as per the given conditions ; Function call to check whether an integer in the form of string is unsigned integer or not ; Build the DFA ; Stores the current state ; Traverse the string ; If at a certain state a digit occurs then change the current state according to the DFA ; Or + / - sign ; Or decimal occurred ; Or any other character ; Or e / E or exponent sign ; State 1 , 4 , 8 will give the final answer ; Driver Code
let digits = " " , sign = " " ; let dot = " " , ex = " " ; let dfa = new Array ( 11 ) ; for ( let i = 0 ; i < 11 ; i ++ ) { dfa [ i ] = new Array ( 5 ) ; } function makeDFA ( ) { dfa [ 0 ] [ 0 ] = 1 ; dfa [ 1 ] [ 0 ] = 1 ; dfa [ 1 ] [ 2 ] = 3 ; dfa [ 1 ] [ 3 ] = 2 ; dfa [ 1 ] [ 4 ] = 6 ; dfa [ 3 ] [ 0 ] = 4 ; dfa [ 4 ] [ 0 ] = 4 ; dfa [ 4 ] [ 3 ] = 5 ; dfa [ 4 ] [ 4 ] = 6 ; dfa [ 6 ] [ 0 ] = 8 ; dfa [ 6 ] [ 1 ] = 7 ; dfa [ 7 ] [ 0 ] = 8 ; dfa [ 8 ] [ 0 ] = 8 ; dfa [ 8 ] [ 3 ] = 9 ; } function buildDFA ( ) { for ( let i = 0 ; i < 11 ; i ++ ) for ( let j = 0 ; j < 5 ; j ++ ) dfa [ i ] [ j ] = 10 ; makeDFA ( ) ; } function checkDFA ( s ) { buildDFA ( ) ; let currentstate = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( digits . indexOf ( s [ i ] ) != - 1 ) currentstate = dfa [ currentstate ] [ 0 ] ; else if ( sign . indexOf ( s [ i ] ) != - 1 ) currentstate = dfa [ currentstate ] [ 1 ] ; else if ( dot . indexOf ( s [ i ] ) != - 1 ) currentstate = dfa [ currentstate ] [ 2 ] ; else if ( ex . indexOf ( s [ i ] ) != - 1 ) currentstate = dfa [ currentstate ] [ 4 ] ; else currentstate = dfa [ currentstate ] [ 3 ] ; } if ( currentstate == 1 currentstate == 4 currentstate == 8 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let S = " " ; checkDFA ( S ) ;
Convert a Mobile Numeric Keypad sequence to equivalent sentence | Function to convert mobile numeric keypad sequence into its equivalent string ; Store the mobile keypad mappings ; Traverse the string str ; If the current character is ' . ' , then continue to the next iteration ; Stores the number of continuous clicks ; Iterate a loop to find the count of same characters ; 2 , 3 , 4 , 5 , 6 and 8 keys will have maximum of 3 letters ; 7 and 9 keys will have maximum of 4 keys ; Handle the end condition ; Check if the current pressed key is 7 or 9 ; Else , the key pressed is either 2 , 3 , 4 , 5 , 6 or 8 ; Driver Code
function printSentence ( S ) { let nums = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let str = S . split ( " " ) ; let i = 0 ; while ( i < str . length ) { if ( str [ i ] == ' ' ) { i ++ ; continue ; } let count = 0 ; while ( i + 1 < str . length && str [ i ] == str [ i + 1 ] ) { if ( count == 2 && ( ( str [ i ] >= ' ' && str [ i ] <= ' ' ) || ( str [ i ] == ' ' ) ) ) break ; else if ( count == 3 && ( str [ i ] == ' ' str [ i ] == ' ' ) ) break ; count ++ ; i ++ ; if ( i == str . length ) break ; } if ( str [ i ] == ' ' str [ i ] == ' ' ) { document . write ( nums [ str [ i ] . charCodeAt ( 0 ) - 48 ] [ count % 4 ] ) ; } else { document . write ( nums [ str [ i ] . charCodeAt ( 0 ) - 48 ] [ count % 3 ] ) ; } i ++ ; } } let str = " " ; printSentence ( str ) ;
Lexicographically largest string with sum of characters equal to N | Function to construct the lexicographically largest string having sum of characters as N ; Stores the resulting string ; Iterate until N is at least 26 ; Append ' z ' to the string ans ; Decrement N by 26 ; Append character at index ( N + ' a ' ) ; Return the resultant string ; Driver Code
function getString ( N ) { let ans = " " while ( N >= 26 ) { ans += ' ' N -= 26 } ans += String . fromCharCode ( N + ' ' . charCodeAt ( 0 ) - 1 ) return ans } let N = 30 document . write ( getString ( N ) )
Length of all prefixes that are also the suffixes of given string | Function to find the length of all prefixes of the given string that are also suffixes of the same string ; Stores the prefix string ; Traverse the string S ; Add the current character to the prefix string ; Store the suffix string ; Check if both the strings are equal or not ; Driver Code
function countSamePrefixSuffix ( s , n ) { var prefix = " " ; for ( let i = 0 ; i < n - 1 ; i ++ ) { prefix += s . charAt ( i ) ; var suffix = s . substring ( n - 1 - i , n ) ; if ( prefix == suffix ) { document . write ( prefix . length + " " ) ; } } } let S = " " ; let N = S . length ; countSamePrefixSuffix ( 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 newly created pairs by swapping the first characters of any pairs of strings ; Stores the count all possible pair of strings ; Push all the strings into the Unordered Map ; Generate all possible pairs of strings from the array arr [ ] ; Store the current pair of strings ; Swap the first character ; Check if both string are not present in map ; Print the result ; Driver code
function countStringPairs ( a , n ) { let ans = 0 ; let s = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! s . has ( a [ i ] ) ) s . set ( a [ i ] , 0 ) ; s . set ( a [ i ] , s . get ( a [ i ] ) + 1 ) ; } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { let p = ( a [ i ] ) . split ( " " ) ; let q = ( a [ j ] ) . split ( " " ) ; if ( p [ 0 ] != q [ 0 ] ) { let t = p [ 0 ] ; p [ 0 ] = q [ 0 ] ; q [ 0 ] = t ; if ( ! s . has ( p . join ( " " ) ) && ! s . has ( q . join ( " " ) ) ) { ans ++ ; } } } } document . write ( ans ) ; } let arr = [ " " , " " , " " ] ; let N = arr . length ; countStringPairs ( arr , N ) ;
Check if it is possible to obtain a Balanced Parenthesis by shifting brackets to either end at most K times | Function to check if a valid parenthesis can be obtained by moving characters to either end at most K number of times ; Base Case 1 ; Count of ' ( ' and ' ) ' ; Base Case 2 ; Store the count of moves required to make a valid parenthesis ; Traverse the string ; Increment cnt if opening bracket has occurred ; Otherwise , decrement cnt by 1 ; Decrement cnt by 1 ; If cnt is negative ; Update the cnt ; Increment the ans ; If ans is at most K , then print Yes . Otherwise print No ; Driver Code
function minimumMoves ( s , n , k ) { if ( n & 1 ) { document . write ( " " ) ; return ; } var countOpen = 0 ; var i ; for ( i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == " " ) countOpen ++ ; } var countClose = 0 ; for ( i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == " " ) countClose ++ ; } ; if ( countOpen != countClose ) { document . write ( " " ) ; return ; } var ans = 0 ; var cnt = 0 ; for ( i = 0 ; i < n ; ++ i ) { if ( s [ i ] == ' ' ) ++ cnt ; else { -- cnt ; if ( cnt < 0 ) { cnt = 0 ; ++ ans ; } } } if ( ans <= k ) document . write ( " " ) ; else document . write ( " " ) ; } var S = " " ; var K = 1 ; minimumMoves ( S , S . length , K ) ;
Generate a random Binary String of length N | Function to find a random number between 0 or 1 ; Generate the random number ; Return the generated number ; Function to generate a random binary string of length N ; Stores the empty string ; Iterate over the range [ 0 , N - 1 ] ; Store the random number ; Append it to the string ; Print the resulting string ; Driver Code
function findRandom ( ) { let num = ( 1 + parseInt ( ( Math . random ( ) * 100 ) ) ) % 2 ; return num ; } function generateBinaryString ( N ) { let S = " " ; for ( let i = 0 ; i < N ; i ++ ) { let x = findRandom ( ) ; S += ( x ) . toString ( ) ; } document . write ( S ) } let N = 7 ; generateBinaryString ( N ) ;
Partition a string into palindromic strings of at least length 2 with every character present in a single string | Function to check if a string can be split into palindromic strings of at least length 2 by including every character exactly once ; Stores the frequency of each character ; Store the frequency of characters with frequencies 1 and even respectively ; Traverse the string s ; Iterate over all the characters ; If the frequency is 1 ; If frequency is even ; Print the result ; Stores the number of characters with frequency equal to 1 that are not part of a palindromic string ; Iterate over all the characters ; If o becomes less than 0 , then break out of the loop ; If frequency of the current character is > 2 and is odd ; Update the value of o ; If a single character is still remaining ; Increment o by 1 ; Set a [ i ] to 1 ; Print the result ; Driver Code
function checkPalindrome ( s ) { let a = [ 0 ] let o = 0 , e = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) a [ s . charCodeAt ( i ) - 97 ] ++ ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( a [ i ] == 1 ) o ++ ; else if ( a [ i ] % 2 == 0 && a [ i ] != 0 ) e += ( a [ i ] / 2 ) ; } if ( e >= o ) document . write ( " " ) else { o = o - e ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( o <= 0 ) break ; if ( o > 0 && a [ i ] % 2 == 1 && a [ i ] > 2 ) { let k = o ; o = o - a [ i ] / 2 ; if ( o > 0 2 * k + 1 == a [ i ] ) { o ++ ; a [ i ] = 1 ; } } } if ( o <= 0 ) document . write ( " " ) else document . write ( " " ) } } let S = " " ; checkPalindrome ( S ) ;