codeparrot/xlcost-text-to-code huggingface dataset:
Sub | Function that returns true if every lowercase character appears atmost once ; every character frequency must be not greater than one ; Function that returns the modified good string if possible ; If the length of the string is less than n ; Sub - strings of length 26 ; To store frequency of each character ; Get the frequency of each character in the current sub - string ; Check if we can get sub - string containing all the 26 characters ; Find which character is missing ; Fill with missing characters ; Find the next missing character ; Return the modified good string ; Driver code
function valid ( cnt ) { for ( var i = 0 ; i < 26 ; i ++ ) { if ( cnt [ i ] >= 2 ) return false ; } return true ; } function getGoodString ( ss , n ) { var s = ss . split ( " " ) ; if ( n < 26 ) return " " ; for ( var i = 25 ; i < n ; i ++ ) { var cnt = new Array ( 26 ) . fill ( 0 ) ; for ( var j = i ; j >= i - 25 ; j -- ) { cnt [ s [ j ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } if ( valid ( cnt ) ) { var cur = 0 ; while ( cnt [ cur ] > 0 ) cur ++ ; for ( var j = i - 25 ; j <= i ; j ++ ) { if ( s [ j ] === " " ) { s [ j ] = String . fromCharCode ( cur + " " . charCodeAt ( 0 ) ) ; cur ++ ; while ( cnt [ cur ] > 0 ) cur ++ ; } } return s . join ( " " ) ; } } return " " ; } var s = " " ; var n = s . length ; document . write ( getGoodString ( s , n ) ) ;
Modify the string by swapping continuous vowels or consonants | Function to check if a character is a vowel ; Function to swap two consecutively repeated vowels or consonants ; Traverse through the length of the string ; Check if the two consecutive characters are vowels or consonants ; swap the two characters ; Driver code
function isVowel ( c ) { c = c . toLowerCase ( ) ; if ( c === " " c === " " c === " " c === " " c === " " ) { return true ; } return false ; } function swapRepeated ( str ) { for ( var i = 0 ; i < str . length - 1 ; i ++ ) { if ( ( isVowel ( str [ i ] ) && isVowel ( str [ i + 1 ] ) ) || ( ! isVowel ( str [ i ] ) && ! isVowel ( str [ i + 1 ] ) ) ) { var c = str [ i ] ; str [ i ] = str [ i + 1 ] ; str [ i + 1 ] = c ; } } return str . join ( " " ) ; } var str = " " ; document . write ( swapRepeated ( str . split ( " " ) ) ) ;
Find the lexicographically largest palindromic Subsequence of a String | Function to find the largest palindromic subsequence ; Find the largest character ; Append all occurrences of largest character to the resultant string ; Driver Code
function largestPalinSub ( s ) { let res = " " ; let mx = s [ 0 ] ; for ( let i = 1 ; i < s . length ; i ++ ) mx = String . fromCharCode ( Math . max ( mx . charCodeAt ( ) , s [ i ] . charCodeAt ( ) ) ) ; for ( let i = 0 ; i < s . length ; i ++ ) if ( s [ i ] == mx ) res += s [ i ] ; return res ; } let s = " " ; document . write ( largestPalinSub ( s ) ) ;
Generate lexicographically smallest string of 0 , 1 and 2 with adjacent swaps allowed | Function to print the required string ; count number of 1 s ; To check if the all the 1 s have been used or not ; Print all the 1 s if any 2 is encountered ; If str [ i ] = 0 or str [ i ] = 2 ; If 1 s are not printed yet ; Driver code
function printString ( str , n ) { let ones = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( str [ i ] == ' ' ) ones ++ ; let used = false ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' && ! used ) { used = true ; for ( let j = 0 ; j < ones ; j ++ ) document . write ( " " ) ; } if ( str [ i ] != ' ' ) document . write ( str [ i ] ) ; } if ( ! used ) for ( let j = 0 ; j < ones ; j ++ ) document . write ( " " ) ; } let str = " " ; let n = str . length ; printString ( str . split ( ' ' ) , n ) ;
K length words that can be formed from given characters without repetition | Function to return the required count ; To store the count of distinct characters in str ; Traverse str character by character ; If current character is appearing for the first time in str ; Increment the distinct character count ; Update the appearance of the current character ; Since P ( n , r ) = n ! / ( n - r ) ! ; Return the answer ; Driver code
function findPermutation ( str , k ) { var has = new Array ( 26 ) ; for ( var i = 0 ; i < 26 ; i ++ ) has [ i ] = false ; var cnt = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( ! has [ str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ) { cnt ++ ; has [ str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] = true ; } } var ans = 1 ; for ( var i = 2 ; i <= cnt ; i ++ ) ans *= i ; for ( var i = cnt - k ; i > 1 ; i -- ) ans /= i ; return ans ; } var str = " " ; var k = 4 ; document . write ( findPermutation ( str , k ) ) ;
Find the number in a range having maximum product of the digits | Returns the product of digits of number x ; This function returns the number having maximum product of the digits ; Converting both integers to strings string a = l . ToString ( ) ; ; Let the current answer be r ; Stores the current number having current digit one less than current digit in b ; Replace all following digits with 9 to maximise the product ; Convert string to number ; Check if it lies in range and its product is greater than max product ; Driver Code
function product ( x ) { let prod = 1 ; while ( x > 0 ) { prod *= ( x % 10 ) ; x = parseInt ( x / 10 , 10 ) ; } return prod ; } function findNumber ( l , r ) { let b = r . toString ( ) ; let ans = r ; for ( let i = 0 ; i < b . length ; i ++ ) { if ( b [ i ] == ' ' ) continue ; let curr = b . split ( ' ' ) ; curr [ i ] = String . fromCharCode ( ( ( curr [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) - 1 ) + ' ' . charCodeAt ( ) ) ; for ( let j = i + 1 ; j < curr . length ; j ++ ) curr [ j ] = ' ' ; let num = 0 ; for ( let j = 0 ; j < curr . length ; j ++ ) num = num * 10 + ( curr [ j ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; if ( num >= l && product ( ans ) < product ( num ) ) ans = num ; } return ans ; } let l = 1 , r = 10 ; document . write ( findNumber ( l , r ) + " " ) ; l = 51 ; r = 62 ; document . write ( findNumber ( l , r ) ) ;
Concatenate suffixes of a String | Function to print the expansion of the string ; Take sub - string from i to n - 1 ; Print the sub - string ; Driver code
function printExpansion ( str ) { var size = 0 ; for ( var i = str . length - 1 ; i >= 0 ; i -- ) { var subStr = str . substring ( i , i + ++ size ) ; document . write ( subStr ) ; } } var str = " " ; printExpansion ( str ) ;
Construct a binary string following the given constraints | Function to print a binary string which has ' a ' number of 0 ' s , ▁ ' b ' ▁ number ▁ of ▁ 1' s and there are at least ' x ' indices such that s [ i ] != s [ i + 1 ] ; Divide index value by 2 and store it into d ; If index value x is even and x / 2 is not equal to a ; Loop for d for each d print 10 ; subtract d from a and b ; Loop for b to print remaining 1 's ; Loop for a to print remaining 0 's ; Driver code
function constructBinString ( a , b , x ) { let d , i ; d = parseInt ( x / 2 , 10 ) ; if ( x % 2 == 0 && parseInt ( x / 2 , 10 ) != a ) { d -- ; document . write ( " " ) ; a -- ; } for ( i = 0 ; i < d ; i ++ ) document . write ( " " ) ; a = a - d ; b = b - d ; for ( i = 0 ; i < b ; i ++ ) { document . write ( " " ) ; } for ( i = 0 ; i < a ; i ++ ) { document . write ( " " ) ; } } let a = 4 , b = 3 , x = 2 ; constructBinString ( a , b , x ) ;
Check If every group of a ' s ▁ is ▁ followed ▁ by ▁ a ▁ group ▁ of ▁ b ' s of same length | Function to match whether there are always n consecutive b ' s ▁ ▁ followed ▁ by ▁ n ▁ consecutive ▁ a ' s throughout the string ; Traverse through the string ; Count a 's in current segment ; Count b 's in current segment ; If both counts are not same . ; Driver code
function matchPattern ( s ) { let count = 0 ; let n = s . length ; let i = 0 ; while ( i < n ) { while ( i < n && s [ i ] == ' ' ) { count ++ ; i ++ ; } while ( i < n && s [ i ] == ' ' ) { count -- ; i ++ ; } if ( count != 0 ) return false ; } return true ; } let s = " " ; if ( matchPattern ( s ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Length of longest consecutive ones by at most one swap in a Binary String | Function to calculate the length of the longest consecutive 1 's ; To count all 1 's in the string ; To store cumulative 1 's ; Counting cumulative 1 's from left ; If 0 then start new cumulative one from that i ; perform step 3 of the approach ; step 3 ; string
function maximum_one ( s , n ) { var cnt_one = 0 ; var max_cnt = 0 , temp = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { cnt_one ++ ; temp ++ ; } else { max_cnt = Math . max ( temp , max_cnt ) ; temp = 0 ; } } max_cnt = Math . max ( max_cnt , temp ) ; var left = Array ( n ) ; var right = Array ( n ) ; if ( s [ 0 ] == ' ' ) left [ 0 ] = 1 ; else left [ 0 ] = 0 ; if ( s [ n - 1 ] == ' ' ) right [ n - 1 ] = 1 ; else right [ n - 1 ] = 0 ; for ( var i = 1 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) left [ i ] = left [ i - 1 ] + 1 ; else left [ i ] = 0 ; } for ( var i = n - 2 ; i >= 0 ; i -- ) { if ( s [ i ] == ' ' ) right [ i ] = right [ i + 1 ] + 1 ; else right [ i ] = 0 ; } for ( var i = 1 ; i < n - 1 ; i ++ ) { if ( s [ i ] == ' ' ) { var sum = left [ i - 1 ] + right [ i + 1 ] ; if ( sum < cnt_one ) max_cnt = Math . max ( max_cnt , sum + 1 ) ; else max_cnt = Math . max ( max_cnt , sum ) ; } } return max_cnt ; } var s = " " ; document . write ( maximum_one ( s , s . length ) ) ;
Maximum length substring with highest frequency in a string | Function to return maximum occurred subString of a String ; Size of the String ; To store maximum frequency ; To store String which has maximum frequency ; Return subString which has maximum frequency ; Driver code ; Function call
function MaxFreq ( str ) { let n = str . length ; let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let s = " " ; for ( let j = i ; j < n ; j ++ ) { s += str [ j ] ; if ( mp . has ( s ) ) { mp . set ( s , mp . get ( s ) + 1 ) ; } else { mp . set ( s , 1 ) ; } } } let maxi = 0 ; let s = " " ; for ( let [ key , value ] of mp . entries ( ) ) { if ( value > maxi ) { maxi = value ; s = key ; } else if ( value == maxi ) { let ss = key ; if ( ss . length > s . length ) s = ss ; } } return s ; } let str = " " ; document . write ( MaxFreq ( str ) ) ;
Lexicographically smallest substring with maximum occurrences containing a ' s ▁ and ▁ b ' s only | Function to Find the lexicographically smallest substring in a given string with maximum frequency and contains a ' s ▁ and ▁ b ' s only . ; To store frequency of digits ; size of string ; Take lexicographically larger digit in b ; get frequency of each character ; If no such string exits ; Maximum frequency ; Driver program
function maxFreq ( s , a , b ) { var fre = new Array ( 10 ) . fill ( 0 ) ; var n = s . length ; if ( a > b ) { var temp = a ; a = b ; b = temp ; } for ( var i = 0 ; i < n ; i ++ ) fre [ s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; if ( fre [ a ] === 0 && fre [ b ] === 0 ) return - 1 ; else if ( fre [ a ] >= fre [ b ] ) return a ; else return b ; } var a = 4 , b = 7 ; var s = " " ; document . write ( maxFreq ( s , a , b ) ) ;
Minimum steps to convert one binary string to other only using negation | Function to find the minimum steps to convert string a to string b ; array to mark the positions needed to be negated ; If two character are not same then they need to be negated ; To count the blocks of 1 ; To count the number of 1 ' s ▁ in ▁ ▁ each ▁ block ▁ of ▁ 1' s ; For the last block of 1 's ; Driver code
function convert ( n , a , b ) { let l = new Array ( n ) ; let i ; for ( i = 0 ; i < n ; i ++ ) l [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) l [ i ] = 1 ; } let cc = 0 ; let vl = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( l [ i ] == 0 ) { if ( vl != 0 ) cc += 1 ; vl = 0 ; } else vl += 1 ; } if ( vl != 0 ) cc += 1 ; document . write ( cc + " " ) ; } let a = " " ; let b = " " ; let n = a . length ; convert ( n , a , b ) ;
Generate a sequence with the given operations | function to find minimum required permutation ; Driver code
function StringMatch ( s ) { var lo = 0 , hi = s . length , len = s . length ; var ans = [ ] ; for ( var x = 0 ; x < len ; x ++ ) { if ( s [ x ] == ' ' ) { ans . push ( lo ) ; lo += 1 ; } else { ans . push ( hi ) ; hi -= 1 ; } } ans . push ( lo ) ; document . write ( " " ) ; for ( var i = 0 ; i < ans . length ; i ++ ) { document . write ( ans [ i ] ) ; if ( i != ans . length - 1 ) document . write ( " " ) ; } document . write ( " " ) ; } var S = " " ; StringMatch ( S ) ;
Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes | Function to find number of ways ; initialise result that store No . of swaps required ; Traverse both strings and check the bits as explained ; calculate result ; Driver code
function countWays ( s1 , s2 , n ) { var a , b , c , d ; a = b = c = d = 0 ; var result = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s2 . charAt ( i ) == ' ' ) { if ( s1 . charAt ( i ) == ' ' ) { c ++ ; } else { d ++ ; } } else { if ( s1 . charAt ( i ) == ' ' ) { a ++ ; } else { b ++ ; } } } result = a * d + b * c + c * d ; return result ; } var n = 5 ; var s1 = " " ; var s2 = " " ; document . write ( countWays ( s1 , s2 , n ) ) ;
Find the player who rearranges the characters to get a palindrome string first | Function that returns the winner of the game ; Initialize the freq array to 0 ; Iterate and count the frequencies of each character in the string ; Count the odd occurring character ; If odd occurrence ; Check condition for Player - 1 winning the game ; Driver code ; Function call that returns the winner
function returnWinner ( s , l ) { let freq = new Array ( 26 ) ; freq . fill ( 0 ) ; for ( let i = 0 ; i < l ; i ++ ) { freq [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; } let cnt = 0 ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] % 2 != 0 ) cnt ++ ; } if ( ( cnt == 0 ) || ( cnt & 1 ) == 1 ) return 1 ; else return 2 ; } let s = " " ; let l = s . length ; let winner = returnWinner ( s , l ) ; document . write ( " " + winner ) ;
Maximum sum and product of the M consecutive digits in a number | Function to find the maximum product ; Driver code
function maxProductSum ( str , m ) { let n = str . length ; let maxProd = Number . MIN_VALUE , maxSum = Number . MIN_VALUE ; for ( let i = 0 ; i <= n - m ; i ++ ) { let product = 1 , sum = 0 ; for ( let j = i ; j < m + i ; j ++ ) { product = product * ( str [ j ] - ' ' ) ; sum = sum + ( str [ j ] - ' ' ) ; } maxProd = Math . max ( maxProd , product ) ; maxSum = Math . max ( maxSum , sum ) ; } document . write ( " " + maxProd + " " ) ; document . write ( " " + maxSum ) ; } let str = " " ; let m = 3 ; maxProductSum ( str , m ) ;
Replace all consonants with nearest vowels in a string | Function to check if a character is vowel or not ; Function to replace consonant with nearest vowels ; if , string element is vowel , jump to next element ; check if consonant lies between two vowels , if it lies , than replace it with nearest vowel ; here the bsolute difference of ascii value is considered ; when s [ i ] is equal to either ' v ' , ' w ' , ' x ' , ' y ' , ' z ' ; Driver code
function isVowel ( ch ) { if ( ch !== " " && ch !== " " && ch !== " " && ch !== " " && ch !== " " ) return false ; return true ; } function replacingConsonants ( s ) { for ( var i = 0 ; i < s . length ; i ++ ) { if ( isVowel ( s [ i ] ) ) continue ; else { if ( s [ i ] > " " && s [ i ] < " " ) { if ( Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) > Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; } else if ( s [ i ] > " " && s [ i ] < " " ) { if ( Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) > Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; } else if ( s [ i ] > " " && s [ i ] < " " ) { if ( Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) > Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; } else if ( s [ i ] > " " && s [ i ] < " " ) { if ( Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) > Math . abs ( s [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) ) s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; } else if ( s [ i ] > " " ) s = s . substring ( 0 , i ) + " " + s . substring ( i + 1 ) ; } } return s ; } var s = " " ; document . write ( replacingConsonants ( s ) ) ;
Find time taken for signal to reach all positions in a string | Returns time needed for signal to traverse through complete string . ; for the calculation of last index ; for strings like oxoooo , xoxxoooo . . ; if coun is greater than max_length ; if ' x ' is present at the right side of max_length ; if ' x ' is present at left side of max_length ; We use ceiling function to handle odd number ' o ' s ; Driver code
function maxLength ( s , n ) { var right = 0 , left = 0 ; var coun = 0 , max_length = Number . MIN_VALUE ; s = s + ' ' ; for ( var i = 0 ; i <= n ; i ++ ) { if ( s [ i ] == ' ' ) coun ++ ; else { if ( coun > max_length ) { right = 0 ; left = 0 ; if ( s [ i ] == ' ' ) right = 1 ; if ( ( ( i - coun ) > 0 ) && ( s [ i - coun - 1 ] == ' ' ) ) left = 1 ; coun = Math . ceil ( coun / ( right + left ) ) ; max_length = Math . max ( max_length , coun ) ; } coun = 0 ; } } return max_length ; } var s = " " ; var n = s . length ; document . write ( maxLength ( s , n ) ) ;
Lexicographically largest string formed from the characters in range L and R | Function to return the lexicographically largest String ; hash array ; make 0 - based indexing ; iterate and count frequencies of character ; ans String ; iterate in frequency array ; add til all characters are added ; Driver Code
function printLargestString ( s , l , r ) { var freq = Array ( 26 ) . fill ( 0 ) ; l -- ; r -- ; for ( i = Math . min ( l , r ) ; i <= Math . max ( l , r ) ; i ++ ) { freq [ s . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ] ++ ; } var ans = " " ; for ( var i = 25 ; i >= 0 ; i -- ) { while ( freq [ i ] > 0 ) { ans += String . fromCharCode ( ' ' . charCodeAt ( 0 ) + i ) ; freq [ i ] -- ; } } return ans ; } var s = " " ; var l = 3 , r = 5 ; document . write ( printLargestString ( s , l , r ) ) ;
Arrange a binary string to get maximum value within a range of indices | Javascript implementation of the approach ; Storing the count of 1 's in the string ; Query of l and r ; Applying range update technique . ; Taking prefix sum to get the range update values ; Final array which will store the arranged string ; if after maximizing the ranges any 1 is left then we maximize the string lexicographically . ; Driver Code
function arrange ( s ) { var cc = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) cc ++ ; } var a = Array ( s . length + 1 ) . fill ( 0 ) ; var qq = [ [ 2 , 3 ] , [ 5 , 5 ] ] ; var n = qq . length ; for ( var i = 0 ; i < n ; i ++ ) { var l = qq [ i ] [ 0 ] , r = qq [ i ] [ 1 ] ; l -- , r -- ; a [ l ] ++ ; a [ r + 1 ] -- ; } var len_a = a . length ; for ( var i = 1 ; i < len_a ; i ++ ) { a [ i ] += a [ i - 1 ] ; } var zz = Array ( s . length ) . fill ( 0 ) ; for ( var i = 0 ; i < len_a - 1 ; i ++ ) { if ( a [ i ] > 0 ) { if ( cc > 0 ) { zz [ i ] = 1 ; cc -- ; } else break ; } if ( cc == 0 ) break ; } if ( cc > 0 ) { for ( var i = 0 ; i < s . length ; i ++ ) { if ( zz [ i ] == 0 ) { zz [ i ] = 1 ; cc -- ; } if ( cc == 0 ) break ; } } for ( var i = 0 ; i < s . length ; i ++ ) document . write ( zz [ i ] ) ; document . write ( " " ) ; } var str = " " ; arrange ( str ) ;
Check whether the vowels in a string are in alphabetical order or not | Function that checks whether the vowel characters in a string are in alphabetical order or not ; ASCII Value 64 is less than all the alphabets so using it as a default value ; check if the vowels in the string are sorted or not ; if the vowel is smaller than the previous vowel ; store the vowel ; Driver code ; check whether the vowel characters in a string are in alphabetical order or not
function areVowelsInOrder ( s ) { var n = s . length ; var c = String . fromCharCode ( 64 ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' ) { if ( s [ i ] < c ) return false ; else { c = s [ i ] ; } } } return true ; } var s = " " ; if ( areVowelsInOrder ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Program to replace every space in a string with hyphen | JavaScript program for the above approach ; Split by space and converting String to list and ; joining the list and storing in string ; returning the string ; Driver code
function printHyphen ( string ) { var lis = string . split ( " " ) string = lis . join ( " " ) ; return string ; } var string = " " ; document . write ( printHyphen ( string ) ) ;
Rearrange the string to maximize the number of palindromic substrings | Function to return the newString ; length of string ; hashing array ; iterate and count ; resulting string ; form the resulting string ; number of times character appears ; append to resulting string ; Driver code
function newString ( s ) { let l = s . length ; let freq = new Array ( 26 ) ; freq . fill ( 0 ) ; for ( let i = 0 ; i < l ; i ++ ) { freq [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] += 1 ; } let ans = " " ; for ( let i = 0 ; i < 26 ; i ++ ) { for ( let j = 0 ; j < freq [ i ] ; j ++ ) { ans += String . fromCharCode ( 97 + i ) ; } } return ans ; } let s = " " ; document . write ( newString ( s ) ) ;
Program to find remainder when large number is divided by r | Function to Return Remainder ; len is variable to store the length of Number string . ; loop that find Remainder ; Return the remainder ; Driver code Get the large number as string ; Get the divisor R ; Find and print the remainder
function Remainder ( str , R ) { var len = str . length ; var Num , Rem = 0 ; for ( var i = 0 ; i < len ; i ++ ) { Num = Rem * 10 + ( str [ i ] - ' ' ) ; Rem = Num % R ; } return Rem ; } var str = " " ; var R = 13 ; document . write ( Remainder ( str , R ) ) ;
Number of balanced bracket subsequence of length 2 and 4 | Javascript implementation of above approach ; Taking the frequency suffix sum of the number of 2 's present after every index ; Storing the count of subsequence ; Subsequence of length 2 ; Subsequence of length 4 of type 1 1 2 2 ; Subsequence of length 4 of type 1 2 1 2 ; Driver Code
function countWays ( a , n ) { let i , j ; let suff = new Array ( n ) ; if ( a [ n - 1 ] == 2 ) suff [ n - 1 ] = 1 ; for ( i = n - 2 ; i >= 0 ; i -- ) { if ( a [ i ] == 2 ) suff [ i ] = suff [ i + 1 ] + 1 ; else suff [ i ] = suff [ i + 1 ] ; } let ss = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) ss += suff [ i ] ; } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( a [ i ] == 1 && a [ j ] == 1 && suff [ j ] >= 2 ) { ss += ( suff [ j ] ) * ( suff [ j ] - 1 ) / 2 ; } } } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( a [ i ] == 1 && a [ j ] == 1 && ( suff [ i ] - suff [ j ] ) >= 1 && suff [ j ] >= 1 ) { ss += ( suff [ i ] - suff [ j ] ) * suff [ j ] ; } } } document . write ( ss ) ; } let a = [ 1 , 2 , 1 , 1 , 2 , 2 ] ; let n = 6 ; countWays ( a , n ) ;