codeparrot/xlcost-text-to-code huggingface dataset:
Longest Non | utility function to check whether a string is palindrome or not ; Check for palindrome . ; palindrome string ; function to find maximum length substring which is not palindrome ; to check whether all characters of the string are same or not ; All characters are same , we can 't  make a non-palindromic string. ; If string is palindrome , we can make it non - palindrome by removing any corner character ; Complete string is not a palindrome . ; Driver Program to test above function
function isPalindrome ( str ) { let n = str . length ; for ( let i = 0 ; i < n / 2 ; i ++ ) { if ( str [ i ] != str [ n - i - 1 ] ) return false ; } return true ; } function maxLengthNonPalinSubstring ( str ) { let n = str . length ; let ch = str [ 0 ] ; let i = 1 ; for ( i = 1 ; i < n ; i ++ ) { if ( str [ i ] != ch ) { break ; } } if ( i == n ) return 0 ; if ( isPalindrome ( str ) ) return n - 1 ; return n ; } let str = " " ; document . write ( " " + maxLengthNonPalinSubstring ( str ) ) ;
Move spaces to front of string in single traversal | Function to find spaces and move to beginning ; Traverse from end and swap spaces ; Driver code
function moveSpaceInFront ( str ) { let i = str . length - 1 ; for ( let j = i ; j >= 0 ; j -- ) if ( str [ j ] != ' ' ) { let c = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = c ; i -- ; } } let str = " " . split ( " " ) ; moveSpaceInFront ( str ) ; document . write ( ( str ) . join ( " " ) ) ;
Move spaces to front of string in single traversal | Function to find spaces and move to beginning ; Keep copying non - space characters ; Move spaces to be beginning ; Driver code
function moveSpaceInFront ( str ) { var i = str . length - 1 ; for ( var j = i ; j >= 0 ; j -- ) if ( str [ j ] !== " " ) str [ i -- ] = str [ j ] ; while ( i >= 0 ) str [ i -- ] = " " ; } var str = " " . split ( " " ) ; moveSpaceInFront ( str ) ; document . write ( str . join ( " " ) ) ;
Minimum number of Appends needed to make a string palindrome | Checking if the string is palindrome or not ; single character is always palindrome ; pointing to first character ; pointing to last character ; Recursive function to count number of appends ; Removing first character of string by incrementing base address pointer . ; Driver code
function isPalindrome ( str ) { let len = str . length ; if ( len == 1 ) return true ; let ptr1 = 0 ; let ptr2 = len - 1 ; while ( ptr2 >= ptr1 ) { if ( str [ ptr1 ] != str [ ptr2 ] ) return false ; ptr1 ++ ; ptr2 -- ; } return true ; } function noOfAppends ( s ) { if ( isPalindrome ( s . split ( " " ) ) ) return 0 ; s = s . substring ( 1 ) ; return 1 + noOfAppends ( s ) ; } let s = " " ; document . write ( noOfAppends ( s ) ) ;
Find Excel column number from column title | Returns resul when we pass title ; This process is similar to binary - to - decimal conversion ; Driver Code
function titleToNumber ( s ) { let result = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { result *= 26 ; result += s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 1 ; } return result ; } document . write ( titleToNumber ( " " ) ) ;
Check whether K | Javascript program to check if k - th bit of a given number is set or not using right shift operator . ; Driver Code
function isKthBitSet ( n , k ) { if ( ( ( n >> ( k - 1 ) ) & 1 ) > 0 ) document . write ( " " ) ; else document . write ( " " ) ; } let n = 5 , k = 1 ; isKthBitSet ( n , k ) ;
Reverse string without using any temporary variable | Function to reverse string and return revesed string ; Iterate loop upto start not equal to end ; XOR for swapping the variable ; Driver Code
function reversingString ( str , start , end ) { while ( start < end ) { str [ start ] = String . fromCharCode ( str [ start ] . charCodeAt ( 0 ) ^ str [ end ] . charCodeAt ( 0 ) ) ; str [ end ] = String . fromCharCode ( str [ end ] . charCodeAt ( 0 ) ^ str [ start ] . charCodeAt ( 0 ) ) ; str [ start ] = String . fromCharCode ( str [ start ] . charCodeAt ( 0 ) ^ str [ end ] . charCodeAt ( 0 ) ) ; ++ start ; -- end ; } return ( str ) . join ( " " ) ; } let s = " " ; document . write ( reversingString ( s . split ( " " ) , 0 , 12 ) ) ;
Check if a string follows a ^ nb ^ n pattern or not | Returns " Yes " str is of the form a ^ nb ^ n . ; check first half is ' a ' and other half is full of ' b ' ; Driver code ; Function call
function isAnBn ( str ) { let n = str . length ; if ( ( n & 1 ) != 0 ) return " " ; let i ; for ( i = 0 ; i < n / 2 ; i ++ ) if ( str [ i ] != ' ' str [ n - i - 1 ] != ' ' ) return " " ; return " " ; } let str = " " ; document . write ( isAnBn ( str ) ) ;
Count substrings with same first and last characters | Returns true if first and last characters of s are same . ; Starting point of substring ; Length of substring ; Check if current substring has same starting and ending characters . ; Driver function
function checkEquality ( s ) { return ( s . charAt ( 0 ) == s . charAt ( s . length - 1 ) ) ; } function countSubstringWithEqualEnds ( s ) { var result = 0 ; var n = s . length ; for ( var i = 0 ; i < n ; i ++ ) for ( var len = 1 ; len <= n - i ; len ++ ) if ( checkEquality ( s . substring ( i , i + len ) ) ) result ++ ; return result ; } var s = " " ; document . write ( countSubstringWithEqualEnds ( s ) ) ;
Count substrings with same first and last characters | Space efficient javascript program to count all substrings with same first and last characters . ; Iterating through all substrings in way so that we can find first and last character easily ; Driver code
function countSubstringWithEqualEnds ( s ) { var result = 0 ; var n = s . length ; for ( i = 0 ; i < n ; i ++ ) for ( j = i ; j < n ; j ++ ) if ( s . charAt ( i ) == s . charAt ( j ) ) result ++ ; return result ; } var s = " " ; document . write ( countSubstringWithEqualEnds ( s ) ) ;
Find the missing number in a string of numbers with no separator | Javascript program to find a missing number in a string of consecutive numbers without any separator . ; Gets the integer at position i with length m , returns it or - 1 , if none ; Find value at index i and length m . ; Returns value of missing number ; Try all lengths for first number ; Get value of first number with current length ; To store missing number of current length ; To indicate whether the sequence failed anywhere for current length . ; Find subsequent numbers with previous number as n ; If we haven 't yet found the missing number  for current length. Next number is n+2. Note  that we use Log10 as (n+2) may have more  length than n. ; If next value is ( n + 1 ) ; Not found or no missing number ; Driver code
let MAX_DIGITS = 6 ; function getValue ( str , i , m ) { if ( i + m > str . length ) { return - 1 ; } let value = 0 ; for ( let j = 0 ; j < m ; j ++ ) { let c = str [ i + j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( c < 0 c > 9 ) { return - 1 ; } value = value * 10 + c ; } return value ; } function findMissingNumber ( str ) { for ( let m = 1 ; m <= MAX_DIGITS ; ++ m ) { let n = getValue ( str , 0 , m ) ; if ( n == - 1 ) { break ; } let missingNo = - 1 ; let fail = false ; for ( let i = m ; i != str . length ; i += 1 + Math . floor ( Math . log10 ( n ) ) ) { if ( ( missingNo == - 1 ) && ( getValue ( str , i , Math . floor ( 1 + Math . log10 ( n + 2 ) ) ) == n + 2 ) ) { missingNo = n + 1 ; n += 2 ; } else if ( getValue ( str , i , Math . floor ( 1 + Math . log10 ( n + 1 ) ) ) == n + 1 ) { n ++ ; } else { fail = true ; break ; } } if ( ! fail ) { return missingNo ; } } return - 1 ; } document . write ( findMissingNumber ( " " ) ) ;
Maximum consecutive repeating character in string | function to find out the maximum repeating character in given string ; Find the maximum repeating character starting from str [ i ] ; Update result if required ; Driver code
function maxRepeating ( str ) { let len = str . length ; let count = 0 ; let res = str [ 0 ] ; for ( let i = 0 ; i < len ; i ++ ) { let cur_count = 1 ; for ( let j = i + 1 ; j < len ; j ++ ) { if ( str [ i ] != str [ j ] ) break ; cur_count ++ ; } if ( cur_count > count ) { count = cur_count ; res = str [ i ] ; } } return res ; } let str = " " ; document . write ( maxRepeating ( str ) ) ;
Sum of two large numbers | Function for finding sum of larger numbers ; Before proceeding further , make sure length of str2 is larger . ; Take an empty String for storing result ; Calculate length of both String ; Reverse both of Strings ; Do school mathematics , compute sum of current digits and carry ; Calculate carry for next step ; Add remaining digits of larger number ; Add remaining carry ; reverse resultant String ; Driver code
function findSum ( str1 , str2 ) { if ( str1 . length > str2 . length ) { let t = str1 ; str1 = str2 ; str2 = t ; } let str = " " ; let n1 = str1 . length , n2 = str2 . length ; str1 = str1 . split ( " " ) . reverse ( ) . join ( " " ) ; str2 = str2 . split ( " " ) . reverse ( ) . join ( " " ) ; let carry = 0 ; for ( let i = 0 ; i < n1 ; i ++ ) { let sum = ( ( str1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) + ( str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) + carry ) ; str += String . fromCharCode ( sum % 10 + ' ' . charCodeAt ( 0 ) ) ; carry = Math . floor ( sum / 10 ) ; } for ( let i = n1 ; i < n2 ; i ++ ) { let sum = ( ( str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) + carry ) ; str += String . fromCharCode ( sum % 10 + ' ' . charCodeAt ( 0 ) ) ; carry = Math . floor ( sum / 10 ) ; } if ( carry > 0 ) str += String . fromCharCode ( carry + ' ' . charCodeAt ( 0 ) ) ; str = str . split ( " " ) . reverse ( ) . join ( " " ) ; return str ; } let str1 = " " ; let str2 = " " ; document . write ( findSum ( str1 , str2 ) )
Palindrome pair in an array of words ( or strings ) | Utility function to check if a string is a palindrome ; Compare each character from starting with its corresponding character from last ; Function to check if a palindrome pair exists ; Consider each pair one by one ; Concatenate both strings ; Check if the concatenated string is palindrome ; Check if the concatenated string is palindrome ; Driver code
function isPalindrome ( str ) { let len = str . length ; for ( let i = 0 ; i < len / 2 ; i ++ ) if ( str [ i ] != str [ len - i - 1 ] ) return false ; return true ; } function checkPalindromePair ( vect ) { for ( let i = 0 ; i < vect . length - 1 ; i ++ ) { for ( let j = i + 1 ; j < vect . length ; j ++ ) { let check_str = " " ; check_str = check_str + vect [ i ] + vect [ j ] ; if ( isPalindrome ( check_str ) ) return true ; check_str = vect [ j ] + vect [ i ] ; if ( isPalindrome ( check_str ) ) return true ; } } return false ; } let vect = [ " " , " " , " " , " " , " " , " " ] if ( checkPalindromePair ( vect ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Print consecutive characters together in a line | Javascript program to print consecutive characters together in a line . ; Driver code
function print ( str1 ) { let str = str1 . split ( " " ) ; document . write ( str [ 0 ] ) ; for ( let i = 1 ; i < str . length ; i ++ ) { if ( ( str [ i ] . charCodeAt ( 0 ) == str [ i - 1 ] . charCodeAt ( 0 ) + 1 ) || ( str [ i ] . charCodeAt ( 0 ) == str [ i - 1 ] . charCodeAt ( 0 ) - 1 ) ) { document . write ( str [ i ] ) ; } else { document . write ( " " + str [ i ] ) ; } } } let str = " " ; print ( str ) ;
Efficiently check if a string has all unique characters without using any additional data structure | Returns true if all characters of str are unique . Assumptions : ( 1 ) str contains only characters from ' a ' to ' z ' ( 2 ) integers are stored using 32 bits ; An integer to store presence / absence of 26 characters using its 32 bits . ; If bit corresponding to current character is already set ; set bit in checker ; Driver Code
function areChractersUnique ( str ) { let checker = 0 ; for ( let i = 0 ; i < str . length ; ++ i ) { let val = ( str [ i ] - ' ' ) ; if ( ( checker & ( 1 << val ) ) > 0 ) return false ; checker |= ( 1 << val ) ; } return true ; } var s = " " ; if ( areChractersUnique ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count of words whose i | Return the count of words . ; If word contain single letter , return 1. ; Checking for first letter . ; Traversing the string and multiplying for combinations . ; If all three letters are same . ; If two letter are distinct . ; If all three letter are distinct . ; Checking for last letter . ; Driven Program
function countWords ( str , len ) { let count = 1 ; if ( len == 1 ) return count ; if ( str [ 0 ] == str [ 1 ] ) count *= 1 ; else count *= 2 ; for ( let j = 1 ; j < len - 1 ; j ++ ) { if ( str [ j ] == str [ j - 1 ] && str [ j ] == str [ j + 1 ] ) count *= 1 ; else if ( str [ j ] == str [ j - 1 ] str [ j ] == str [ j + 1 ] str [ j - 1 ] == str [ j + 1 ] ) count *= 2 ; else count *= 3 ; } if ( str [ len - 1 ] == str [ len - 2 ] ) count *= 1 ; else count *= 2 ; return count ; } let str = " " ; let len = str . length ; document . write ( countWords ( str , len ) ) ;
Maximum and minimum sums from two numbers with digit replacements | Find new value of x after replacing digit " from " to " to " ; Required digit found , replace it ; Returns maximum and minimum possible sums of x1 and x2 if digit replacements are allowed . ; We always get minimum sum if we replace 6 with 5. ; We always get maximum sum if we replace 5 with 6. ; Driver code
function replaceDig ( x , from , to ) { var result = 0 ; var multiply = 1 ; while ( x > 0 ) { var reminder = x % 10 ; if ( reminder == from ) result = result + to * multiply ; else result = result + reminder * multiply ; multiply *= 10 ; x = parseInt ( x / 10 ) ; } return result ; } function calculateMinMaxSum ( x1 , x2 ) { var minSum = replaceDig ( x1 , 6 , 5 ) + replaceDig ( x2 , 6 , 5 ) ; var maxSum = replaceDig ( x1 , 5 , 6 ) + replaceDig ( x2 , 5 , 6 ) ; document . write ( " " + minSum ) ; document . write ( " " + maxSum ) ; } var x1 = 5466 , x2 = 4555 ; calculateMinMaxSum ( x1 , x2 ) ;
Queries on substring palindrome formation | Query type 1 : update string position i with character x . ; Print " Yes " if range [ L . . R ] can form palindrome , else print " No " . ; Find the frequency of each character in S [ L ... R ] . ; Checking if more than one character have frequency greater than 1. ; Driven code
function qType1 ( l , x , str1 ) { str1 [ l - 1 ] = x ; } function qType2 ( l , r , str1 ) { let freq = new Array ( 27 ) ; for ( let i = 0 ; i < 27 ; i ++ ) { freq [ i ] = 0 ; } for ( let i = l - 1 ; i <= r - 1 ; i ++ ) { freq [ str1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } let count = 0 ; for ( let j = 0 ; j < 26 ; j ++ ) { if ( freq [ j ] % 2 != 0 ) { count ++ ; } } if ( count <= 1 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let str = " " . split ( " " ) ; let n = str . length ; qType1 ( 4 , ' ' , str ) ; qType2 ( 1 , 4 , str ) ; qType2 ( 2 , 3 , str ) ; qType1 ( 10 , ' ' , str ) ; qType2 ( 10 , 11 , str ) ;
Queries on substring palindrome formation | Javascript program to Queries on substring palindrome formation . ; Return the frequency of the character in the i - th prefix . ; Updating the BIT ; Query to update the character in the string . ; Adding - 1 at L position ; Updating the character ; Adding + 1 at R position ; Query to find if rearrangement of character in range L ... R can form palindrome ; Checking on the first character of the string S . ; Checking if frequency of character is even or odd . ; Creating the Binary Index Tree of all aphabet ; Driver code
let max = 1000 ; function getFrequency ( tree , idx , i ) { let sum = 0 ; while ( idx > 0 ) { sum += tree [ idx ] [ i ] ; idx -= ( idx & - idx ) ; } return sum ; } function update ( tree , idx , val , i ) { while ( idx <= max ) { tree [ idx ] [ i ] += val ; idx += ( idx & - idx ) ; } } function qType1 ( tree , l , x , str ) { update ( tree , l , - 1 , str [ l - 1 ] . charCodeAt ( 0 ) - 97 + 1 ) ; str [ l - 1 ] = x ; update ( tree , l , 1 , str [ l - 1 ] . charCodeAt ( 0 ) - 97 + 1 ) ; } function qType2 ( tree , l , r , str ) { let count = 0 ; for ( let i = 1 ; i <= 26 ; i ++ ) { if ( l == 1 ) { if ( getFrequency ( tree , r , i ) % 2 == 1 ) count ++ ; } else { if ( ( getFrequency ( tree , r , i ) - getFrequency ( tree , l - 1 , i ) ) % 2 == 1 ) count ++ ; } } if ( count <= 1 ) document . write ( " " ) ; else document . write ( " " ) ; } function buildBIT ( tree , str , n ) { for ( let i = 0 ; i < n ; i ++ ) update ( tree , i + 1 , 1 , str [ i ] . charCodeAt ( 0 ) - 97 + 1 ) ; } let str = " " . split ( " " ) ; let n = str . length ; let tree = new Array ( max ) ; for ( let i = 0 ; i < tree . length ; i ++ ) { tree [ i ] = new Array ( 27 ) ; for ( let j = 0 ; j < tree [ i ] . length ; j ++ ) { tree [ i ] [ j ] = 0 ; } } buildBIT ( tree , str , n ) ; qType1 ( tree , 4 , ' ' , str ) ; qType2 ( tree , 1 , 4 , str ) ; qType2 ( tree , 2 , 3 , str ) ; qType1 ( tree , 10 , ' ' , str ) ; qType2 ( tree , 10 , 11 , str ) ;
Partition a number into two divisible parts | Finds if it is possible to partition str into two parts such that first part is divisible by a and second part is divisible by b . ; Create an array of size len + 1 and initialize it with 0. Store remainders from left to right when divided by ' a ' ; Compute remainders from right to left when divided by ' b ' ; Find a point that can partition a number ; If split is not possible at this point ; We can split at i if one of the following two is true . a ) All characters after str [ i ] are 0 b ) String after str [ i ] is divisible by b , i . e . , str [ i + 1. . n - 1 ] is divisible by b . ; Driver code
function findDivision ( str , a , b ) { let len = str . length ; let lr = [ ] ; for ( let i = 0 ; i < len + 1 ; i ++ ) lr . push ( 0 ) ; lr [ 0 ] = ( str [ 0 ] - ' ' ) % a ; for ( let i = 1 ; i < len ; i ++ ) lr [ i ] = ( ( lr [ i - 1 ] * 10 ) % a + ( str . charCodeAt ( i ) ) ) % a ; let rl = [ ] ; for ( let i = 0 ; i < len + 1 ; i ++ ) rl . push ( 0 ) ; rl [ len - 1 ] = ( str . charCodeAt ( len - 1 ) ) % b ; let power10 = 10 ; for ( let i = len - 2 ; i >= 0 ; i -- ) { rl [ i ] = ( rl [ i + 1 ] + ( str . charCodeAt ( i ) ) * power10 ) % b ; power10 = ( power10 * 10 ) % b ; } for ( let i = 0 ; i < len - 1 ; i ++ ) { if ( lr [ i ] != 0 ) continue ; if ( rl [ i + 1 ] == 0 ) { document . write ( " " ) ; for ( let k = 0 ; k <= i ; k ++ ) document . write ( str [ k ] ) ; document . write ( " " ) ; for ( let k = i + 1 ; k < len ; k ++ ) document . write ( str [ k ] ) ; return ; } } document . write ( " " ) ; } let str = " " ; let a = 12 , b = 3 ; findDivision ( str , a , b ) ;
Efficient method for 2 's complement of a binary string | Method to find two 's complement ; Traverse the string to get first '1' from the last of string ; If there exists no '1' concat 1 at the starting of string ; Continue traversal after the position of first '1' ; Just flip the values ; return the modified string ; Driver method
function findTwoscomplement ( str ) { var n = str . length ; var i ; for ( i = n - 1 ; i >= 0 ; i -- ) if ( str . charAt ( i ) == ' ' ) break ; if ( i == - 1 ) return " " + str ; for ( k = i - 1 ; k >= 0 ; k -- ) { if ( str . charAt ( k ) == ' ' ) str = str . substring ( 0 , k ) + " " + str . substring ( k + 1 , str . length ) ; else str = str . substring ( 0 , k ) + " " + str . substring ( k + 1 , str . length ) ; } return str . toString ( ) ; } var str = " " ; document . write ( findTwoscomplement ( str ) ) ;
Check length of a string is equal to the number appended at its last | Function to find if given number is equal to length or not ; Traverse string from end and find the number stored at the end . x is used to store power of 10. ; Check if number is equal to string length except that number 's digits ; Driver code
function isequal ( str ) { let n = str . length ; let num = 0 , x = 1 , i = n - 1 ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( ' ' <= str [ i ] && str [ i ] <= ' ' ) { num = ( str [ i ] - ' ' ) * x + num ; x = x * 10 ; if ( num >= n ) return false ; } else break ; } return num == i + 1 ; } let str = " " ; if ( isequal ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if two strings are k | Function to check k anagram of two strings ; First Condition : If both the strings have different length , then they cannot form anagram ; Converting str1 to Character Array arr1 ; Converting str2 to Character Array arr2 ; Sort arr1 in increasing order ; Sort arr2 in increasing order ; Iterate till str1 . length ( ) ; Condition if arr1 [ i ] is not equal to arr2 [ i ] then add it to list ; Condition to check if strings for K - anagram or not ; Driver code ; Function Call
function kAnagrams ( str1 , str2 , k ) { let flag = 0 ; let list = [ ] ; if ( str1 . length != str2 . length ) { return false ; } let arr1 = str1 . split ( ' ' ) ; let arr2 = str2 . split ( ' ' ) ; arr1 . sort ( ) ; arr2 . sort ( ) ; for ( let i = 0 ; i < str1 . length ; i ++ ) { if ( arr1 [ i ] != arr2 [ i ] ) { list . push ( arr2 [ i ] ) ; } } if ( list . length <= k ) flag = 1 ; if ( flag == 1 ) return true ; else return false ; } let str1 = " " ; let str2 = " " ; let k = 2 ; kAnagrams ( str1 , str2 , k ) ; if ( kAnagrams ( str1 , str2 , k ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum number of characters to be removed to make a binary string alternate | Returns count of minimum characters to be removed to make s alternate . ; if two alternating characters of string are same ; result ++ ; then need to delete a character ; Driver code
function countToMake0lternate ( s ) { let result = 0 ; for ( let i = 0 ; i < ( s . length - 1 ) ; i ++ ) if ( s [ i ] == s [ i + 1 ] ) return result ; } document . write ( countToMake0lternate ( " " ) + " " ) ; document . write ( countToMake0lternate ( " " ) + " " ) ; document . write ( countToMake0lternate ( " " ) + " " ) ;
Convert to a string that is repetition of a substring of k length | Returns true if str can be converted to a string with k repeated substrings after replacing k characters . ; Length of string must be a multiple of k ; Map to store strings of length k and their counts ; If string is already a repetition of k substrings , return true . ; If number of distinct substrings is not 2 , then not possible to replace a string . ; One of the two distinct must appear exactly once . Either the first entry appears once , or it appears n / k - 1 times to make other substring appear once . ; Driver code
function checkString ( str , k ) { let n = str . length ; if ( n % k != 0 ) return false ; let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i += k ) { if ( mp . has ( str . substring ( i , i + k ) ) ) mp . set ( str . substring ( i , i + k ) , mp . get ( str . substring ( i , i + k ) ) + 1 ) ; else mp . set ( str . substring ( i , i + k ) , 1 ) ; } if ( mp . size == 1 ) return true ; if ( mp . size != 2 ) { return false ; } for ( let [ key , value ] of mp . entries ( ) ) { if ( value == ( Math . floor ( n / k ) - 1 ) value == 1 ) return true ; } return false ; } if ( checkString ( " " , 2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Sort a string according to the order defined by another string | Javascript program to sort a string according to the order defined by a pattern string ; Sort str according to the order defined by pattern . ; Create a count array stor count of characters in str . ; Count number of occurrences of each character in str . ; Traverse the pattern and print every characters same number of times as it appears in str . This loop takes O ( m + n ) time where m is length of pattern and n is length of str . ; Driver code
let MAX_CHAR = 26 ; function sortByPattern ( str , pat ) { let count = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { count [ i ] = 0 ; } for ( let i = 0 ; i < str . length ; i ++ ) { count [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } let index = 0 ; for ( let i = 0 ; i < pat . length ; i ++ ) { for ( let j = 0 ; j < count [ pat [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; j ++ ) { str [ index ++ ] = pat [ i ] ; } } } let pat = " " . split ( " " ) ; let str = " " . split ( " " ) ; sortByPattern ( str , pat ) ; document . write ( ( str ) . join ( " " ) ) ;