codeparrot/xlcost-text-to-code huggingface dataset:
Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character | Counts the no of manipulations required ; Store the count of character ; Iterate though the first String and update count ; Iterate through the second string update char_count . If character is not found in char_count then increase count ; Driver code
function countManipulations ( s1 , s2 ) { let count = 0 ; let char_count = new Array ( 26 ) ; for ( let i = 0 ; i < char_count . length ; i ++ ) { char_count [ i ] = 0 ; } for ( let i = 0 ; i < s1 . length ; i ++ ) char_count [ s1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( let i = 0 ; i < s2 . length ; i ++ ) { char_count [ s2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -- ; } for ( let i = 0 ; i < 26 ; ++ i ) { if ( char_count [ i ] != 0 ) { count += Math . abs ( char_count [ i ] ) ; } } return count ; } let s1 = " " ; let s2 = " " ; document . write ( countManipulations ( s1 , s2 ) ) ;
Least number of manipulations needed to ensure two strings have identical characters | Javascript program to count least number of manipulations to have two strings set of same characters ; Return the count of manipulations required ; Count the number of different characters in both strings ; Check the difference in characters by comparing count arrays ; Driver Code
let MAX_CHAR = 26 ; function leastCount ( s1 , s2 , n ) { let count1 = new Array ( MAX_CHAR ) ; let count2 = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { count1 [ i ] = 0 ; count2 [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { count1 [ s1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] += 1 ; count2 [ s2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] += 1 ; } let res = 0 ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { if ( count1 [ i ] != 0 ) { res += Math . abs ( count1 [ i ] - count2 [ i ] ) ; } } return res ; } let s1 = " " ; let s2 = " " ; let len = s1 . length ; let res = leastCount ( s1 , s2 , len ) ; document . write ( res ) ;
Given two strings check which string makes a palindrome first | javascript program to check which string makes palindrome first . ; returns winner of two strings ; Count frequencies of characters in both given strings ; Check if there is a character that appears more than once in A and does not appear in B ; Driver Code
var MAX_CHAR = 26 ; function stringPalindrome ( A , B ) { var countA = Array . from ( { length : MAX_CHAR } , ( _ , i ) => 0 ) ; var countB = Array . from ( { length : MAX_CHAR } , ( _ , i ) => 0 ) ; var l1 = A . length ; var l2 = B . length ; for ( var i = 0 ; i < l1 ; i ++ ) countA [ A . charAt ( i ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( var i = 0 ; i < l2 ; i ++ ) countB [ B . charAt ( i ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( var i = 0 ; i < 26 ; i ++ ) if ( ( countA [ i ] > 1 && countB [ i ] == 0 ) ) return ' ' ; return ' ' ; } var a = " " ; var b = " " ; document . write ( stringPalindrome ( a , b ) ) ;
Print the longest common substring | function to find and print the longest common substring of X [ 0. . m - 1 ] and Y [ 0. . n - 1 ] ; Create a table to store lengths of longest common suffixes of substrings . Note that LCSuff [ i ] [ j ] contains length of longest common suffix of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] . The first row and first column entries have no logical meaning , they are used only for simplicity of program ; To store length of the longest common substring ; To store the index of the cell which contains the maximum value . This cell 's index helps in building  up the longest common substring from right to left. ; Following steps build LCSuff [ m + 1 ] [ n + 1 ] in bottom up fashion . ; if true , then no common substring exists ; allocate space for the longest common substring ; traverse up diagonally form the ( row , col ) cell until LCSuff [ row ] [ col ] != 0 ; resultStr = X [ row - 1 ] + resultStr ; or Y [ col - 1 ] ; move diagonally up to previous cell ; required longest common substring ; Driver program to test above function
function printLCSubStr ( X , Y , m , n ) { let LCSuff = new Array ( m + 1 ) ; let len = 0 ; let row = 0 , col = 0 ; for ( let i = 0 ; i <= m ; i ++ ) { LCSuff [ i ] = Array ( n + 1 ) ; for ( let j = 0 ; j <= n ; j ++ ) { LCSuff [ i ] [ j ] = 0 ; if ( i == 0 j == 0 ) LCSuff [ i ] [ j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) { LCSuff [ i ] [ j ] = LCSuff [ i - 1 ] [ j - 1 ] + 1 ; if ( len < LCSuff [ i ] [ j ] ) { len = LCSuff [ i ] [ j ] ; row = i ; col = j ; } } else LCSuff [ i ] [ j ] = 0 ; } } if ( len == 0 ) { document . write ( " " ) ; return ; } let resultStr = " " ; while ( LCSuff [ row ] [ col ] != 0 ) { -- len ; row -- ; col -- ; } document . write ( resultStr ) ; } let X = " " ; let Y = " " ; let m = X . length ; let n = Y . length ; printLCSubStr ( X , Y , m , n ) ;
Convert all substrings of length ' k ' from base ' b ' to decimal | Simple Javascript program to convert all substrings from decimal to given base . ; Saving substring in sub ; Evaluating decimal for current substring and printing it . ; Driver code
function substringConversions ( str , k , b ) { for ( let i = 0 ; i + k <= str . length ; i ++ ) { let sub = str . substring ( i , i + k ) ; let sum = 0 , counter = 0 ; for ( let j = sub . length - 1 ; j >= 0 ; j -- ) { sum = ( sum + ( ( sub [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) * Math . pow ( b , counter ) ) ) ; counter ++ ; } document . write ( sum + " " ) ; } } let str = " " ; let b = 3 , k = 3 ; substringConversions ( str , b , k ) ;
Find numbers of balancing positions in string | Javascript program to find number of balancing points in string ; function to return number of balancing points ; hash array for storing hash of string initialized by 0 being global ; process string initially for rightVisited ; check for balancing points ; for every position inc left hash & dec rightVisited ; check whether both hash have same character or not ; Either both leftVisited [ j ] and rightVisited [ j ] should have none zero value or both should have zero value ; if both have same character increment count ; driver program
var MAX_CHAR = 256 ; function countBalance ( str ) { var leftVisited = Array ( MAX_CHAR ) . fill ( 0 ) ; var rightVisited = Array ( MAX_CHAR ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) rightVisited [ str [ i ] . charCodeAt ( 0 ) ] ++ ; var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) { leftVisited [ str [ i ] . charCodeAt ( 0 ) ] ++ ; rightVisited [ str [ i ] . charCodeAt ( 0 ) ] -- ; var j ; for ( j = 0 ; j < MAX_CHAR ; j ++ ) { if ( ( leftVisited [ j ] == 0 && rightVisited [ j ] != 0 ) || ( leftVisited [ j ] != 0 && rightVisited [ j ] == 0 ) ) break ; } if ( j == MAX_CHAR ) res ++ ; } return res ; } var str = " " ; document . write ( countBalance ( str ) ) ;
Min flips of continuous characters to make all characters same in a string | To find min number of flips in binary string ; If last character is not equal to str [ i ] increase res ; To return min flips ; Driver program to check findFlips ( )
function findFlips ( str , n ) { var last = ' ' ; var res = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( last != str . charAt ( i ) ) res ++ ; last = str . charAt ( i ) ; } return parseInt ( res / 2 ) ; } var str = " " ; var n = str . length ; document . write ( findFlips ( str , n ) ) ;
Maximum length substring having all same characters after k changes | method to find the maximum length of substring having character ch ; traverse the whole string ; if character is not same as ch increase count ; While count > k traverse the string again until count becomes less than k and decrease the count when characters are not same ; length of substring will be rightIndex - leftIndex + 1. Compare this with the maximum length and return maximum length ; method which returns maximum length of substring ; Driver Method
function findLen ( A , n , k , ch ) { let maxlen = 1 ; let cnt = 0 ; let l = 0 , r = 0 ; while ( r < n ) { if ( A [ r ] != ch ) ++ cnt ; while ( cnt > k ) { if ( A [ l ] != ch ) -- cnt ; ++ l ; } maxlen = Math . max ( maxlen , r - l + 1 ) ; ++ r ; } return maxlen ; } function answer ( A , n , k ) { let maxlen = 1 ; for ( let i = 0 ; i < 26 ; ++ i ) { maxlen = Math . max ( maxlen , findLen ( A , n , k , String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ) ) ; maxlen = Math . max ( maxlen , findLen ( A , n , k , String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ) ) ; } return maxlen ; } let n = 5 , k = 2 ; let A = " " ; document . write ( " " + answer ( A , n , k ) + " " ) ; n = 6 ; k = 4 ; let B = " " ; document . write ( " " + answer ( B , n , k ) ) ;
Quick way to check if all the characters of a string are same | Function to check is all the characters in string are or not ; Insert characters in the set ; If all characters are same Size of set will always be 1 ; Driver Code
function allCharactersSame ( s ) { let s1 = new Set ( ) ; for ( let i = 0 ; i < s . length ; i ++ ) { s1 . add ( s [ i ] ) ; } if ( s1 . size == 1 ) document . write ( " " ) ; else document . write ( " " ) ; } let str = " " ; allCharactersSame ( str ) ;
Find the character in first string that is present at minimum index in second string | method to find the minimum index character ; to store the index of character having minimum index ; lengths of the two strings ; traverse ' patt ' ; for each character of ' patt ' traverse ' str ' ; if patt . charAt ( i ) is found in ' str ' , check if it has the minimum index or not . If yes , then update ' minIndex ' and break ; print the minimum index character ; if no character of ' patt ' is present in ' str ' ; Driver Method
function printMinIndexChar ( str , patt ) { let minIndex = Number . MAX_VALUE ; let m = str . length ; let n = patt . length ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < m ; j ++ ) { if ( patt [ i ] == str [ j ] && j < minIndex ) { minIndex = j ; break ; } } } if ( minIndex != Number . MAX_VALUE ) document . write ( " " + str [ minIndex ] ) ; else document . write ( " " ) ; } let str = " " ; let patt = " " ; printMinIndexChar ( str , patt ) ;
Find the character in first string that is present at minimum index in second string | Method to find the minimum index character ; map to store the first index of each character of ' str ' ; To store the index of character having minimum index ; Lengths of the two strings ; Store the first index of each character of ' str ' ; Traverse the string ' patt ' ; If patt [ i ] is found in ' um ' , check if it has the minimum index or not accordingly update ' minIndex ' ; Print the minimum index character ; If no character of ' patt ' is present in ' str ' ; Driver Code
function printMinIndexChar ( str , patt ) { let hm = new Map ( ) ; let minIndex = Number . MAX_VALUE ; let m = str . length ; let n = patt . length ; for ( let i = 0 ; i < m ; i ++ ) if ( ! hm . has ( str [ i ] ) ) hm . set ( str [ i ] , i ) ; for ( let i = 0 ; i < n ; i ++ ) if ( hm . has ( patt [ i ] ) && hm . get ( patt [ i ] ) < minIndex ) minIndex = hm . get ( patt [ i ] ) ; if ( minIndex != Number . MAX_VALUE ) document . write ( " " + str [ minIndex ] ) ; else document . write ( " " ) ; } let str = " " ; let patt = " " ; printMinIndexChar ( str , patt ) ;
Check if both halves of the string have same set of characters | Javascript program to check if it two half of string contain same Character set or not ; function to check both halves for equality ; Counter array initialized with 0 ; Length of the string ; traverse till the middle element is reached ; First half ; Second half ; Checking if values are different set flag to 1 ; String to be checked
let MAX_CHAR = 26 ; function checkCorrectOrNot ( s ) { let count1 = new Array ( MAX_CHAR ) ; let count2 = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { count1 [ i ] = 0 ; count2 [ i ] = 0 ; } let n = s . length ; if ( n == 1 ) return true ; for ( let i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { count1 [ s [ i ] - ' ' ] ++ ; count2 [ s [ j ] - ' ' ] ++ ; } for ( let i = 0 ; i < MAX_CHAR ; i ++ ) if ( count1 [ i ] != count2 [ i ] ) return false ; return true ; } let s = " " ; if ( checkCorrectOrNot ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Given a number as a string , find the number of contiguous subsequences which recursively add up to 9 | Set 2 | Javascript program to count substrings with recursive sum equal to 9 ; to store no . of previous encountered modular sums ; no . of modular sum ( == 0 ) encountered till now = 1 ; if number is 0 increase ; no . of continuous_zero by 1 ; else continuous_zero is 0 ; increase d value of this mod_sum ; subtract no . of cases where there are only zeroes in substring ; driver program to test above function
function count9s ( number ) { let n = number . length ; let d = new Array ( 9 ) ; for ( let i = 0 ; i < d . length ; i ++ ) { d [ i ] = 0 ; } d [ 0 ] = 1 ; let result = 0 ; let mod_sum = 0 , continuous_zero = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( number [ i ] - ' ' ) == 0 ) { continuous_zero ++ ; } else { continuous_zero = 0 ; } mod_sum += ( number [ i ] - ' ' ) ; mod_sum %= 9 ; result += d [ mod_sum ] ; d [ mod_sum ] ++ ; result -= continuous_zero ; } return result ; } document . write ( count9s ( " " ) + " " ) ; document . write ( count9s ( " " ) + " " ) ; document . write ( count9s ( " " ) + " " ) ;
Extract maximum numeric value from a given string | Set 1 ( General approach ) | Utility method to find maximum string ; If both having equal lengths ; Reach first unmatched character / value ; Return string with maximum value ; If different lengths return string with maximum length ; Method to extract the maximum value ; Start traversing the string ; Ignore leading zeroes ; Store numeric value into a string ; Update maximum string ; To handle the case if there is only 0 numeric value ; Return maximum string ; Driver method
function maximumNum ( curr_num , res ) { let len1 = curr_num . length ; let len2 = res . length ; if ( len1 == len2 ) { let i = 0 ; while ( curr_num [ i ] == res [ i ] ) i ++ ; if ( curr_num [ i ] < res [ i ] ) return res ; else return curr_num ; } return len1 < len2 ? res : curr_num ; } function extractMaximum ( str ) { let n = str . length ; let curr_num = " " ; let res = " " ; for ( let i = 0 ; i < n ; i ++ ) { while ( i < n && str [ i ] == ' ' ) i ++ ; while ( i < n && ! isNaN ( String ( str [ i ] ) * 1 ) ) { curr_num = curr_num + str [ i ] ; i ++ ; } if ( i == n ) break ; if ( curr_num . length > 0 ) i -- ; res = maximumNum ( curr_num , res ) ; curr_num = " " ; } if ( curr_num . length == 0 && res . length == 0 ) res = res + ' ' ; return maximumNum ( curr_num , res ) ; } let str = " " ; document . write ( extractMaximum ( str ) ) ;
To check divisibility of any large number by 999 | function to check divisibility ; Append required 0 s at the beginning . ; add digits in group of three in gSum ; group saves 3 - digit group ; calculate result till 3 digit sum ; Driver Code
function isDivisible999 ( num ) { let n = num . length ; if ( n == 0 && num [ 0 ] == ' ' ) return true ; if ( n % 3 == 1 ) num = " " + num ; if ( n % 3 == 2 ) num = " " + num ; let gSum = 0 ; for ( let i = 0 ; i < n ; i += 3 ) { group = 0 ; group += ( num . charCodeAt ( i ) - 48 ) * 100 ; group += ( num . charCodeAt ( i + 1 ) - 48 ) * 10 ; group += ( num . charCodeAt ( i + 2 ) - 48 ) ; gSum += group ; } if ( gSum > 1000 ) { num = String ( gSum ) ; n = strlen ( num ) ; gSum = isDivisible999 ( num ) ; } return ( gSum == 999 ) ; } let num = " " ; if ( isDivisible999 ( num ) ) document . write ( " " ) ; else document . write ( " " ) ;
Rearrange a string in sorted order followed by the integer sum | Javascript program for above implementation ; Method to return string in lexicographic order followed by integers sum ; Traverse the string ; Count occurrence of uppercase alphabets ; Store sum of integers ; Traverse for all characters A to Z ; Append the current character in the string no . of times it occurs in the given string ; Append the sum of integers ; return resultant string ; Driver method
let MAX_CHAR = 26 ; function arrangeString ( str ) { let char_count = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { char_count [ i ] = 0 ; } let sum = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] >= " " && str [ i ] <= " " ) char_count [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; else sum = sum + ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; } let res = " " ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { let ch = String . fromCharCode ( ' ' . charCodeAt ( 0 ) + i ) ; while ( char_count [ i ] -- != 0 ) res = res + ch ; } if ( sum > 0 ) res = res + sum ; return res ; } let str = " " ; document . write ( arrangeString ( str ) ) ;
URLify a given string ( Replace spaces is % 20 ) | Instantiate the string ; Trim the given string ; Replace All space ( unicode is \\ s ) to % 20 ; Display the result
var str = " " ; str = str . trim ( ) ; str = str . replaceAll ( " " , " " ) ; document . write ( str ) ;
Program to print all substrings of a given string | Function to print all sub strings ; Pick starting point ; Pick ending point ; Print characters from current starting point to current ending point . ; Driver program to test above function
function subString ( str , n ) { for ( let len = 1 ; len <= n ; len ++ ) { for ( let i = 0 ; i <= n - len ; i ++ ) { let j = i + len - 1 ; for ( let k = i ; k <= j ; k ++ ) { document . write ( str [ k ] ) ; } document . write ( " " ) ; } } } let str = [ ' ' , ' ' , ' ' ] ; subString ( str , str . length ) ;
Reverse a string preserving space positions | Function to reverse the string and preserve the space position ; Mark spaces in result ; Traverse input string from beginning and put characters in result from end ; Ignore spaces in input string ; ignore spaces in result . ; driver function
function reverses ( str ) { let inputArray = str . split ( " " ) ; let result = new Array ( inputArray . length ) ; for ( let i = 0 ; i < inputArray . length ; i ++ ) { if ( inputArray [ i ] == ' ' ) { result [ i ] = ' ' ; } } let j = result . length - 1 ; for ( let i = 0 ; i < inputArray . length ; i ++ ) { if ( inputArray [ i ] != ' ' ) { if ( result [ j ] == ' ' ) { j -- ; } result [ j ] = inputArray [ i ] ; j -- ; } } document . write ( ( result ) . join ( " " ) ) ; } reverses ( " " ) ;
Find uncommon characters of the two strings | size of the hash table ; function to find the uncommon characters of the two strings ; mark presence of each character as 0 in the hash table ' present [ ] ' ; for each character of str1 , mark its presence as 1 in ' present [ ] ' ; for each character of str2 ; if a character of str2 is also present in str1 , then mark its presence as - 1 ; else mark its presence as 2 ; print all the uncommon characters ; Driver program to test above
var MAX_CHAR = 26 ; function findAndPrintUncommonChars ( str1 , str2 ) { var present = Array ( MAX_CHAR ) ; for ( var i = 0 ; i < MAX_CHAR ; i ++ ) present [ i ] = 0 ; var l1 = str1 . length ; var l2 = str2 . length ; for ( var i = 0 ; i < l1 ; i ++ ) present [ str1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] = 1 ; for ( var i = 0 ; i < l2 ; i ++ ) { if ( present [ str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 1 || present [ str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == - 1 ) present [ str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] = - 1 ; else present [ str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] = 2 ; } for ( var i = 0 ; i < MAX_CHAR ; i ++ ) if ( present [ i ] == 1 present [ i ] == 2 ) document . write ( ( String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ) + " " ) ; } var str1 = " " ; var str2 = " " ; findAndPrintUncommonChars ( str1 , str2 ) ;
Length Of Last Word in a String | js program for implementation of simple approach to find length of last word ; String a is ' final ' -- can not be modified So , create a copy and trim the spaces from both sides ; Driver code
function lengthOfLastWord ( a ) { let len = 0 ; x = a . trim ( ) ; for ( let i = 0 ; i < x . length ; i ++ ) { if ( x [ i ] == ' ' ) { len = 0 ; } else { len ++ ; } } return len ; } input = " " ; document . write ( " " + lengthOfLastWord ( input ) ) ;
Length Of Last Word in a String | Javascript program for implementation of efficient approach to find length of last word ; Split by space and converting String to list and ; Driver code
function length ( str ) { var lis = str . split ( " " ) return lis [ lis . length - 1 ] . length ; } var str = " " document . write ( " " + length ( str ) ) ;
Program to count vowels in a string ( Iterative and Recursive ) | Function to check the Vowel ; Returns count of vowels in str ; Check for vowel ; string object ; Total numbers of Vowel
function isVowel ( ch ) { ch = ch . toUpperCase ( ) ; return ch == " " || ch == " " || ch == " " || ch == " " || ch == " " ; } function countVowels ( str ) { var count = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) if ( isVowel ( str [ i ] ) ) ++ count ; return count ; } var str = " " ; document . write ( countVowels ( str ) ) ; document . write ( " " ) ;
Toggle case of a string using Bitwise Operators | program to get toggle case of a string ; tOGGLE cASE = swaps CAPS to lower case and lower case to CAPS ; Bitwise EXOR with 32 ; Driver program
x = 32 ; function toggleCase ( a ) { for ( i = 0 ; i < a . length ; i ++ ) { a [ i ] = String . fromCharCode ( a [ i ] . charCodeAt ( 0 ) ^ 32 ) ; } return a . join ( " " ) ; ; } var str = " " ; document . write ( " " ) ; str = toggleCase ( str . split ( ' ' ) ) ; document . write ( str ) ; document . write ( " " ) ; str = toggleCase ( str . split ( ' ' ) ) ; document . write ( str ) ;
Determine if a string has all Unique Characters | Function program to illustrate String with unique characters ; Converting string to set ; If length of set is equal to len of string then it will have unique characters ; Driver Code
function uniqueCharacters ( str ) { var setstring = new Set ( str ) if ( setstring . size == str . length ) { return true } else { return false } } var input = " " if ( uniqueCharacters ( input ) ) { document . write ( " " + input + " " ) ; } else { document . write ( " " + input + " " ) }
Binary representation of next greater number with same number of 1 ' s ▁ and ▁ 0' s | Function to find the next greater number with same number of 1 ' s ▁ and ▁ 0' s ; Locate first ' i ' from end such that bnum [ i ] == '0' and bnum [ i + 1 ] == '1' swap these value and break ; ; If no swapping performed ; Since we want the smallest next value , shift all 1 ' s ▁ at ▁ the ▁ end ▁ in ▁ the ▁ binary ▁ ▁ substring ▁ starting ▁ from ▁ index ▁ ' i + 2 ' ; Special case while swapping if '0' occurs then break ; Required next greater number ; Driver code
function nextGreaterWithSameDigits ( bnum ) { let l = bnum . length ; let i ; for ( i = l - 2 ; i >= 1 ; i -- ) { if ( bnum [ i ] == ' ' && bnum [ i + 1 ] == ' ' ) { let ch = bnum [ i ] ; bnum [ i ] = bnum [ i + 1 ] ; bnum [ i + 1 ] = ch ; break ; } } if ( i == 0 ) document . write ( " " ) ; let j = i + 2 , k = l - 1 ; while ( j < k ) { if ( bnum [ j ] == ' ' && bnum [ k ] == ' ' ) { let ch = bnum [ j ] ; bnum [ j ] = bnum [ k ] ; bnum [ k ] = ch ; j ++ ; k -- ; } else if ( bnum [ i ] == ' ' ) break ; else j ++ ; } return ( bnum ) . join ( " " ) ; } let bnum = " " . split ( " " ) ; document . write ( " " + " " + nextGreaterWithSameDigits ( bnum ) ) ;
Generate all rotations of a given string | Print all the rotated string . ; Generate all rotations one by one and print ; Current index in str ; Current index in temp ; Copying the second part from the point of rotation . ; Copying the first part from the point of rotation . ; Driver code
function printRotatedString ( str ) { var len = str . length ; var sb ; for ( i = 0 ; i < len ; i ++ ) { sb = [ ] ; var j = i ; var k = 0 ; for ( k2 = j ; k2 < str . length ; k2 ++ ) { sb . push ( str . charAt ( j ) ) ; k ++ ; j ++ ; } j = 0 ; while ( j < i ) { sb . push ( str . charAt ( j ) ) ; j ++ ; k ++ ; } document . write ( sb . join ( " " ) + " " ) ; } } var str = " " ; printRotatedString ( str ) ;