codeparrot/xlcost-text-to-code huggingface dataset:
Number of strings that satisfy the given condition | Function to return the count of valid strings ; Set to store indices of valid strings ; Find the maximum digit for current position ; Add indices of all the strings in the set that contain maximal digit ; Return number of strings in the set ; Driver code
function countStrings ( n , m , s ) { let ind = new Set ( ) ; for ( let j = 0 ; j < m ; j ++ ) { let mx = 0 ; for ( let i = 0 ; i < n ; i ++ ) mx = Math . max ( mx , ( s [ i ] [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) ; for ( let i = 0 ; i < n ; i ++ ) if ( s [ i ] [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) == mx ) ind . add ( i ) ; } return ind . size ; } let s = [ " " , " " , " " ] ; let m = s [ 0 ] . length ; let n = s . length ; document . write ( countStrings ( n , m , s ) ) ;
Lexicographically largest sub | Function to return the lexicographically largest sub - sequence of s ; Get the max character from the string ; Use all the occurrences of the current maximum character ; Repeat the steps for the remaining string ; Driver code
function getSubSeq ( s , n ) { var res = " " ; var cr = 0 ; while ( cr < n ) { var mx = s [ cr ] . charCodeAt ( 0 ) ; for ( var i = cr + 1 ; i < n ; i ++ ) mx = Math . max ( mx , s [ i ] . charCodeAt ( 0 ) ) ; var lst = cr ; for ( var i = cr ; i < n ; i ++ ) if ( s [ i ] . charCodeAt ( 0 ) == mx ) { res += s [ i ] ; lst = i ; } cr = lst + 1 ; } return res ; } var s = " " ; var n = s . length ; document . write ( getSubSeq ( s , n ) ) ;
Minimum replacements such that the difference between the index of the same characters is divisible by 3 | JavaScript program to find minimum replacements such that the difference between the index of the same characters is divisible by 3 Function to count the number of minimal replacements ; Max Integer Value ; Generate all permutations ; Count the replacements ; Return the replacements ; Driver Code
function countMinimalReplacements ( s ) { var n = s . length ; var mini = 2147483647 ; var dup = " " . split ( " " ) ; do { var dif = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( s [ i ] !== dup [ i % 3 ] ) { dif ++ ; } } mini = Math . min ( mini , dif ) ; } while ( next_permutation ( dup ) ) ; return mini ; } function next_permutation ( p ) { for ( var a = p . length - 2 ; a >= 0 ; -- a ) { if ( p [ a ] < p [ a + 1 ] ) { for ( var b = p . length - 1 ; ; -- b ) { if ( p [ b ] > p [ a ] ) { var t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } } } } return false ; } var s = " " ; document . write ( countMinimalReplacements ( s ) ) ;
Count of sub | Function to return the count of sub - strings of str that are divisible by k ; Take all sub - strings starting from i ; If current sub - string is divisible by k ; Return the required count ; Driver code
function countSubStr ( str , len , k ) { let count = 0 ; for ( let i = 0 ; i < len ; i ++ ) { let n = 0 ; for ( let j = i ; j < len ; j ++ ) { n = n * 10 + ( str [ j ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; if ( n % k == 0 ) count ++ ; } } return count ; } let str = " " ; let len = str . length ; let k = 11 ; document . write ( countSubStr ( str , len , k ) ) ;
Find the resulting Colour Combination | Function to return Colour Combination ; Check for B * G = Y ; Check for B * Y = G ; Check for Y * G = B ; Driver Code
function Colour_Combination ( s ) { let temp = s [ 0 ] ; for ( let i = 1 ; i < s . length ; i ++ ) { if ( temp != s [ i ] ) { if ( ( temp == ' ' temp == ' ' ) && ( s [ i ] == ' ' s [ i ] == ' ' ) ) temp = ' ' ; else if ( ( temp == ' ' temp == ' ' ) && ( s [ i ] == ' ' s [ i ] == ' ' ) ) temp = ' ' ; else temp = ' ' ; } } return temp ; } let s = " " ; document . write ( Colour_Combination ( s ) ) ;
Reverse Middle X Characters | Function to reverse the middle x characters in a string ; Find the position from where the characters have to be reversed ; Print the first n characters ; Print the middle x characters in reverse ; Print the last n characters ; Drived code
function reverse ( str , x ) { var n = ( str . length - x ) / 2 ; for ( i = 0 ; i < n ; i ++ ) document . write ( str . charAt ( i ) ) ; for ( i = n + x - 1 ; i >= n ; i -- ) document . write ( str . charAt ( i ) ) ; for ( i = n + x ; i < str . length ; i ++ ) document . write ( str . charAt ( i ) ) ; } var str = " " ; var x = 3 ; reverse ( str , x ) ;
Print all permutation of a string using ArrayList | Utility function to print the contents of the ArrayList ; Function to returns the arraylist which contains all the permutation of str ; If string is empty ; Return an empty arraylist ; Take first character of str ; Take sub - string starting from the second character ; Recurvise call ; Store the generated permutations into the resultant arraylist ; Return the resultant arraylist ; Driver code
function printArrayList ( arrL ) { for ( let i = 0 ; i < arrL . length ; i ++ ) document . write ( arrL [ i ] + " " ) ; } function getPermutation ( str ) { if ( str . length == 0 ) { let empty = [ ] ; empty . push ( " " ) ; return empty ; } let ch = str [ 0 ] ; let subStr = str . substring ( 1 ) ; let prevResult = getPermutation ( subStr ) ; let Res = [ ] ; for ( let val of prevResult ) { for ( let i = 0 ; i <= val . length ; i ++ ) { Res . push ( val . substring ( 0 , i ) + ch + val . substring ( i ) ) ; } } return Res ; } let str = " " ; printArrayList ( getPermutation ( str ) ) ;
Count distinct substrings that contain some characters at most k times | js implementation of the approach ; Function to return the count of valid sub - strings ; Store all characters of anotherStr in a direct index table for quick lookup . ; To store distinct output substrings ; Traverse through the given string and one by one generate substrings beginning from s [ i ] . ; One by one generate substrings ending with s [ j ] ; If character is illegal ; If current substring is valid ; If current substring is invalid , adding more characters would not help . ; Return the count of distinct sub - strings ; Driver code
let MAX_CHAR = 256 ; function countSubStrings ( s , anotherStr , k ) { let illegal = [ ] ; for ( let i = 0 ; i < 256 ; i ++ ) illegal . push ( false ) ; for ( let i = 0 ; i < anotherStr . length ; i ++ ) illegal [ anotherStr [ i ] ] = true ; let us = new Set ( ) ; for ( let i = 0 ; i < s . length ; ++ i ) { let ss = " " ; let count = 0 ; for ( let j = i ; j < s . length ; ++ j ) { if ( illegal [ s [ j ] ] ) ++ count ; ss = ss + s [ j ] ; if ( count <= k ) { us . add ( ss ) ; } else break ; } } return us . size ; } let str = " " ; let anotherStr = " " ; let k = 2 ; document . write ( countSubStrings ( str , anotherStr , k ) ) ;
Count pairs of parentheses sequences such that parentheses are balanced | Function to count the number of pairs ; Hashing function to count the opening and closing brackets ; Traverse for all bracket sequences ; Get the string ; Counts the opening and closing required ; Traverse in the string ; If it is a opening bracket ; Closing bracket ; If openings are there , then close it ; Else increase count of closing ; If requirements of openings are there and no closing ; If requirements of closing are there and no opening ; Perfect ; Divide by two since two perfect makes one pair ; Traverse in the open and find corresponding minimum ; Driver Code
function countPairs ( bracks , num ) { let open = new Map ( ) ; let close = new Map ( ) ; let cnt = 0 ; for ( let i = 0 ; i < num ; i ++ ) { let s = bracks [ i ] ; let l = s . length ; let op = 0 , cl = 0 ; for ( let j = 0 ; j < l ; j ++ ) { if ( s [ j ] == ' ' ) op ++ ; else { if ( op != 0 ) op -- ; else cl ++ ; } } if ( op != 0 && cl == 0 ) open . set ( op , open . get ( op ) == null ? 1 : open . get ( op ) + 1 ) ; if ( cl != 0 && op == 0 ) close . set ( cl , close . get ( cl ) == null ? 1 : close . get ( cl ) + 1 ) ; if ( op == 0 && cl == 0 ) cnt ++ ; } cnt /= 2 ; for ( let [ key , value ] of open . entries ( ) ) cnt += Math . min ( value , close . get ( value ) ) ; return cnt ; } let bracks = [ " " , " " , " " , " " , " " , " " , " " ] ; let num = bracks . length ; document . write ( countPairs ( bracks , num ) ) ;
Decrypt a string according to given rules | Function to return the original string after decryption ; Stores the decrypted string ; If length is odd ; Step counter ; Starting and ending index ; Iterate till all characters are decrypted ; Even step ; Odd step ; If length is even ; Step counter ; Starting and ending index ; Even step ; Odd step ; Reverse the decrypted string ; Driver code
function decrypt ( s , l ) { var ans = " " ; if ( l % 2 == 1 ) { var cnt = 0 ; var indl = 0 , indr = l - 1 ; while ( ans . length != l ) { if ( cnt % 2 == 0 ) ans += s . charAt ( indl ++ ) ; else ans += s . charAt ( indr -- ) ; cnt ++ ; } } else { var cnt = 0 ; var indl = 0 , indr = l - 1 ; while ( ans . length != l ) { if ( cnt % 2 == 0 ) ans += s . charAt ( indr -- ) ; else ans += s . charAt ( indl ++ ) ; cnt ++ ; } } return ans . split ( ' ' ) . reverse ( ) . join ( ' ' ) ; } var s = " " ; var l = s . length ; document . write ( decrypt ( s , l ) ) ;
Remove consecutive alphabets which are in same case | Function to return the modified string ; Traverse through the remaining characters in the string ; If the current and the previous characters are not in the same case then take the character ; Driver code
function removeChars ( s ) { let modifiedStr = " " ; modifiedStr += s [ 0 ] ; for ( let i = 1 ; i < s . length ; i ++ ) { if ( s [ i ] == ( s [ i ] ) . toUpperCase ( ) && ( s [ i - 1 ] ) == ( s [ i - 1 ] ) . toLowerCase ( ) || s [ i ] == s [ i ] . toLowerCase ( ) && ( s [ i - 1 ] ) == ( s [ i - 1 ] ) . toUpperCase ( ) ) modifiedStr += s [ i ] ; } return modifiedStr ; } let s = " " ; document . write ( removeChars ( s ) ) ;
Cost to make a string Panagram | Function to return the total cost required to make the string Pangram ; Mark all the alphabets that occurred in the string ; Calculate the total cost for the missing alphabets ; Driver Code
function pangramCost ( arr , str ) { var cost = 0 ; var occurred = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) occurred [ i ] = false ; for ( let i = 0 ; i < str . length ; i ++ ) occurred [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] = true ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( occurred [ i ] == false ) cost += arr [ i ] ; } return cost ; } var arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 ] ; var str = " " ; document . write ( pangramCost ( arr , str ) ) ;
Recursive program to insert a star between pair of identical characters | Recursive Javascript program to insert * between two consecutive same characters . ; Function to insert * at desired position ; Append current character ; If we reached last character ; If next character is same , append ' * ' ; Driver code
let output = " " ; function pairStar ( input , i ) { output = output + input [ i ] ; if ( i == input . length - 1 ) return ; if ( input [ i ] == input [ i + 1 ] ) output = output + ' ' ; pairStar ( input , i + 1 ) ; } let input = " " ; pairStar ( input , 0 ) ; document . write ( output ) ;
Maximum number of removals of given subsequence from a string | Function to return max possible operation of the given type that can be performed on str ; Increment count of sub - sequence ' g ' ; Increment count of sub - sequence ' gk ' if ' g ' is available ; Increment count of sub - sequence ' gks ' if sub - sequence ' gk ' appeared previously ; Return the count of sub - sequence ' gks ' ; Driver code
function maxOperations ( str ) { let i , g , gk , gks ; i = g = gk = gks = 0 ; for ( i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == ' ' ) { g ++ ; } else if ( str [ i ] == ' ' ) { if ( g > 0 ) { g -- ; gk ++ ; } } else if ( str [ i ] == ' ' ) { if ( gk > 0 ) { gk -- ; gks ++ ; } } } return gks ; } let a = " " ; document . write ( maxOperations ( a ) ) ;
Split a string in equal parts such that all parts are palindromes | Function to return the frequency array for the given string ; Function to return the required count ; Add frequencies of the even appearing characters ; Count of the characters that appeared odd number of times ; If there are no characters with odd frequency ; If there are no characters with even frequency ; Only a single character with odd frequency ; More than 1 character with odd frequency string isn 't a palindrome ; All odd appearing characters can also contribute to the even length palindrome if one character is removed from the frequency leaving it as even ; If k palindromes are possible where k is the number of characters with odd frequency ; Current character can no longer be an element in a string other than the mid character ; If current character has odd frequency > 1 take two characters which can be used in any of the parts ; Update the frequency ; If not possible , then every character of the string will act as a separate palindrome ; Driver code
function getFrequencies ( str ) { let freq = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) freq [ i ] = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { freq [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } return freq ; } function countMinParts ( str ) { let n = str . length ; let freq = getFrequencies ( str ) ; let oddFreq = [ ] ; let evenFreq = [ ] ; let i , sumEven = 0 ; for ( i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) continue ; if ( freq [ i ] % 2 == 0 ) evenFreq . push ( freq [ i ] ) ; else oddFreq . push ( freq [ i ] ) ; } for ( i = 0 ; i < evenFreq . length ; i ++ ) { sumEven += evenFreq [ i ] ; } if ( oddFreq . length == 0 ) return 1 ; if ( sumEven == 0 ) { if ( oddFreq . length == 1 ) return 1 ; return 0 ; } i = 0 ; while ( i < oddFreq . length ) { if ( ( sumEven / 2 ) % oddFreq . length == 0 ) return oddFreq . length ; if ( oddFreq [ i ] == 1 ) { i ++ ; continue ; } sumEven += 2 ; oddFreq [ i ] = oddFreq [ i ] - 2 ; } return n ; } let s = " " ; document . write ( countMinParts ( s ) ) ;
Substring Reverse Pattern | Function to print the required pattern ; Print the unmodified string ; Reverse the string ; Replace the first and last character by ' * ' then second and second last character and so on until the string has characters remaining ; Driver code
function printPattern ( s , n ) { document . write ( s . join ( ' ' ) + " " ) ; var i = 0 , j = n - 1 ; while ( i < j ) { var c = s [ i ] ; s [ i ] = s [ j ] ; s [ j ] = c ; i ++ ; j -- ; } i = 0 ; j = n - 1 ; while ( j - i > 1 ) { s [ i ] = s [ j ] = ' ' ; document . write ( s . join ( ' ' ) + " " ) ; i ++ ; j -- ; } } var s = " " . split ( ' ' ) ; var n = s . length ; printPattern ( s , n ) ;
Check if the string satisfies the given condition | Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if c is a vowel ; Function that returns true if the count of vowels in word is prime ; If count of vowels is prime ; Driver code
function prime ( n ) { if ( n <= 1 ) { return false ; } if ( n <= 3 ) { return true ; } if ( n % 2 == 0 n % 3 == 0 ) { return false ; } for ( let i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } function isVowel ( c ) { c = c . toLowerCase ( ) ; if ( c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' ) { return true ; } return false ; } function isValidString ( word ) { let cnt = 0 ; for ( let i = 0 ; i < word . length ; i ++ ) { if ( isVowel ( word [ i ] ) ) { cnt ++ ; } } if ( prime ( cnt ) ) { return true ; } else { return false ; } } let s = " " ; if ( isValidString ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
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 suff = " " ; for ( var i = str . length - 1 ; i >= 0 ; i -- ) { suff = suff + str [ i ] ; document . write ( suff ) ; } } var str = " " ; printExpansion ( str ) ;
Solve the Logical Expression given by string | Function to evaluate the logical expression ; traversing string from the end . ; for NOT operation ; for AND and OR operation ; Driver code
function logicalExpressionEvaluation ( str ) { let arr = [ ] ; for ( let i = str . length - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' ' ) { let s = [ ] ; while ( arr [ arr . length - 1 ] != ' ' ) { s . push ( arr [ arr . length - 1 ] ) ; arr . pop ( ) ; } arr . pop ( ) ; if ( s . length == 3 ) { arr . push ( s [ 2 ] == ' ' ? ' ' : ' ' ) ; } else if ( s . length == 5 ) { let a = s [ 0 ] . charCodeAt ( 0 ) - 48 , b = s [ 4 ] . charCodeAt ( 0 ) - 48 , c ; if ( s [ 2 ] == ' ' ) { c = a & b ; } else { c = a | b ; } arr . push ( String . fromCharCode ( c + 48 ) ) ; } } else { arr . push ( str [ i ] ) ; } } return arr [ arr . length - 1 ] ; } let str = " " ; document . write ( logicalExpressionEvaluation ( str ) ) ;
Find number of substrings of length k whose sum of ASCII value of characters is divisible by k | JavaScript program to find number of substrings of length k whose sum of ASCII value of characters is divisible by k ; Finding length of string ; Finding sum of ASCII value of first substring ; Using sliding window technique to find sum of ASCII value of rest of the substring ; checking if sum is divisible by k ; Driver code
function count ( s , k ) { var n = s . length ; var d = 0 , i ; var count = 0 ; for ( i = 0 ; i < n ; i ++ ) d += s [ i ] . charCodeAt ( 0 ) ; if ( d % k === 0 ) { count += 1 ; } for ( i = k ; i < n ; i ++ ) { var prev = s [ i - k ] ; d -= prev . charCodeAt ( 0 ) ; d += s [ i ] . charCodeAt ( 0 ) ; if ( d % k === 0 ) count += 1 ; } return count ; } var s = " " ; var k = 3 ; var ans = count ( s , k ) ; document . write ( ans ) ;
Final string after performing given operations | Function to return the modified string ; Count number of ' x ' ; Count number of ' y ' ; min ( x , y ) number of ' x ' and ' y ' will be deleted ; Driver Program to test above function
function printFinalString ( s ) { var i , n ; n = s . length ; var x = 0 , y = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) x ++ ; else y ++ ; } var finalString = " " ; if ( x > y ) for ( i = 0 ; i < x - y ; i ++ ) finalString += " " ; else for ( i = 0 ; i < y - x ; i ++ ) finalString += " " ; return finalString ; } var s = " " ; document . write ( printFinalString ( s ) ) ;
String which when repeated exactly K times gives a permutation of S | Function to return a string which when repeated exactly k times gives a permutation of s ; size of string ; to frequency of each character ; get frequency of each character ; to store final answer ; check if frequency is divisible by k ; add to answer ; if frequency is not divisible by k ; Driver code ; function call
function K_String ( s , k ) { let n = s . length ; let fre = new Array ( 26 ) ; for ( let i = 0 ; i < fre . length ; i ++ ) { fre [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) fre [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; let str = " " ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( fre [ i ] % k == 0 ) { let x = Math . floor ( fre [ i ] / k ) ; while ( x != 0 ) { str += String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; x -- ; } } else { return " " ; } } return str ; } let s = " " ; let k = 2 ; document . write ( K_String ( s , k ) ) ;
Minimum deletions from string to reduce it to string with at most 2 unique characters | Function to find the minimum deletions ; Array to store the occurrences of each characters ; Length of the string ; ASCII of the character ; Increasing the frequency for this character ; Choosing two character ; Finding the minimum deletion ; Driver code
function check ( s ) { var i , j ; var fr = Array ( 26 ) . fill ( 0 ) ; var n = s . length ; for ( i = 0 ; i < n ; i ++ ) { var x = s [ i ] ; fr [ x . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] += 1 ; } var minimum = 10000000000 ; for ( i = 0 ; i < 26 ; i ++ ) { for ( j = i + 1 ; j < 26 ; j ++ ) { var z = fr [ i ] + fr [ j ] ; minimum = Math . min ( minimum , n - z ) ; } } return minimum ; } var s = " " ; document . write ( check ( s ) ) ;
Count and Print the alphabets having ASCII value not in the range [ l , r ] | Function to count the number of characters whose ascii value not in range [ l , r ] ; Initializing the count to 0 ; using map to print a character only once ; Increment the count if the value is less ; return the count ; Driver code
function CountCharacters ( str , l , r ) { let cnt = 0 ; let m = new Map ( ) ; let len = str . length ; for ( let i = 0 ; i < len ; i ++ ) { if ( ! ( l <= str [ i ] . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= r ) ) { cnt ++ ; if ( ! m . has ( str [ i ] ) ) { m . set ( str [ i ] , 0 ) ; document . write ( str [ i ] + " " ) ; } m . set ( str [ i ] , m . get ( str [ i ] + 1 ) ) ; } } return cnt ; } let str = " " ; let l = 102 , r = 111 ; document . write ( " " + " " ) ; document . write ( " " + CountCharacters ( str , l , r ) ) ;
Count the number of words having sum of ASCII values less than and greater than k | Function to count the words ; Sum of ascii values ; Number of words having sum of ascii less than k ; If character is a space ; Add the ascii value to sum ; Handling the Last word separately ; Driver code
function CountWords ( str , k ) { let sum = 0 ; let NumberOfWords = 0 ; let counter = 0 ; let len = str . length ; for ( let i = 0 ; i < len ; ++ i ) { if ( str [ i ] == " " ) { if ( sum < k ) { counter ++ ; } sum = 0 ; NumberOfWords ++ ; } else { sum += str . charCodeAt ( i ) ; } } NumberOfWords ++ ; if ( sum < k ) { counter ++ ; } document . write ( " " + " " + counter + " " ) ; document . write ( " " + " " + ( NumberOfWords - counter ) + " " ) ; } let str = " " ; let k = 400 ; CountWords ( str , k ) ;
Rearrange a string in the form of integer sum followed by the minimized character | function to return maximum volume ; separate digits and alphabets ; change digit sum to string ; change alphabet sum to string ; concatenate sum to alphabets string ; Driver code
function separateChar ( str ) { var n = str . length , digitSum = 0 ; var alphabetSum = 0 , j = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] >= ' ' && str [ i ] <= ' ' ) digitSum += ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; else { alphabetSum += ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 1 ) ; alphabetSum %= 26 ; } } var sumStr = digitSum . toString ( ) ; var alphabetStr = String . fromCharCode ( alphabetSum + ' ' . charCodeAt ( 0 ) - 1 ) ; sumStr += alphabetStr ; return sumStr ; } var str = " " ; document . write ( separateChar ( str ) ) ;
Find the count of palindromic sub | Javascript program to find the count of palindromic sub - string of a string in it 's ascending form ; function to return count of palindromic sub - string ; calculate frequency ; calculate count of palindromic sub - string ; return result ; driver program
var MAX_CHAR = 26 ; function countPalindrome ( str ) { var n = str . length ; var sum = 0 ; var hashTable = Array ( MAX_CHAR ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) hashTable [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( var i = 0 ; i < 26 ; i ++ ) { if ( hashTable [ i ] ) sum += ( hashTable [ i ] * ( hashTable [ i ] + 1 ) / 2 ) ; } return sum ; } var str = " " ; document . write ( countPalindrome ( str ) ) ;
Minimum number of elements to be removed so that pairwise consecutive elements are same | Function to count the minimum number of elements to remove from a number so that pairwise two consecutive digits are same . ; initialize counting variable ; check if two consecutive digits are same ; Driver code
function countConsecutive ( s ) { let count = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) i ++ ; else count ++ ; } return count ; } let str = " " ; document . write ( countConsecutive ( str ) ) ;