codeparrot/xlcost-text-to-code huggingface dataset:
Count of lexicographically smaller characters on right | Function to count the smaller characters on the right of index i ; store the length of string ; initialize each elements of arr to zero ; array to store count of smaller characters on the right side of that index ; initialize the variable to store the count of characters smaller than that at index i ; adding the count of characters smaller than index i ; print the count of characters smaller than index i stored in ans array ; Driver Code
function countSmaller ( str ) { var n = str . length ; var arr = Array ( 26 ) . fill ( 0 ) ; var ans = Array ( n ) ; for ( var i = n - 1 ; i >= 0 ; i -- ) { arr [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; var ct = 0 ; for ( var j = 0 ; j < str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; j ++ ) { ct += arr [ j ] ; } ans [ i ] = ct ; } for ( var i = 0 ; i < n ; i ++ ) { document . write ( ans [ i ] + " " ) ; } } var str = " " ; countSmaller ( str ) ;
Character whose frequency is equal to the sum of frequencies of other characters of the given string | Function that returns true if some character exists in the given string whose frequency is equal to the sum frequencies of other characters of the string ; If string is of odd length ; To store the frequency of each character of the string ; Update the frequencies of the characters ; No such character exists ; Driver code
function isFrequencyEqual ( str , len ) { if ( len % 2 == 1 ) return false ; var i , freq = Array ( 26 ) . fill ( 0 ) ; for ( i = 0 ; i < len ; i ++ ) freq [ str [ i ] - ' ' ] ++ ; for ( i = 0 ; i < 26 ; i ++ ) if ( freq [ i ] == parseInt ( len / 2 ) ) return true ; return false ; } var str = " " ; var len = str . length ; if ( isFrequencyEqual ( str , len ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum replacements to make adjacent characters unequal in a ternary string | Function to count the number of minimal replacements ; Find the length of the string ; Iterate in the string ; Check if adjacent is similar ; If not the last pair ; Check for character which is not same in i + 1 and i - 1 ; } Last pair ; Check for character which is not same in i - 1 index ; Driver Code
function countMinimalReplacements ( s ) { var n = s . length ; var cnt = 0 ; var str = " " ; for ( var i = 1 ; i < n ; i ++ ) { if ( s [ i ] === s [ i - 1 ] ) { cnt += 1 ; if ( i !== n - 1 ) { for ( var it = 0 ; it < str . length ; it ++ ) { if ( str [ it ] !== s [ i + 1 ] && str [ it ] !== s [ i - 1 ] ) { s [ i ] = str [ it ] ; break ; } } else { for ( var it = 0 ; it < str . length ; it ++ ) { if ( str [ it ] !== s [ i - 1 ] ) { s [ i ] = str [ it ] ; break ; } } } } } return cnt ; } var s = " " ; document . write ( countMinimalReplacements ( s ) ) ;
Sub | Function that returns the index of next occurrence of the character c in string str starting from index start ; Starting from start ; If current character = c ; Not found ; Function to return the count of required sub - strings ; Stores running count of ' x ' starting from the end ; Next index of ' x ' starting from index 0 ; Next index of ' y ' starting from index 0 ; To store the count of required sub - strings ; If ' y ' appears before ' x ' it won 't contribute to a valid sub-string ; Find next occurrence of ' y ' ; If ' y ' appears after ' x ' every sub - string ending at an ' x ' appearing after this ' y ' and starting with the current ' x ' is a valid sub - string ; Find next occurrence of ' x ' ; Return the count ; Driver code
function nextIndex ( str , start , c ) { for ( let i = start ; i < str . length ; i ++ ) { if ( str [ i ] == c ) return i ; } return - 1 ; } function countSubStrings ( str ) { let i , n = str . length ; let countX = new Array ( n ) ; let count = 0 ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' ' ) count ++ ; countX [ i ] = count ; } let nextIndexX = nextIndex ( str , 0 , ' ' ) ; let nextIndexY = nextIndex ( str , 0 , ' ' ) ; count = 0 ; while ( nextIndexX != - 1 && nextIndexY != - 1 ) { if ( nextIndexX > nextIndexY ) { nextIndexY = nextIndex ( str , nextIndexY + 1 , ' ' ) ; continue ; } else { count += countX [ nextIndexY ] ; nextIndexX = nextIndex ( str , nextIndexX + 1 , ' ' ) ; } } return count ; } let s = " " ; document . write ( countSubStrings ( s ) ) ;
Check whether the frequencies of all the characters in a string are prime or not | function that returns true if n is prime else false ; 1 is not prime ; check if there is any factor or not ; function that returns true if the frequencies of all the characters of s are prime ; create a map to store the frequencies of characters ; update the frequency ; check whether all the frequencies are prime or not ; Driver code ; if all the frequencies are prime
function isPrime ( n ) { var i ; if ( n == 1 ) return false ; for ( i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) if ( n % i == 0 ) return false ; return true ; } function check_frequency ( s ) { var m = new Map ( ) ; for ( var i = 0 ; i < s . length ; i ++ ) if ( m . has ( s [ i ] ) ) { m . set ( s [ i ] , m . get ( s [ i ] ) + 1 ) ; } else { m . set ( s [ i ] , 1 ) ; } for ( var ch = ' ' . charCodeAt ( 0 ) ; ch <= ' ' . charCodeAt ( 0 ) ; ch ++ ) if ( m . get ( String . fromCharCode ( ch ) ) > 0 && ! isPrime ( m . get ( String . fromCharCode ( ch ) ) ) ) return false ; return true ; } var s = " " ; if ( check_frequency ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the count of substrings in alphabetic order | Function to find number of substrings ; Iterate over string length ; if any two chars are in alphabetic order ; find next char not in order ; return the result ; Driver function
function findSubstringCount ( str ) { var result = 0 ; var n = str . length ; for ( var i = 0 ; i < n - 1 ; i ++ ) { if ( String . fromCharCode ( str [ i ] . charCodeAt ( 0 ) + 1 ) == str [ i + 1 ] ) { result ++ ; while ( String . fromCharCode ( str [ i ] . charCodeAt ( 0 ) + 1 ) === str [ i + 1 ] ) { i ++ ; } } } return result ; } var str = " " ; document . write ( findSubstringCount ( str ) ) ;
Reduce the string by removing K consecutive identical characters | Javascript implementation of the approach ; Function to find the reduced string ; Base Case ; Creating a stack of type Pair ; Length of the string S ; iterate through the string ; if stack is empty then simply add the character with count 1 else check if character is same as top of stack ; if character at top of stack is same as current character increase the number of repetitions in the top of stack by 1 ; if character at top of stack is not same as current character push the character along with count 1 into the top of stack ; iterate through the stack Use string ( int , char ) in order to replicate the character multiple times and convert into string then add in front of output string ; Driver code
class Pair { constructor ( c , ctr ) { this . c = c ; this . ctr = ctr ; } } function reduced_String ( k , s ) { if ( k == 1 ) { let ans = " " ; return ans ; } let st = [ ] ; let l = s . length ; let ctr = 0 ; for ( let i = 0 ; i < l ; i ++ ) { if ( st . length == 0 ) { st . push ( new Pair ( s [ i ] , 1 ) ) ; continue ; } if ( st [ st . length - 1 ] . c == s [ i ] ) { let p = st [ st . length - 1 ] ; st . pop ( ) ; p . ctr += 1 ; if ( p . ctr == k ) { continue ; } else { st . push ( p ) ; } } else { st . push ( new Pair ( s [ i ] , 1 ) ) ; } } let ans = " " ; while ( st . length > 0 ) { let c = st [ st . length - 1 ] . c ; let cnt = st [ st . length - 1 ] . ctr ; while ( cnt -- > 0 ) ans = c + ans ; st . pop ( ) ; } return ans ; } let k = 2 ; let st = " " ; let ans = reduced_String ( k , st ) ; document . write ( ans + " " ) ;
First non | The function returns index of the first non - repeating character in a string . If all characters are repeating then returns INT_MAX ; Initialize all characters as absent . ; After below loop , the value of arr [ x ] is going to be index of x if x appears only once . Else the value is going to be either - 1 or - 2. ; If this character occurs only once and appears before the current result , then update the result ; Driver Code
function firstNonRepeating ( str ) { let NO_OF_CHARS = 256 ; let arr = new Array ( NO_OF_CHARS ) ; for ( let i = 0 ; i < NO_OF_CHARS ; i ++ ) arr [ i ] = - 1 ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( arr [ str [ i ] . charCodeAt ( 0 ) ] == - 1 ) arr [ str [ i ] . charCodeAt ( 0 ) ] = i ; else arr [ str [ i ] . charCodeAt ( 0 ) ] = - 2 ; } let res = Number . MAX_VALUE ; for ( let i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( arr [ i ] >= 0 ) res = Math . min ( res , arr [ i ] ) ; return res ; } let str = " " ; let index = firstNonRepeating ( str ) ; if ( index == Number . MAX_VALUE ) document . write ( " " + " " ) ; else document . write ( " " + " " + str [ index ] ) ;
Character replacement after removing duplicates from a string | Function to minimize String ; Duplicate characters are removed ; Checks if character has previously occurred or not if not then add it to the minimized String ' mstr ' ; Minimized String ; Utility function to print the minimized , replaced String ; Minimized String ; Creating final String by replacing character ; Index calculation ; Final String ; Driver code
function minimize ( str ) { let mstr = " " ; let l , i , flagchar = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { flagchar [ i ] = 0 ; } let ch ; l = str . length ; for ( i = 0 ; i < str . length ; i ++ ) { ch = str [ i ] ; if ( flagchar [ ch . charCodeAt ( 0 ) - 97 ] == 0 ) { mstr = mstr + ch ; flagchar [ ch . charCodeAt ( 0 ) - 97 ] = 1 ; } } return mstr ; } function replaceMinimizeUtil ( str ) { let minimizedStr , finalStr = " " ; let i , index , l ; let ch ; l = str . length ; minimizedStr = minimize ( str ) ; for ( i = 0 ; i < minimizedStr . length ; i ++ ) { ch = minimizedStr [ i ] . charCodeAt ( 0 ) ; index = ( ch * ch ) % l ; finalStr = finalStr + str [ index ] ; } document . write ( " " + finalStr ) ; } let str = " " ; replaceMinimizeUtil ( str ) ;
Latin alphabet cipher | JavaScript program to demonstrate Latin Alphabet Cipher function for calculating the encryption ; check isLetter ; Driver Code
function cipher ( str ) { for ( var i = 0 ; i < str . length ; i ++ ) { if ( ! isLetter ( str [ i ] ) && str [ i ] !== " " ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] >= " " && str [ i ] <= " " ) { document . write ( str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) + 1 + " " ) ; } else if ( str [ i ] >= " " && str [ i ] <= " " ) { document . write ( str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) + 1 + " " ) ; } if ( str [ i ] == " " ) document . write ( str [ i ] ) ; } document . write ( " " ) ; } function isLetter ( str ) { return str . length === 1 && str . match ( / [a-z] / i ) ; } var str = " " ; cipher ( str ) ;
Count palindrome words in a sentence | Function to check if a word is palindrome ; Function to count palindrome words ; to check last word for palindrome ; to store each word ; extracting each word ; Driver code
function checkPalin ( word ) { var n = word . length word = word . toLowerCase ( ) ; for ( var i = 0 ; i < n ; i ++ , n -- ) if ( word [ i ] != word [ n - 1 ] ) return false ; return true ; } function countPalin ( str ) { str = str + " " ; var word = " " ; var count = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) { var ch = str [ i ] ; if ( ch != ' ' ) word = word + ch ; else { if ( checkPalin ( word ) ) count ++ ; word = " " ; } } return count ; } document . write ( countPalin ( " " ) + " " ) ; document . write ( countPalin ( " " ) ) ;
Round the given number to nearest multiple of 10 | function to round the number ; Smaller multiple ; Larger multiple ; Return of closest of two
function round ( n ) { let a = parseInt ( n / 10 , 10 ) * 10 ; let b = a + 10 ; return ( n - a > b - n ) ? b : a ; } let n = 4722 ; document . write ( round ( n ) ) ;
Number of subsequences as " ab " in a string repeated K times | JavaScript code to find number of subsequences of " ab " in the string S which is repeated K times . ; Count of ' a ' s ; Count of ' b ' s ; Occurrence of " ab " s in string S ; Add following two : 1 ) K * ( Occurrences of " ab " in single string ) 2 ) a is from one string and b is from other . ; Driver Code
function countOccurrences ( s , K ) { let n = s . length ; let C = 0 , c1 = 0 , c2 = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) c1 ++ ; if ( s [ i ] == ' ' ) { c2 ++ ; C += c1 ; } } return C * K + ( K * ( K - 1 ) / 2 ) * c1 * c2 ; } let S = " " ; let k = 2 ; document . write ( countOccurrences ( S , k ) ) ;
String with k distinct characters and no same characters adjacent | Function to find a string of length n with k distinct characters . ; Initialize result with first k Latin letters ; Fill remaining n - k letters by repeating k letters again and again . ; Driver code
function findString ( n , k ) { let res = " " ; for ( let i = 0 ; i < k ; i ++ ) res = res + String . fromCharCode ( ' ' . charCodeAt ( 0 ) + i ) ; let count = 0 ; for ( let i = 0 ; i < n - k ; i ++ ) { res = res + String . fromCharCode ( ' ' . charCodeAt ( 0 ) + count ) ; count ++ ; if ( count == k ) count = 0 ; } return res ; } let n = 5 , k = 2 ; document . write ( findString ( n , k ) ) ;
Find substrings that contain all vowels | Returns true if x is vowel . ; Function to check whether a character is vowel or not ; Function to FindSubstrings of string ; To store vowels ; If current character is vowel then insert into hash , ; If all vowels are present in current substring ; Driver Code
function isVowel ( x ) { return ( x == ' ' x == ' ' x == ' ' x == ' ' x == ' ' ) ; } function findSubstring ( str ) { let hash = new Set ( ) ; let start = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( isVowel ( str [ i ] ) == true ) { hash . add ( str [ i ] ) ; if ( hash . size == 5 ) document . write ( str . substring ( start , i + 1 ) + " " ) ; } else { start = i + 1 ; hash . clear ( ) ; } } } let str = " " ; findSubstring ( str ) ;
Null Cipher | Function to decode the message . ; Store the decoded string ; found variable is used to tell that the encoded encoded character is found in that particular word . ; Set found variable to false whenever we find whitespace , meaning that encoded character for new word is not found ; Driver Code
function decode ( str ) { let res = " " ; let found = false ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == ' ' ) { found = false ; continue ; } if ( ! found ) { if ( ( str [ i ] >= ' ' && str [ i ] <= ' ' ) || ( str [ i ] >= ' ' && str [ i ] <= ' ' ) ) { res += ( str [ i ] ) ; found = true ; } } } return res . toLowerCase ( ) ; } let In = " " ; document . write ( " " + decode ( In ) ) ;
Generate all rotations of a given string | Prvar all the rotated string . ; Concatenate str with itself ; Prvar all substrings of size n . Note that size of sb is 2 n ; Driver method
function printRotatedString ( str ) { var n = str . length ; var sb = str ; sb += ( str ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j != n ; j ++ ) document . write ( sb . charAt ( i + j ) ) ; document . write ( ' ' ) ; } } var str = " " ; printRotatedString ( str ) ;
Check if frequency of all characters can become same by one removal | Javascript program to get same frequency character string by removal of at most one char ; Utility method to get index of character ch in lower alphabet characters ; Returns true if all non - zero elements values are same ; Get first non - zero element ; Check equality of each element with variable same ; Returns true if we can make all character frequencies same ; Fill frequency array ; If all frequencies are same , then return true ; Try decreasing frequency of all character by one and then check all equality of all non - zero frequencies ; Check character only if it occurs in str ; Driver code
let M = 26 ; function getIdx ( ch ) { return ( ch - ' ' ) ; } function allSame ( freq , N ) { let same = 0 ; let i ; for ( i = 0 ; i < N ; i ++ ) { if ( freq [ i ] > 0 ) { same = freq [ i ] ; break ; } } for ( let j = i + 1 ; j < N ; j ++ ) if ( freq [ j ] > 0 && freq [ j ] != same ) return false ; return true ; } function possibleSameCharFreqByOneRemoval ( str ) { let l = str . length ; let freq = new Array ( M ) ; for ( let i = 0 ; i < M ; i ++ ) { freq [ i ] = 0 ; } for ( let i = 0 ; i < l ; i ++ ) freq [ getIdx ( str [ i ] ) ] ++ ; if ( allSame ( freq , M ) ) return true ; for ( let c = ' ' ; c <= ' ' ; c ++ ) { let i = getIdx ( c ) ; if ( freq [ i ] > 0 ) { freq [ i ] -- ; if ( allSame ( freq , M ) ) return true ; freq [ i ] ++ ; } } return false ; } let str = " " ; if ( possibleSameCharFreqByOneRemoval ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a large number is divisible by 11 or not | Function to find that number divisible by 11 or not ; Compute sum of even and odd digit sums ; When i is even , position of digit is odd ; Check its difference is divisible by 11 or not ; Driver Code
function check ( str ) { let n = str . length ; let oddDigSum = 0 , evenDigSum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) oddDigSum += ( str [ i ] - ' ' ) ; else evenDigSum += ( str [ i ] - ' ' ) ; } return ( ( oddDigSum - evenDigSum ) % 11 == 0 ) ; } let str = " " ; if ( check ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Hamming Distance between two strings | function to calculate Hamming distance ; driver code ; function call
function hammingDist ( str1 , str2 ) { let i = 0 , count = 0 ; while ( i < str1 . length ) { if ( str1 [ i ] != str2 [ i ] ) count ++ ; i ++ ; } return count ; } let str1 = " " ; let str2 = " " ; document . write ( hammingDist ( str1 , str2 ) ) ;
Check if two strings are k | Javascript program to check if two strings are k anagram or not . ; Function to check that string is k - anagram or not ; If both strings are not of equal length then return false ; Store the occurrence of all characters in a hash_array ; Count number of characters that are different in both strings ; Return true if count is less than or equal to k ; Driver code
let MAX_CHAR = 26 ; function arekAnagrams ( str1 , str2 , k ) { let n = str1 . length ; if ( str2 . length != n ) return false ; let count1 = new Array ( MAX_CHAR ) ; let count2 = new Array ( MAX_CHAR ) ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) count1 [ str1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( let i = 0 ; i < n ; i ++ ) count2 [ str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) if ( count1 [ i ] > count2 [ i ] ) count = count + Math . abs ( count1 [ i ] - count2 [ i ] ) ; return ( count <= k ) ; } let str1 = " " ; let str2 = " " ; let k = 2 ; if ( arekAnagrams ( str1 , str2 , k ) ) document . write ( " " ) ; else document . write ( " " ) ;
Remove spaces from a given string | Function to remove all spaces from a given string ; To keep track of non - space character count ; Traverse the given string . If current character is not space , then place it at index ' count + + ' ; here count is incremented ; Driver code
function removeSpaces ( str ) { var count = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) if ( str [ i ] !== " " ) str [ count ++ ] = str [ i ] ; return count ; } var str = " " . split ( " " ) ; var i = removeSpaces ( str ) ; document . write ( str . join ( " " ) . substring ( 0 , i ) ) ;
Given a binary string , count number of substrings that start and end with 1. | A O ( n ) javascript program to count number of substrings starting and ending with 1 ; Traverse input string and count of 1 's in it ; Return count of possible pairs among m 1 's ; Driver program to test the above function
function countSubStr ( str , n ) { for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) m ++ ; } return m * Math . floor ( ( m - 1 ) / 2 ) ; } let str = " " ; let n = str . length ; document . write ( countSubStr ( str , n ) ) ;
Given a number as a string , find the number of contiguous subsequences which recursively add up to 9 | JavaScript program to count substrings with recursive sum equal to 9 ; To store result ; Consider every character as beginning of substring ; sum of digits in current substring ; One by one choose every character as an ending character ; Add current digit to sum , if sum becomes multiple of 5 then increment count . Let us do modular arithmetic to avoid overflow for big strings ; driver program to test above function
function count9s ( number ) { let count = 0 ; let n = ( number . length ) ; for ( let i = 0 ; i < n ; i ++ ) { let sum = number [ i ] - ' ' ; if ( number [ i ] == ' ' ) { count ++ ; } for ( let j = i + 1 ; j < n ; j ++ ) { sum = ( sum + number [ j ] - ' ' ) % 9 ; if ( sum == 0 ) { count ++ ; } } } return count ; } document . write ( count9s ( " " ) ) ; document . write ( " " ) ; document . write ( count9s ( " " ) ) ;
Generate n | This function generates all n bit Gray codes and prints the generated codes ; base case ; ' arr ' will store all generated codes ; start with one - bit pattern ; Every iteration of this loop generates 2 * i codes from previously generated i codes . ; Enter the prviously generated codes again in arr [ ] in reverse order . Nor arr [ ] has double number of codes . ; append 0 to the first half ; append 1 to the second half ; print contents of arr [ ] ; Driver program to test above function
function generateGrayarr ( n ) { if ( n <= 0 ) return ; let arr = [ ] ; arr . push ( " " ) ; arr . push ( " " ) ; let i , j ; for ( i = 2 ; i < ( 1 << n ) ; i = i << 1 ) { for ( j = i - 1 ; j >= 0 ; j -- ) arr . push ( arr [ j ] ) ; for ( j = 0 ; j < i ; j ++ ) arr [ j ] = " " + arr [ j ] ; for ( j = i ; j < 2 * i ; j ++ ) arr [ j ] = " " + arr [ j ] ; } for ( i = 0 ; i < arr . length ; i ++ ) document . write ( arr [ i ] + " " ) ; } generateGrayarr ( 3 ) ;