codeparrot/xlcost-text-to-code huggingface dataset:
Smallest odd digits number not less than N | function to check if all digits are odd of a given number ; iterate for all digits ; if digit is even ; all digits are odd ; function to return the smallest number with all digits odd ; iterate till we find a number with all digits odd ; Driver Code
function check_digits ( n ) { while ( n > 1 ) { if ( ( n % 10 ) % 2 == 0 ) return 0 ; n = parseInt ( n / 10 ) ; } return 1 ; } function smallest_number ( n ) { for ( i = n ; ; i ++ ) if ( check_digits ( i ) ) return i ; } let N = 2397 ; document . write ( smallest_number ( N ) ) ;
Smallest odd digits number not less than N | function to return the smallest number with all digits odd ; convert the number to string to perform operations ; find out the first even number ; if no even numbers are there , than n is the answer ; add all digits till first even ; increase the even digit by 1 ; add 1 to the right of the even number ; Driver Code
function smallestNumber ( n ) { var num = 0 ; var s = " " ; var duplicate = n ; while ( n ) { s = String . fromCharCode ( n % 10 + 48 ) + s ; n = parseInt ( n / 10 ) ; } var index = - 1 ; for ( var i = 0 ; i < s . length ; i ++ ) { var digit = s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( ( digit & 1 ) == 0 ) { index = i ; break ; } } if ( index == - 1 ) return duplicate ; for ( var i = 0 ; i < index ; i ++ ) num = num * 10 + ( s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; num = num * 10 + ( s [ index ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 1 ) ; for ( var i = index + 1 ; i < s . length ; i ++ ) num = num * 10 + 1 ; return num ; } var N = 2397 ; document . write ( smallestNumber ( N ) ) ;
Count and Print the alphabets having ASCII value in the range [ l , r ] | Function to count the number of characters whose ascii value is in range [ l , r ] ; Initializing the count to 0 ; Increment the count if the value is less ; return the count ; Driver code
function CountCharacters ( str , l , r ) { let cnt = 0 ; let len = str . length ; for ( let i = 0 ; i < len ; i ++ ) { if ( l <= str [ i ] . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= r ) { cnt ++ ; document . write ( str [ i ] + " " ) ; } } return cnt ; } let str = " " ; let l = 102 , r = 111 ; document . write ( " " + " " ) ; document . write ( " " + CountCharacters ( str , l , r ) ) ;
Minimum steps to remove substring 010 from a binary string | Function to find the minimum steps ; substring "010" found ; Get the binary string ; Find the minimum steps
function minSteps ( str ) { let count = 0 ; for ( let i = 0 ; i < str . length - 2 ; i ++ ) { if ( ( str [ i ] ) == ' ' ) { if ( str [ i + 1 ] == ' ' ) { if ( str [ i + 2 ] == ' ' ) { count ++ ; i += 2 ; } } } } return count ; } let str = " " ; document . write ( minSteps ( str ) ) ;
XOR of Prime Frequencies of Characters in a String | Function to create Sieve to check primes ; false here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to find XOR of prime frequencies ; map is used to store character frequencies ; Traverse the map ; Calculate XOR of all prime frequencies ; Driver code
function SieveOfEratosthenes ( prime , p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( var p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( var i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } function xorOfPrime ( s ) { var prime = Array ( 100005 ) . fill ( true ) ; SieveOfEratosthenes ( prime , 10005 ) ; var i , j ; var m = new Map ( ) ; for ( 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 ) ; } } var result = 0 ; var flag = 0 ; m . forEach ( ( value , key ) => { if ( prime [ value ] ) { result ^= value ; flag = 1 ; } } ) ; if ( ! flag ) return - 1 ; return result ; } var s = " " ; document . write ( xorOfPrime ( s ) ) ;
Count of alphabets having ASCII value less than and greater than k | Function to count the number of characters whose ascii value is less than k ; Initialising the count to 0 ; Incrementing the count if the value is less ; return the count ; Driver code
function CountCharacters ( str , k ) { let cnt = 0 ; let len = str . length ; for ( let i = 0 ; i < len ; i ++ ) { if ( str [ i ] . charCodeAt ( 0 ) < k ) cnt ++ ; } return cnt ; } let str = " " ; let k = 90 ; let count = CountCharacters ( str , k ) ; document . write ( " " + count + " " ) ; document . write ( " " + ( str . length - count ) ) ;
De Bruijn sequence | Set 1 | Javascript implementation of the above approach ; Modified DFS in which no edge is traversed twice ; Function to find a de Bruijn sequence of order n on k characters ; Clearing global variables ; Number of edges ; Driver code
var seen = new Set ( ) ; var edges = [ ] ; function dfs ( node , k , A ) { for ( var i = 0 ; i < k ; ++ i ) { var str = node + A [ i ] ; if ( ! seen . has ( str ) ) { seen . add ( str ) ; dfs ( str . substring ( 1 ) , k , A ) ; edges . push ( i ) ; } } } function deBruijn ( n , k , A ) { seen = new Set ( ) ; edges = [ ] ; var startingNode = A [ 0 ] . repeat ( n - 1 ) ; dfs ( startingNode , k , A ) ; var S = " " ; var l = Math . pow ( k , n ) ; for ( var i = 0 ; i < l ; ++ i ) S += A [ edges [ i ] ] ; S += startingNode ; return S ; } function strings ( n , charAt ) { var str = " " ; for ( var i = 0 ; i < n ; i ++ ) str += charAt ; return str ; } var n = 3 , k = 2 ; var A = " " ; document . write ( deBruijn ( n , k , A ) ) ;
Number of words in a camelcase sequence | Function to find the count of words in a CamelCase sequence ; driver program
function countWords ( str ) { let count = 1 ; for ( let i = 1 ; i < str . length - 1 ; i ++ ) { if ( str [ i ] >= ' ' && str [ i ] <= ' ' ) count ++ ; } return count ; } let str = " " ; document . write ( countWords ( str ) ) ;
Check whether frequency of characters in a string makes Fibonacci Sequence | Function to check if the frequencies are in Fibonacci series ; map to store the frequencies of character ; Vector to store first n fibonacci numbers ; Get the size of the map ; a and b are first and second terms of fibonacci series ; vector v contains elements of fibonacci series ; Compare vector elements with values in Map ; Driver code
function isFibonacci ( 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 ] ) ) ; } else { m . set ( s [ i ] , 1 ) ; } } var v = [ ] ; var n = m . length ; var a = 1 , b = 1 ; var c ; v . push ( a ) ; v . push ( b ) ; for ( var i = 0 ; i < n - 2 ; i ++ ) { v . push ( a + b ) ; c = a + b ; a = b ; b = c ; } var flag = 1 ; var i = 0 ; m . forEach ( ( value , key ) => { if ( value != v [ i ] ) { flag = 0 ; } } ) ; if ( flag == 1 ) return " " ; else return " " ; } var s = " " ; document . write ( isFibonacci ( s ) ) ;
Lexicographically smallest string formed by removing at most one character | Function to return the smallest String ; iterate the String ; first point where s [ i ] > s [ i + 1 ] ; append the String without i - th character in it ; leave the last character ; Driver Code
function smallest ( s ) { let l = s . length ; let ans = " " ; for ( let i = 0 ; i < l - 1 ; i ++ ) { if ( s [ i ] > s [ i + 1 ] ) { for ( let j = 0 ; j < l ; j ++ ) { if ( i != j ) { ans += s [ j ] ; } } return ans ; } } ans = s . substring ( 0 , l - 1 ) ; return ans ; } let s = " " ; document . write ( smallest ( s ) ) ;
Arrangement of the characters of a word such that all vowels are at odd places | Function to return the factorial of a number ; calculating nPr ; Function to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Get total even positions ; Get total odd positions ; Store frequency of each character of the string ; Count total number of vowels ; Count total number of consonants ; Calculate the total number of ways ; Driver code
function fact ( n ) { let f = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { f = f * i ; } return f ; } function npr ( n , r ) { return fact ( n ) / fact ( n - r ) ; } function countPermutations ( str ) { let even = Math . floor ( str . length / 2 ) ; let odd = str . length - even ; let ways = 0 ; let freq = new Array ( 26 ) . fill ( 0 ) ; for ( let i = 0 ; i < str . length ; i ++ ) { ++ freq [ str . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ] ; } let nvowels = freq [ 0 ] + freq [ 4 ] + freq [ 8 ] + freq [ 14 ] + freq [ 20 ] ; let nconsonants = str . length - nvowels ; ways = npr ( odd , nvowels ) * npr ( nconsonants , nconsonants ) ; return ways ; } let str = " " ; document . write ( countPermutations ( str ) ) ;
Replace all consonants with nearest vowels in a string | Function to replace consonant with nearest vowels ; Driver code
function replacingConsonants ( s ) { var nVowel = " " ; for ( var i = 0 ; i < s . length ; i ++ ) s [ i ] = nVowel [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; return s . join ( ' ' ) ; } var s = " " . split ( ' ' ) ; document . write ( replacingConsonants ( s ) ) ;
Replace consonants with next immediate consonants alphabetically in a String | Function to check if a character is vowel or not ; Function that replaces consonant with next immediate consonant alphabatically ; Start traversing the string ; if character is z , than replace it with character b ; if the alphabet is not z ; replace the element with next immediate alphabet ; if next immediate alphabet is vowel , than take next 2 nd immediate alphabet ( since no two vowels occurs consecutively in alphabets ) hence no further checking is required ; Driver code
function isVowel ( ch ) { if ( ch != ' ' && ch != ' ' && ch != ' ' && ch != ' ' && ch != ' ' ) return false ; return true ; } function replaceConsonants ( s ) { for ( var i = 0 ; i < s . length ; i ++ ) { if ( ! isVowel ( s [ i ] ) ) { if ( s [ i ] == ' ' ) s [ i ] = ' ' ; else { s [ i ] = String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) + 1 ) ; if ( isVowel ( s [ i ] ) ) s [ i ] = String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) + 1 ) ; } } } return s . join ( ' ' ) ; } var s = " " . split ( ' ' ) ; document . write ( replaceConsonants ( s ) ) ;
Remove characters from string that appears strictly less than K times | Javascript program to reduce the string by removing the characters which appears less than k times ; Function to reduce the string by removing the characters which appears less than k times ; Hash table initialised to 0 ; Increment the frequency of the character ; create a new empty string ; Append the characters which appears more than equal to k times ; Driver Code
let MAX_CHAR = 26 ; function removeChars ( str , k ) { let hash = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) hash [ i ] = 0 ; let n = str . length ; for ( let i = 0 ; i < n ; ++ i ) { hash [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } let res = " " ; for ( let i = 0 ; i < n ; ++ i ) { if ( hash [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] >= k ) { res += str [ i ] ; } } return res ; } let str = " " ; let k = 2 ; document . write ( removeChars ( str , k ) ) ;
Count changes in Led Lights to display digits one by one | javascript program to count number of on offs to display digits of a number . ; store the led lights required to display a particular number . ; compute the change in led and keep on adding the change ; Driver code
function countOnOff ( n ) { var Led = [ 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 5 ] ; var len = n . length ; var sum = Led [ n . charAt ( 0 ) - ' ' ] ; for ( i = 1 ; i < len ; i ++ ) { sum = sum + Math . abs ( Led [ n . charAt ( i ) - ' ' ] - Led [ n . charAt ( i - 1 ) - ' ' ] ) ; } return sum ; } n = " " ; document . write ( countOnOff ( n ) ) ;
Program to check if all characters have even frequency | JavaScript implementation of the above approach ; creating a frequency array ; Finding length of s ; counting frequency of all characters ; checking if any odd frequency is there or not ; Driver Code
function check ( s ) { var freq = Array ( 26 ) . fill ( 0 ) ; var n = s . length ; for ( var i = 0 ; i < s . length ; i ++ ) freq [ s [ i ] - 97 ] ++ ; for ( var i = 0 ; i < 26 ; i ++ ) if ( freq [ i ] % 2 == 1 ) return false ; return true ; } var s = " " ; check ( s ) ? document . write ( " " ) : document . write ( " " ) ;
Sub | Function that checks if the string contain exactly K characters having ASCII value greater than p ; if ASCII value is greater than ' p ' ; if count of satisfying characters is equal to ' K ' then return true ; otherwise return false ; function to count sub - strings ; length of the string ; count of sub - strings ; ' i ' is the starting index for the sub - string ; ' j ' is the no . of characters to include in the sub - string ; check if the sub - string satisfies the condition ; Driver code
function isValidSubString ( r , K , p ) { var c = 0 ; for ( var i = 0 ; i < r . length ; i ++ ) { if ( r . charCodeAt ( i ) > p ) c ++ ; } if ( c == K ) return true ; else return false ; } function countSubStrings ( s , K , p ) { var l = s . length ; var count = 0 ; for ( var i = 0 ; i < l ; i ++ ) { for ( var j = K ; ( i + j ) <= l ; j ++ ) { var r = s . substring ( i , i + j ) ; if ( isValidSubString ( r , K , p ) ) count ++ ; } } document . write ( count + " " ) ; } var s = " " ; var K = 4 ; var p = 110 ; countSubStrings ( s , K , p ) ;
Count number of substrings with numeric value greater than X | Function that counts valid sub - strings ; Only take those numbers that do not start with '0' . ; converting the sub - string starting from index ' i ' and having length ' len ' to int and checking if it is greater than X or not ; Driver code
function count ( S , X ) { var count = 0 ; var N = S . length ; for ( var i = 0 ; i < N ; ++ i ) { if ( S [ i ] != ' ' ) { for ( var len = 1 ; ( i + len ) <= N ; ++ len ) { if ( parseInt ( S . substring ( i , i + len ) ) > X ) count ++ ; } } } return count ; } var S = " " ; var X = 97 ; document . write ( count ( S , X ) ) ;
Check whether a binary string can be formed by concatenating given N numbers sequentially | Function that returns false if the number passed as argument contains digit ( s ) other than '0' or '1' ; Function that checks whether the binary String can be formed or not ; Empty String for storing the binary number ; check if a [ i ] can be a part of the binary String ; Conversion of var into String ; if a [ i ] can 't be a part  then break the loop ; possible to create binary String ; impossible to create binary String ; Driver code
function isBinary ( n ) { while ( n != 0 ) { var temp = n % 10 ; if ( temp != 0 && temp != 1 ) { return false ; } n = parseInt ( n / 10 ) ; } return true ; } function formBinaryStr ( n , a ) { var flag = true ; var s = " " ; for ( i = 0 ; i < n ; i ++ ) { if ( isBinary ( a [ i ] ) ) s += " " + a [ i ] ; else { flag = false ; break ; } } if ( flag ) document . write ( s + " " ) ; else document . write ( " " ) ; } var a = [ 10 , 1 , 0 , 11 , 10 ] ; var N = a . length ; formBinaryStr ( N , a ) ;
Check if all the palindromic sub | Function to check if the string is palindrome ; Function that checks whether all the palindromic sub - strings are of odd length . ; Creating each substring ; If the sub - string is of even length and is a palindrome then , we return False ; Driver code
function checkPalindrome ( s ) { for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] != s [ s . length - i - 1 ] ) return false ; } return true ; } function CheckOdd ( s ) { let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { let x = " " ; for ( let j = i ; j < n ; j ++ ) { x += s [ j ] ; if ( x . length % 2 == 0 && checkPalindrome ( x ) == true ) return false ; } } return true ; } let s = " " ; if ( CheckOdd ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Number of permutations of a string in which all the occurrences of a given character occurs together | Function to return factorial of the number passed as argument ; Function to get the total permutations which satisfy the given condition ; Create has to store count of each character ; Store character occurrences ; Count number of times Particular character comes ; If particular character isn 't  present in the string then return 0 ; Remove count of particular character ; Total length of the string ; Assume all occurrences of particular character as a single character . ; Compute factorial of the length ; Divide by the factorials of the no . of occurrences of all the characters . ; return the result ; Driver Code ; Assuming the string and the character are all in uppercase
function fact ( n ) { let result = 1 ; for ( let i = 1 ; i <= n ; i ++ ) result *= i ; return result ; } function getResult ( str , ch ) { let has = new Array ( 26 ) . fill ( null ) ; for ( let i = 0 ; i < str . length ; i ++ ) has [ str . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ] ++ ; particular = has [ ch . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; if ( particular == 0 ) return 0 ; has [ ch . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] = 0 ; let total = str . length ; total = total - particular + 1 ; let result = fact ( total ) ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( has [ i ] > 1 ) { result = result / fact ( has [ i ] ) ; } } return result ; } let str = " " ; document . write ( getResult ( str , ' ' ) + " " ) ;
Check if there exists any sub | Function to check if there exists at least 1 sub - sequence in a string which is not palindrome ; use set to count number of distinct characters ; insert each character in set ; If there is more than 1 unique characters , return true ; Else , return false ; Driver code
function isAnyNotPalindrome ( s ) { var unique = new Set ( ) ; for ( var i = 0 ; i < s . length ; i ++ ) unique . add ( s [ i ] ) ; if ( unique . size > 1 ) return true ; else return false ; } var s = " " ; if ( isAnyNotPalindrome ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Arrangement of words without changing the relative position of vowel and consonants | this function return n ! ; this will return total number of ways ; freq maintains frequency of each character in word ; check character is vowel or not ; the characters that are not vowel must be consonant ; number of ways to arrange vowel ; multiply both as these are independent ; string contains only capital letters ; this will contain ans
function factorial ( n ) { let res = 1 ; for ( let i = 1 ; i <= n ; i ++ ) res = res * i ; return res ; } function count1 ( word ) { let freq = new Array ( 27 ) . fill ( 0 ) ; for ( let i = 0 ; i < 27 ; i ++ ) freq [ i ] = 0 ; let vowel = 0 ; let consonant = 0 ; for ( let i = 0 ; i < word . length ; i ++ ) { freq [ word . charCodeAt ( i ) - 65 ] ++ ; if ( word [ i ] == ' ' word [ i ] == ' ' word [ i ] == ' ' word [ i ] == ' ' word [ i ] == ' ' ) { vowel ++ ; } else consonant ++ ; } vowelArrange = factorial ( vowel ) ; vowelArrange /= factorial ( freq [ 0 ] ) ; vowelArrange /= factorial ( freq [ 4 ] ) ; vowelArrange /= factorial ( freq [ 8 ] ) ; vowelArrange /= factorial ( freq [ 14 ] ) ; vowelArrange /= factorial ( freq [ 20 ] ) ; consonantArrange = factorial ( consonant ) ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( i != 0 && i != 4 && i != 8 && i != 14 && i != 20 ) consonantArrange /= factorial ( freq [ i ] ) ; } let total = vowelArrange * consonantArrange ; return total ; } let word = " " ; let ans = count1 ( word ) ; document . write ( ans ) ;
Program to find the product of ASCII values of characters in a string | Function to find product of ASCII value of characters in string ; Traverse string to find the product ; Return the product ; Driver Code
function productAscii ( str ) { var prod = 1 ; for ( i = 0 ; i < str . length ; i ++ ) { prod *= str . charAt ( i ) . charCodeAt ( 0 ) ; } return prod ; } str = " " ; document . write ( productAscii ( str ) ) ;
Check if a large number can be divided into two or more segments of equal sum | Function to check if a number can be divided into segments ; length of string ; array to store prefix sum ; first index ; calculate the prefix ; iterate for all number from second number ; sum from 0 th index to i - 1 th index ; counter turns true when sum is obtained from a segment ; iterate till the last number ; sum of segments ; if segment sum is equal to first segment ; when greater than not possible ; if at the end all values are traversed and all segments have sum equal to first segment then it is possible ; Driver Code
function check ( s ) { let n = s . length ; var Presum = [ ] ; Presum . push ( parseInt ( s [ 0 ] ) ) ; let i ; for ( i = 1 ; i < n ; i ++ ) { Presum . push ( Presum [ i - 1 ] + parseInt ( s [ i ] ) ) ; } for ( i = 1 ; i <= n - 1 ; i ++ ) { let sum = Presum [ i - 1 ] ; let presum = 0 ; let it = i ; let flag = 0 ; while ( it < n ) { presum += parseInt ( s [ it ] ) ; if ( presum == sum ) { presum = 0 ; flag = 1 ; } else if ( presum > sum ) { break ; } it ++ ; } if ( presum == 0 && it == n && flag == 1 ) { return true ; } } return false ; } var s = " " ; if ( check ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Balance a string after removing extra brackets | Print balanced and remove extra brackets from string ; Maintain a count for opening brackets Traversing string ; check if opening bracket ; print str [ i ] and increment count by 1 ; check if closing bracket and count != 0 ; decrement count by 1 ; if str [ i ] not a closing brackets print it ; balanced brackets if opening brackets are more then closing brackets ; print remaining closing brackets ; Driver Code
function balancedString ( str ) { var count = 0 , i ; var n = str . length ; for ( i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { document . write ( str [ i ] ) ; count ++ ; } else if ( str [ i ] == ' ' && count != 0 ) { document . write ( str [ i ] ) ; count -- ; } else if ( str [ i ] != ' ' ) document . write ( str [ i ] ) ; } if ( count != 0 ) for ( i = 0 ; i < count ; i ++ ) document . write ( " " ) ; } var str = " " ; balancedString ( str ) ;
Minimum operation require to make first and last character same | Javascript program to minimum operation require to make first and last character same ; Recursive function call ; Decrement ending index only ; Increment starting index only ; Increment starting index and decrement index ; Driver code ; Function call
var MAX = 1000000000 ; function minOperation ( s , i , j , count ) { if ( ( i >= s . length && j < 0 ) || ( i == j ) ) return MAX ; if ( s [ i ] == s [ j ] ) return count ; if ( i >= s . length ) return minOperation ( s , i , j - 1 , count + 1 ) ; else if ( j < 0 ) return minOperation ( s , i + 1 , j , count + 1 ) ; else return Math . min ( minOperation ( s , i , j - 1 , count + 1 ) , minOperation ( s , i + 1 , j , count + 1 ) ) ; } var s = " " ; var ans = minOperation ( s , 0 , s . length - 1 , 0 ) ; if ( ans == MAX ) document . write ( - 1 ) ; else document . write ( ans ) ;
Count strings with consonants and vowels at alternate position | Function to find the count of strings ; Variable to store the final result ; Loop iterating through string ; If ' $ ' is present at the even position in the string ; ' sum ' is multiplied by 21 ; If ' $ ' is present at the odd position in the string ; ' sum ' is multiplied by 5 ; Let the string ' str ' be s$$e$ ; Print result
function countStrings ( s ) { let sum = 1 ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( i % 2 == 0 && s [ i ] == ' ' ) sum *= 21 ; else if ( s [ i ] == ' ' ) sum *= 5 ; } return sum ; } let str = " " ; document . write ( countStrings ( str ) ) ;
Remove duplicates from a string in O ( 1 ) extra space | Function to remove duplicates ; keeps track of visited characters ; gets character value ; keeps track of length of resultant String ; check if Xth bit of counter is unset ; mark current character as visited ; Driver code
function removeDuplicatesFromString ( string ) { let counter = 0 ; let str = string . split ( " " ) ; let i = 0 ; let size = str . length ; let x ; let length = 0 ; while ( i < size ) { x = str [ i ] . charCodeAt ( 0 ) - 97 ; if ( ( counter & ( 1 << x ) ) == 0 ) { str [ length ] = String . fromCharCode ( ' ' . charCodeAt ( 0 ) + x ) ; counter = counter | ( 1 << x ) ; length ++ ; } i ++ ; } return str . join ( " " ) . slice ( 0 , length ) ; } let str = " " ; document . write ( removeDuplicatesFromString ( str ) ) ;