codeparrot/xlcost-text-to-code huggingface dataset:
Create a new string by alternately combining the characters of two halves of the string in reverse | Function performing calculations ; Calculating the two halves of string s as first and second The final string p ; It joins the characters to final string in reverse order ; It joins the characters to final string in reverse order ; Driver method ; Calling function
function solve ( s ) { var l = s . length ; var x = l / 2 ; var y = l ; var p = " " ; while ( x > 0 && y > l / 2 ) { var ch = s . charAt ( x - 1 ) ; p += ch ; x -- ; ch = s . charAt ( y - 1 ) ; p += ch ; y -- ; } if ( y > l / 2 ) { var ch = s . charAt ( x - 1 ) ; p += ch ; y -- ; } document . write ( p ) ; } var s = " " ; solve ( s ) ;
Program for length of a string using recursion | Function to calculate length ; if we reach at the end of the string ; Driver code
function recLen ( str ) { if ( str == " " ) return 0 ; else return recLen ( str . substring ( 1 ) ) + 1 ; } let str = " " ; document . write ( recLen ( str ) ) ;
Count consonants in a string ( Iterative and recursive methods ) | Function to check for consonant ; To handle lower case ; To check is character is Consonant ; Driver code
function isConsonant ( ch ) { ch = ch . toUpperCase ( ) ; console . log ( ch ) ; return ( ! ( ch == " " ch == " " ch == " " ch == " " ch == " " ) && ch . match ( / [A-Z] / i ) ) ; } function totalConsonants ( str ) { var count = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) if ( isConsonant ( str [ i ] ) ) ++ count ; return count ; } var str = " " ; document . write ( totalConsonants ( str ) ) ;
Count consonants in a string ( Iterative and recursive methods ) | Function to check for consonant ; To handle lower case ; To count total number of consonants from 0 to n - 1 ; Driver code
function isConsonant ( ch ) { ch = ch . toUpperCase ( ) ; return ( ! ( ch == ' ' ch == ' ' ch == ' ' ch == ' ' ch == ' ' ) && ch . charCodeAt ( 0 ) >= 65 && ch . charCodeAt ( 0 ) <= 90 ) ; } function totalConsonants ( str , n ) { if ( n == 1 ) return isConsonant ( str [ 0 ] ) ; return totalConsonants ( str , n - 1 ) + isConsonant ( str [ n - 1 ] ) ; } var str = " " ; document . write ( totalConsonants ( str , str . length ) ) ;
Check if actual binary representation of a number is palindrome | function to reverse bits of a number ; traversing bits of ' n ' from the right ; bitwise left shift ' rev ' by 1 ; if current bit is '1' ; bitwise right shift ' n ' by 1 ; required number ; function to check a number is palindrome or not ; get the number by reversing bits in the binary representation of ' n ' ; Driver code
function reverseBits ( n ) { let rev = 0 ; while ( n > 0 ) { rev <<= 1 ; if ( ( n & 1 ) == 1 ) rev ^= 1 ; n >>= 1 ; } return rev ; } function isPalindrome ( n ) { let rev = reverseBits ( n ) ; return ( n == rev ) ; } let n = 9 ; if ( isPalindrome ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Reduce Hamming distance by swapping two characters | Javascript code to decrease hamming distance using swap . ; Function to return the swapped indexes to get minimum hamming distance . ; Find the initial hamming distance ; Case - I : To decrease distance by two ; ASCII values of present character . ; If two same letters appear in different positions print their indexes ; Store the index of letters which is in wrong position ; Case : II ; If misplaced letter is found , print its original index and its new index ; Store the index of letters in wrong position ; Case - III ; Driver code
let MAX = 26 ; function Swap ( s , t , n ) { let dp = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { dp [ i ] = new Array ( MAX ) ; for ( let j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } let tot = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( s [ i ] != t [ i ] ) tot ++ ; for ( let i = 0 ; i < n ; i ++ ) { let a = s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; let b = t [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( a == b ) continue ; if ( dp [ a ] [ b ] != - 1 ) { document . write ( i + 1 + " " + ( dp [ a ] [ b ] + 1 ) + " " ) ; return ; } dp [ b ] [ a ] = i ; } let A = new Array ( MAX ) , B = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { A [ i ] = - 1 ; B [ i ] = - 1 ; } for ( let i = 0 ; i < n ; i ++ ) { let a = s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; let b = t [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( a == b ) continue ; if ( A [ b ] != - 1 ) { document . write ( i + 1 + " " + ( A [ b ] + 1 ) + " " ) ; return ; } if ( B [ a ] != - 1 ) { document . write ( i + 1 + " " + ( B [ a ] + 1 ) + " " ) ; return ; } A [ a ] = i ; B [ b ] = i ; } document . write ( - 1 ) ; } let S = " " ; let T = " " ; let n = S . length ; if ( S == " " T == " " ) document . write ( " " ) ; else Swap ( S , T , n ) ;
Convert string X to an anagram of string Y with minimum replacements | JavaScript program to convert string X to string Y which minimum number of changes . ; Function that converts string X into lexicographically smallest anagram of string Y with minimal changes ; Counting frequency of characters in each string . ; We maintain two more counter arrays ctrx [ ] and ctry [ ] Ctrx [ ] maintains the count of extra elements present in string X than string Y Ctry [ ] maintains the count of characters missing from string X which should be present in string Y . ; This means that we cannot edit the current character as it 's frequency  in string X is equal to or less  than the frequency in string Y.  Thus, we go to the next position ; Here , we try to find that character , which has more frequency in string Y and less in string X . We try to find this character in lexicographical order so that we get lexicographically smaller string ; This portion deals with the lexicographical property . Now , we put a character in string X when either this character has smaller value than the character present there right now or if this is the last position for it to exchange , else we fix the character already present here in this position . ; Driver code
const MAX = 26 ; function printAnagramAndChanges ( X , Y ) { var countx = new Array ( MAX ) . fill ( 0 ) ; var county = new Array ( MAX ) . fill ( 0 ) ; var ctrx = new Array ( MAX ) . fill ( 0 ) ; var ctry = new Array ( MAX ) . fill ( 0 ) ; var change = 0 ; var l = X . length ; for ( var i = 0 ; i < l ; i ++ ) { countx [ X [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; county [ Y [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } for ( var i = 0 ; i < MAX ; i ++ ) { if ( countx [ i ] > county [ i ] ) { ctrx [ i ] += countx [ i ] - county [ i ] ; } else if ( countx [ i ] < county [ i ] ) { ctry [ i ] += county [ i ] - countx [ i ] ; } change += Math . abs ( county [ i ] - countx [ i ] ) ; } for ( var i = 0 ; i < l ; i ++ ) { if ( ctrx [ X [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] === 0 ) { continue ; } var j ; for ( j = 0 ; j < MAX ; j ++ ) { if ( ctry [ j ] > 0 ) { break ; } } if ( countx [ X [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] === ctrx [ X [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] || X [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) > j ) { countx [ X [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] -- ; ctrx [ X [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] -- ; ctry [ j ] -- ; X [ i ] = String . fromCharCode ( " " . charCodeAt ( 0 ) + j ) ; } else { countx [ X [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] -- ; } } document . write ( " " + X . join ( " " ) + " " ) ; document . write ( " " + change / 2 ) ; } var x = " " , y = " " ; printAnagramAndChanges ( x . split ( " " ) , y . split ( " " ) ) ;
Find a string in lexicographic order which is in between given two strings | Function to find the lexicographically next string ; Iterate from last character ; If not ' z ' , increase by one ; if ' z ' , change it to ' a ' ; Driver Code ; If not equal , print the resultant string
function lexNext ( s , n ) { for ( let i = n - 1 ; i >= 0 ; i -- ) { if ( s [ i ] != ' ' ) { let code = s . charCodeAt ( i ) + 1 ; let str = String . fromCharCode ( code ) ; return s . substr ( 0 , i ) + str + s . substr ( i + 1 ) ; } s [ i ] = ' ' ; } } let S = " " ; let T = " " ; let n = S . length ; let res = lexNext ( S , n ) ; if ( res != T ) document . write ( res , ' ' ) ; else document . write ( " " ) ;
Add two numbers represented by two arrays | Return sum of two number represented by the arrays . Size of a [ ] is greater than b [ ] . It is made sure be the wrapper function ; array to store sum . ; Until we reach beginning of array . we are comparing only for second array because we have already compare the size of array in wrapper function . ; find sum of corresponding element of both arrays . ; Finding carry for next sum . ; If second array size is less the first array size . ; Add carry to first array elements . ; If there is carry on adding 0 index elements . append 1 to total sum . ; Converting array into number . ; Wrapper Function ; Making first array which have greater number of element ; Driven Program
function calSumUtil ( a , b , n , m ) { let sum = new Array ( n ) ; let i = n - 1 , j = m - 1 , k = n - 1 ; let carry = 0 , s = 0 ; while ( j >= 0 ) { s = a [ i ] + b [ j ] + carry ; sum [ k ] = ( s % 10 ) ; carry = Math . floor ( s / 10 ) ; k -- ; i -- ; j -- ; } while ( i >= 0 ) { s = a [ i ] + carry ; sum [ k ] = ( s % 10 ) ; carry = Math . floor ( s / 10 ) ; i -- ; k -- ; } let ans = 0 ; if ( carry ) ans = 10 ; for ( let i = 0 ; i <= n - 1 ; i ++ ) { ans += sum [ i ] ; ans *= 10 ; } return ans / 10 ; } function calSum ( a , b , n , m ) { if ( n >= m ) return calSumUtil ( a , b , n , m ) ; else return calSumUtil ( b , a , m , n ) ; } let a = [ 9 , 3 , 9 ] ; let b = [ 6 , 1 ] ; let n = a . length ; let m = b . length ; document . write ( calSum ( a , b , n , m ) + " " ) ;
Longest Common Anagram Subsequence | Javascript implementation to find the length ( ) of the longest common anagram subsequence ; function to find the length ( ) of the longest common anagram subsequence ; hash tables for storing frequencies of each character ; calculate frequency of each character of ' str1 [ ] ' ; calculate frequency of each character of ' str2 [ ] ' ; for each character add its minimum frequency out of the two Strings in ' len ' ; required length ( ) ; Driver Code
let SIZE = 26 ; function longCommomAnagramSubseq ( str1 , str2 , n1 , n2 ) { let freq1 = new Array ( SIZE ) ; let freq2 = new Array ( SIZE ) ; for ( let i = 0 ; i < SIZE ; i ++ ) { freq1 [ i ] = 0 ; freq2 [ i ] = 0 ; } let len = 0 ; for ( let i = 0 ; i < n1 ; i ++ ) freq1 [ str1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( let i = 0 ; i < n2 ; i ++ ) freq2 [ str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( let i = 0 ; i < SIZE ; i ++ ) len += Math . min ( freq1 [ i ] , freq2 [ i ] ) ; return len ; } let str1 = " " ; let str2 = " " ; let n1 = str1 . length ; let n2 = str2 . length ; document . write ( " " + longCommomAnagramSubseq ( str1 , str2 , n1 , n2 ) ) ;
Panalphabetic window in a string | Return if given string contain panalphabetic window . ; traversing the string ; if character of string is equal to ch , increment ch . ; if all characters are found , return true . ; Driven Program
function isPanalphabeticWindow ( s , n ) { var ch = ' ' ; for ( var i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ch ) ch = String . fromCharCode ( ch . charCodeAt ( 0 ) + 1 ) ; if ( ch == String . fromCharCode ( ' ' . charCodeAt ( 0 ) + 1 ) ) return true ; } return false ; } var s = " " + " " ; var n = ( s . length ) ; ( isPanalphabeticWindow ( s , n ) ) ? ( document . write ( " " ) ) : ( document . write ( " " ) ) ;
Program to print characters present at prime indexes in a given string | Javascript program to print characters at Prime index in a given String ; Corner case ; Check from 2 to n - 1 ; Function to print character at prime index ; Loop to check if index prime or not ; Driver code
function isPrime ( n ) { if ( n <= 1 ) return false ; for ( let i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } function prime_index ( input ) { let n = input . length ; for ( let i = 2 ; i <= n ; i ++ ) if ( isPrime ( i ) ) document . write ( input [ i - 1 ] ) ; } let input = " " ; prime_index ( input ) ;
Check whether a given string is Heterogram or not | Javascript program to check whether the given string is Heterogram or not . ; Traversing the string . ; Ignore the space ; If already encountered ; Else return false . ; Driver code
function isHeterogram ( s , n ) { var hash = Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( s [ i ] != ' ' ) { if ( hash [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 0 ) hash [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] = 1 ; else return false ; } } return true ; } var s = " " ; var n = s . length ; ( isHeterogram ( s , n ) ) ? ( document . write ( " " ) ) : ( document . write ( " " ) ) ;
Print given sentence into its equivalent ASCII form | Function to compute the ASCII value of each character one by one ; Driver function
function ASCIISentence ( str ) { let l = str . length ; let convert ; for ( let i = 0 ; i < l ; i ++ ) { convert = str [ i ] . charCodeAt ( 0 ) ; document . write ( convert ) ; } } let str = " " ; document . write ( " " ) ; ASCIISentence ( str ) ;
Snake case of a given sentence | Function to replace spaces and convert into snake case ; Converting space to underscor ; If not space , convert into lower letacter ; Driver Code ; Calling function
function convert ( str ) { let n = str . length ; let str1 = " " ; for ( i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) str1 = str1 + ' ' ; else str1 = str1 + ( str [ i ] ) . toLowerCase ( ) ; } document . write ( str1 ) ; } let str = " " ; convert ( str ) ;
Find the size of largest subset of anagram words | Utility function to find size of largest subset of anagram ; sort the String ; Increment the count of String ; Compute the maximum size of String ; Driver code
function largestAnagramSet ( arr , n ) { var maxSize = 0 ; var count = new Map ( ) ; for ( var i = 0 ; i < n ; ++ i ) { var temp = arr [ i ] . split ( ' ' ) ; temp . sort ( ) ; arr [ i ] = temp . join ( ' ' ) ; if ( count . has ( arr [ i ] ) ) { count . set ( arr [ i ] , count . get ( arr [ i ] ) + 1 ) ; } else { count . set ( arr [ i ] , 1 ) ; } maxSize = Math . max ( maxSize , count . get ( arr [ i ] ) ) ; } return maxSize ; } var arr = [ " " , " " , " " , " " , " " ] ; var n = arr . length ; document . write ( largestAnagramSet ( arr , n ) + " " ) ; var arr1 = [ " " , " " , " " , " " ] ; n = arr1 . length ; document . write ( largestAnagramSet ( arr1 , n ) ) ;
Program to count vowels , consonant , digits and special characters in string . | Function to count number of vowels , consonant , digitsand special character in a string . ; Declare the variable vowels , consonant , digit and special characters ; str . length ( ) function to count number of character in given string . ; To handle upper case letters ; Driver function .
function countCharacterType ( str ) { var vowels = 0 , consonant = 0 , specialChar = 0 , digit = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) { var ch = str [ i ] ; if ( ( ch >= " " && ch <= " " ) || ( ch >= " " && ch <= " " ) ) { ch = ch . toLowerCase ( ) ; if ( ch == " " ch == " " ch == " " ch == " " ch == " " ) vowels ++ ; else consonant ++ ; } else if ( ch >= " " && ch <= " " ) digit ++ ; else specialChar ++ ; } document . write ( " " + vowels + " " ) ; document . write ( " " + consonant + " " ) ; document . write ( " " + digit + " " ) ; document . write ( " " + specialChar + " " ) ; } var str = " " ; countCharacterType ( str ) ;
Next word that does not contain a palindrome and has characters from first k | function to return lexicographically next word ; we made m as m + 97 that means our required string contains not more than m + 97 ( as per ASCII value ) in it . ; increment last alphabet to make next lexicographically next word . ; if i - th alphabet not in first k letters then make it as " a " and then increase ( i - 1 ) th letter ; to check whether formed string palindrome or not . ; increment i . ; if i less than or equals to one that means we not formed such word . ; Driver code for above function .
function findNextWord ( s , m ) { m += 97 ; var n = s . length ; var i = s . length - 1 ; s [ i ] = String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) + 1 ) ; while ( i >= 0 && i <= n - 1 ) { if ( s [ i ] . charCodeAt ( 0 ) >= m ) { s [ i ] = ' ' ; s [ i - 1 ] = String . fromCharCode ( s [ i - 1 ] . charCodeAt ( 0 ) + 1 ) ; i -- ; } else if ( s [ i ] == s [ i - 1 ] s [ i ] == s [ i - 2 ] ) s [ i ] = String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) + 1 ) ; else i ++ ; } if ( i <= - 1 ) document . write ( " " ) ; else document . write ( s . join ( ' ' ) ) ; } var str = " " . split ( ' ' ) ; var k = 4 ; findNextWord ( str , k ) ;
Replace a character c1 with c2 and c2 with c1 in a string S | Javascript program to replace c1 with c2 and c2 with c1 ; loop to traverse in the string ; check for c1 and replace ; check for c2 and replace ; Driver code
function replace ( s , c1 , c2 ) { let l = s . length ; let arr = s . split ( " " ) ; for ( let i = 0 ; i < l ; i ++ ) { if ( arr [ i ] == c1 ) arr [ i ] = c2 ; else if ( arr [ i ] == c2 ) arr [ i ] = c1 ; } return arr . join ( " " ) ; } let s = " " ; let c1 = ' ' , c2 = ' ' ; document . write ( replace ( s , c1 , c2 ) ) ;
Fibonacci Word | Returns n - th Fibonacci word ; driver program
function fibWord ( n ) { var Sn_1 = " " ; var Sn = " " ; var tmp ; for ( var i = 2 ; i <= n ; i ++ ) { tmp = Sn ; Sn += Sn_1 ; Sn_1 = tmp ; } return Sn ; } var n = 6 ; document . write ( fibWord ( n ) ) ;
Decimal to octal conversion with minimum use of arithmetic operators | function for decimal to binary conversion without using arithmetic operators ; to store the binary equivalent of decimal ; to get the last binary digit of the number ' n ' and accumulate it at the beginning of ' bin ' ; right shift ' n ' by 1 ; required binary number ; Function to find octal equivalent of binary ; add min 0 's in the beginning to make  string length divisible by 3 ; create map between binary and its equivalent octal code ; one by one extract from left , substring of size 3 and add its octal code ; required octal number ; function to find octal equivalent of decimal ; convert decimal to binary ; convert binary to octal required octal equivalent of decimal ; Driver program to test above
function decToBin ( n ) { if ( n == 0 ) return " " ; var bin = " " ; while ( n > 0 ) { bin = ( ( n & 1 ) == 0 ? ' ' : ' ' ) + bin ; n >>= 1 ; } return bin ; } function convertBinToOct ( bin ) { var l = bin . length ; for ( var i = 1 ; i <= ( 3 - l % 3 ) % 3 ; i ++ ) bin = ' ' + bin ; var bin_oct_map = new Map ( ) ; bin_oct_map . set ( " " , ' ' ) ; bin_oct_map . set ( " " , ' ' ) ; bin_oct_map . set ( " " , ' ' ) ; bin_oct_map . set ( " " , ' ' ) ; bin_oct_map . set ( " " , ' ' ) ; bin_oct_map . set ( " " , ' ' ) ; bin_oct_map . set ( " " , ' ' ) ; bin_oct_map . set ( " " , ' ' ) ; var i = 0 ; var octal = " " ; while ( 1 ) { octal += bin_oct_map . get ( bin . substring ( i , i + 3 ) ) ; i += 3 ; if ( i == bin . length ) break ; } return octal ; } function decToOctal ( n ) { var bin = decToBin ( n ) ; return convertBinToOct ( bin ) ; } var n = 151 ; document . write ( decToOctal ( n ) ) ; ing
Split the string into substrings using delimiter | function to split string into substrings on the basis of delimiter and return the substrings after split ; to count the number of split strings ; adding delimiter character at the end of ' str ' ; length of ' str ' ; traversing ' str ' from left to right ; if str [ i ] is not equal to the delimiter character then accumulate it to ' word ' ; if ' word ' is not an empty string , then add this ' word ' to the array ' substr _ list [ ] ' ; reset ' word ' ; return the splitted strings ; Driver program to test above
function splitStrings ( str , dl ) { var word = " " ; var num = 0 ; str = str + dl ; var l = str . length ; var substr_list = [ ] ; for ( var i = 0 ; i < l ; i ++ ) { if ( str [ i ] != dl ) word = word + str [ i ] ; else { if ( word . length != 0 ) substr_list . push ( word ) ; word = " " ; } } return substr_list ; } var str = " " ; var dl = ' ' ; var res = splitStrings ( str , dl ) ; res . forEach ( x => { document . write ( x + " " ) ; } ) ;
Construct lexicographically smallest palindrome | function for printing palindrome ; iterate till i < j ; continue if str [ i ] == str [ j ] ; update str [ i ] = str [ j ] = ' a ' if both are ' * ' ; update str [ i ] = str [ j ] if only str [ i ] = ' * ' ; update str [ j ] = str [ i ] if only str [ j ] = ' * ' ; else print not possible and return ; driver program
function constructPalin ( str , len ) { var i = 0 , j = len - 1 ; for ( ; i < j ; i ++ , j -- ) { if ( str [ i ] == str [ j ] && str [ i ] != ' ' ) continue ; else if ( str [ i ] == str [ j ] && str [ i ] == ' ' ) { str [ i ] = ' ' ; str [ j ] = ' ' ; continue ; } else if ( str [ i ] == ' ' ) { str [ i ] = str [ j ] ; continue ; } else if ( str [ j ] == ' ' ) { str [ j ] = str [ i ] ; continue ; } document . write ( " " ) ; return " " ; } return str . join ( " " ) ; } var str = " " . split ( " " ) ; var len = str . length ; document . write ( constructPalin ( str , len ) ) ;
Change string to a new character set | Converts str to given character set ; hashing for new character set ; conversion of new character set ; Driver code
function conversion ( charSet , str ) { var n = str . length ; var hashChar = Array ( 26 ) ; for ( var i = 0 ; i < 26 ; i ++ ) { var ch = Math . abs ( charSet [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; hashChar [ ch ] = String . fromCharCode ( ' ' . charCodeAt ( 0 ) + i ) ; } var s = " " ; for ( var i = 0 ; i < n ; i ++ ) s += ( hashChar [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ) ; return s ; } var charSet = " " . split ( ' ' ) ; var str = " " ; str = conversion ( charSet , str ) ; document . write ( str ) ;
Change string to a new character set | JavaScript program to change the sentence with virtual dictionary ; function for converting the string ; find the index of each element of the string in the modified set of alphabets replace the element with the one having the same index in the actual set of alphabets ; Driver Code
var alphabets = " " . split ( " " ) ; function conversion ( charSet , str1 ) { var s2 = " " ; str1 . forEach ( ( i ) => { s2 = s2 + alphabets [ charSet . indexOf ( i ) ] ; } ) ; return s2 ; } var charSet = " " ; var str1 = " " ; document . write ( conversion ( charSet , str1 . split ( " " ) ) ) ;
Different substrings in a string that start and end with given strings | function to return number of different sub - strings ; initially our answer is zero . ; find the length of given strings ; currently make array and initially put zero . ; find occurrence of " a " and " b " in string " s " ; We use a hash to make sure that same substring is not counted twice . ; go through all the positions to find occurrence of " a " first . ; if we found occurrence of " a " . ; then go through all the positions to find occurrence of " b " . ; if we do found " b " at index j then add it to already existed substring . ; if we found occurrence of " b " . ; now add string " b " to already existed substring . ; If current substring is not included already . ; put any non negative integer to make this string as already existed . ; make substring null . ; return answer . ; Driver Code
function numberOfDifferentSubstrings ( s , a , b ) { let ans = 0 ; let ls = s . length ; let x = new Array ( ls ) ; let y = new Array ( ls ) ; for ( let i = 0 ; i < ls ; i ++ ) { x [ i ] = 0 ; y [ i ] = 0 ; } for ( let i = 0 ; i < ls ; i ++ ) { if ( s [ i ] == a ) x [ i ] = 1 ; if ( s [ i ] == b ) y [ i ] = 1 ; } let hash = new Set ( ) ; let curr_substr = " " ; for ( let i = 0 ; i < ls ; i ++ ) { if ( x [ i ] != 0 ) { for ( let j = i ; j < ls ; j ++ ) { if ( y [ j ] == 0 ) curr_substr += s [ i ] ; if ( y [ j ] != 0 ) { curr_substr += s [ j ] ; if ( ! hash . has ( curr_substr ) ) ans ++ ; hash . add ( curr_substr ) ; } } curr_substr = " " ; } } return ans ; } let s = " " ; let begin = ' ' ; let end = ' ' ; document . write ( numberOfDifferentSubstrings ( s , begin , end ) ) ;