codeparrot/xlcost-text-to-code huggingface dataset:
Find winner of an election where votes are represented as candidate names | We have four Candidates with name as ' John ' , ' Johnny ' , ' jamie ' , ' jackie ' . The votes in String array are as per the votes casted . Print the name of candidates received Max vote . ; Insert all votes in a hashmap ; Traverse through map to find the candidate with maximum votes . ; If there is a tie , pick lexicographically smaller . ; Driver code
function findWinner ( votes ) { let map = new Map ( ) ; for ( let i = 0 ; i < votes . length ; i ++ ) { if ( map . has ( votes [ i ] ) ) { map . set ( votes [ i ] , map . get ( votes [ i ] ) + 1 ) ; } else map . set ( votes [ i ] , 1 ) ; } let maxValueInMap = 0 ; let winner = " " ; for ( let [ key , value ] of map . entries ( ) ) { let Key = key ; let val = value ; if ( val > maxValueInMap ) { maxValueInMap = val ; winner = key ; } else if ( val == maxValueInMap && winner > Key ) winner = Key ; } document . write ( winner ) ; } let votes = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; findWinner ( votes ) ;
Luhn algorithm | Returns true if given card number is valid ; We add two digits to handle cases that make two digits after doubling ; Driver code
function checkLuhn ( cardNo ) { let nDigits = cardNo . length ; let nSum = 0 ; let isSecond = false ; for ( let i = nDigits - 1 ; i >= 0 ; i -- ) { let d = cardNo [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; if ( isSecond == true ) d = d * 2 ; nSum += parseInt ( d / 10 , 10 ) ; nSum += d % 10 ; isSecond = ! isSecond ; } return ( nSum % 10 == 0 ) ; } let cardNo = " " ; if ( checkLuhn ( cardNo ) ) document . write ( " " ) ; else document . write ( " " ) ;
Distributing all balls without repetition | Javascript program to find if its possible to distribute balls without repitiion ; function to find if its possible to distribute balls or not ; count array to count how many times each color has occurred ; increasing count of each color every time it appears ; to check if any color appears more than K times if it does we will print NO ; Driver code
let MAX_CHAR = 26 ; function distributingBalls ( k , n , str ) { let a = new Array ( MAX_CHAR ) ; a . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { a [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; } for ( let i = 0 ; i < MAX_CHAR ; i ++ ) if ( a [ i ] > k ) return false ; return true ; } let n = 6 , k = 3 ; let str = " " ; if ( distributingBalls ( k , n , str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find substrings that contain all vowels | Returns true if x is vowel . ; Function to check whether a character is vowel or not ; Outer loop picks starting character and inner loop picks ending character . ; If current character is not vowel , then no more result substrings possible starting from str [ i ] . ; If vowel , then we insert it in 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 n = str . length ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i ; j < n ; j ++ ) { if ( isVowel ( str [ j ] ) == false ) break ; hash . add ( str [ j ] ) ; if ( hash . size == 5 ) document . write ( str . substring ( i , j + 1 ) + " " ) ; } hash . clear ( ) ; } } let str = " " ; findSubstring ( str ) ;
Find if an array contains a string with one mismatch | Javascript program to find if given string is present with one mismatch . ; If the array is empty ; If sizes are same ; If first mismatch ; Second mismatch ; Driver code
function check ( list , s ) { let n = list . length ; if ( n == 0 ) { return false ; } for ( let i = 0 ; i < n ; i ++ ) { if ( list [ i ] . length != s . length ) { continue ; } let diff = false ; for ( let j = 0 ; j < list [ i ] . length ; j ++ ) { if ( list [ i ] [ j ] != s [ j ] ) { if ( ! diff ) { diff = true ; } else { diff = false ; break ; } } } if ( diff ) { return true ; } } return false ; } let s = [ ] ; s . push ( " " ) ; s . push ( " " ) ; s . push ( " " ) ; s . push ( " " ) ; s . push ( " " ) ; document . write ( check ( s , " " ) == true ? 1 : 0 ) ;
Sentence Palindrome ( Palindrome after removing spaces , dots , . . etc ) | To check sentence is palindrome or not ; Lowercase string ; Compares character until they are equal ; If there is another symbol in left of sentence ; If there is another symbol in right of sentence ; If characters are equal ; If characters are not equal then sentence is not palindrome ; Returns true if sentence is palindrome ; Driver program to test sentencePallindrome ( )
function sentencePalindrome ( str ) { let l = 0 ; let h = str . length - 1 ; str = str . toLowerCase ( ) ; while ( l <= h ) { let getAtl = str [ l ] ; let getAth = str [ h ] ; if ( ! ( getAtl >= ' ' && getAtl <= ' ' ) ) l ++ ; else if ( ! ( getAth >= ' ' && getAth <= ' ' ) ) h -- ; else if ( getAtl == getAth ) { l ++ ; h -- ; } else return false ; } return true ; } let str = " " ; if ( sentencePalindrome ( str ) ) document . write ( " " ) ; else document . write ( " " + " " + " " ) ;
Ways to remove one element from a binary string so that XOR becomes zero | Returns number of ways in which XOR become ZERO by remove 1 element ; Counting number of 0 and 1 ; If count of ones is even then return count of zero else count of one ; Driver Code
function xorZero ( s ) { let one_count = 0 , zero_count = 0 ; let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) if ( s [ i ] == ' ' ) one_count ++ ; else zero_count ++ ; if ( one_count % 2 == 0 ) return zero_count ; return one_count ; } let s = " " ; document . write ( xorZero ( s ) ) ;
Keyword Cipher | Function generates the encoded text ; This array represents the 26 letters of alphabets ; This loop inserts the keyword at the start of the encoded string ; To check whether the character is inserted earlier in the encoded string or not ; This loop inserts the remaining characters in the encoded string . ; Function that generates encodes ( cipher ) the message ; This loop ciphered the message . Spaces , special characters and numbers remain same . ; Hold the Keyword ; Function call to generate encoded text ; Message that need to encode ; Function call to print ciphered text
function encoder ( key ) { let encoded = " " ; let arr = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { arr [ i ] = false ; } for ( let i = 0 ; i < key . length ; i ++ ) { if ( key [ i ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && key [ i ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) { if ( arr [ key [ i ] . charCodeAt ( 0 ) - 65 ] == false ) { encoded += ( key [ i ] ) ; arr [ key [ i ] . charCodeAt ( 0 ) - 65 ] = true ; } } else if ( key [ i ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && key [ i ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) { if ( arr [ key [ i ] . charCodeAt ( 0 ) - 97 ] == false ) { encoded += String . fromCharCode ( key [ i ] . charCodeAt ( 0 ) - 32 ) ; arr [ key [ i ] . charCodeAt ( 0 ) - 97 ] = true ; } } } for ( let i = 0 ; i < 26 ; i ++ ) { if ( arr [ i ] == false ) { arr [ i ] = true ; encoded += String . fromCharCode ( i + 65 ) ; } } return encoded ; } function cipheredIt ( msg , encoded ) { let cipher = " " ; for ( let i = 0 ; i < msg . length ; i ++ ) { if ( msg [ i ] >= ' ' && msg [ i ] <= ' ' ) { let pos = msg [ i ] . charCodeAt ( 0 ) - 97 ; cipher += encoded [ pos ] ; } else if ( msg [ i ] >= ' ' && msg [ i ] <= ' ' ) { let pos = msg [ i ] . charCodeAt ( 0 ) - 65 ; cipher += encoded [ pos ] ; } else { cipher += msg [ i ] ; } } return cipher ; } let key ; key = " " ; document . write ( " " + key + " " ) ; let encoded = encoder ( key . split ( " " ) ) ; let message = " " ; document . write ( " " + message + " " ) ; document . write ( " " + cipheredIt ( message , encoded ) ) ;
Check if both halves of the string have same set of characters | Javascript program to check if it two half of string contain same Character set or not ; Function to check both halves for equality ; Counter array initialized with 0 ; Length of the string ; Traverse till the middle element is reached ; First half ; Second half ; Checking if values are different set flag to 1 ; String to be checked
let MAX_CHAR = 26 ; function checkCorrectOrNot ( s ) { let count = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < count . length ; i ++ ) { count [ i ] = 0 ; } let n = s . length ; if ( n == 1 ) return true ; for ( let i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { count [ s [ i ] - ' ' ] ++ ; count [ s [ j ] - ' ' ] -- ; } for ( let i = 0 ; i < MAX_CHAR ; i ++ ) if ( count [ i ] != 0 ) return false ; return true ; } let s = " " ; if ( checkCorrectOrNot ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if a string is Isogram or not | function to check isogram ; loop to store count of chars and check if it is greater than 1 ; if count > 1 , return false ; Driver code ; checking str as isogram ; checking str2 as isogram
function check_isogram ( str ) { let length = str . length ; let mapHash = new Array ( 26 ) ; mapHash . fill ( 0 ) ; for ( let i = 0 ; i < length ; i ++ ) { mapHash [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; if ( mapHash [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] > 1 ) { return false ; } } return true ; } let str = " " ; let str2 = " " ; if ( check_isogram ( str ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; if ( check_isogram ( str2 ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Check if a binary string has a 0 between 1 s or not | Set 1 ( General approach ) | Method returns 1 when string is valid else returns 0 ; Find first occurrence of 1 in s [ ] ; Find last occurrence of 1 in s [ ] ; Check if there is any 0 in range ; Driver method
function checkString ( s ) { let len = s . length ; let first = 0 ; for ( let i = 0 ; i < len ; i ++ ) { if ( s [ i ] == ' ' ) { first = i ; break ; } } let last = 0 ; for ( let i = len - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == ' ' ) { last = i ; break ; } } for ( let i = first ; i <= last ; i ++ ) if ( s [ i ] == ' ' ) return false ; return true ; } let s = " " ; document . write ( checkString ( s ) ? " " : " " ) ;
Program to print all substrings of a given string | Function to print all ( n * ( n + 1 ) ) / 2 subStrings of a given String s of length n . ; Fix start index in outer loop . Reveal new character in inner loop till end of String . Print till - now - formed String . ; Driver code
function printAllSubStrings ( s , n ) { for ( let i = 0 ; i < n ; i ++ ) { let temp = new Array ( n - i + 1 ) ; let tempindex = 0 ; for ( let j = i ; j < n ; j ++ ) { temp [ tempindex ++ ] = s [ j ] ; temp [ tempindex ] = ' \0 ' ; document . write ( temp . join ( " " ) + " " ) ; } } } let s = " " ; printAllSubStrings ( s , s . length ) ;
Reverse a string preserving space positions | Javascript program to implement the above approach ; Initialize two pointers as two corners ; Move both pointers toward each other ; If character at start or end is space , ignore it ; If both are not spaces , do swap ; Driver code
function preserveSpace ( str ) { let n = str . length ; let start = 0 ; let end = n - 1 ; let Str = str . split ( ' ' ) ; while ( start < end ) { if ( Str [ start ] == ' ' ) { start ++ ; continue ; } else if ( Str [ end ] == ' ' ) { end -- ; continue ; } else { let temp = Str [ start ] ; Str [ start ] = Str [ end ] ; Str [ end ] = temp ; start ++ ; end -- ; } } document . write ( Str . join ( " " ) ) ; } let str = " " ; preserveSpace ( str ) ;
Put spaces between words starting with capital letters | Function to amend the sentence ; Traverse the string ; Convert to lowercase if its an uppercase character ; Print space before it if its an uppercase character ; Print the character ; if lowercase character then just print ; Driver Code
function amendSentence ( sstr ) { let str = sstr . split ( ' ' ) ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) { str [ i ] = String . fromCharCode ( str [ i ] . charCodeAt ( ) + 32 ) ; if ( i != 0 ) document . write ( " " ) ; document . write ( str [ i ] ) ; } else document . write ( str [ i ] ) ; } } let str = " " ; amendSentence ( str ) ;
C ++ program to concatenate a string given number of times | Function which return string by concatenating it . ; Copying given string to temporary string . ; s += s1 ; Concatenating strings ; Driver code
function repeat ( s , n ) { let s1 = s ; for ( let i = 1 ; i < n ; i ++ ) return s ; } let s = " " ; let n = 3 ; document . write ( repeat ( s , n ) ) ;
Number of distinct permutation a String can have | Javascript program to find number of distinct permutations of a string . ; Utility function to find factorial of n . ; Returns count of distinct permutations of str . ; finding frequency of all the lower case alphabet and storing them in array of integer ; finding factorial of number of appearances and multiplying them since they are repeating alphabets ; finding factorial of size of string and dividing it by factorial found after multiplying ; Driver code
let MAX_CHAR = 26 ; function factorial ( n ) { let fact = 1 ; for ( let i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } function countDistinctPermutations ( str ) { let length = str . length ; let freq = new Array ( MAX_CHAR ) ; freq . fill ( 0 ) ; for ( let i = 0 ; i < length ; i ++ ) if ( str [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) ) freq [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; let fact = 1 ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) fact = fact * factorial ( freq [ i ] ) ; return parseInt ( factorial ( length ) / fact , 10 ) ; } let str = " " ; document . write ( countDistinctPermutations ( str ) ) ;
Determine if a string has all Unique Characters | Javascript program to illustrate String with unique characters without using any data structure ; Assuming string can have characters a - z this has 32 bits set to 0 ; if that bit is already set in checker , return false ; otherwise update and continue by setting that bit in the checker ; no duplicates encountered , return true ; Driver Code
function uniqueCharacters ( str ) { let checker = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { let bitAtIndex = str [ i ] - ' ' ; if ( ( checker & ( 1 << bitAtIndex ) ) > 0 ) { return false ; } checker = checker | ( 1 << bitAtIndex ) ; } return true ; } let input = " " ; if ( uniqueCharacters ( input ) ) { document . write ( " " + input + " " ) ; } else { document . write ( " " + input + " " ) ; }
Check whether K | Javascript program to check if k - th bit of a given number is set or not . ; Driver code
function isKthBitSet ( n , k ) { if ( ( n & ( 1 << ( k - 1 ) ) ) > 0 ) document . write ( " " ) ; else document . write ( " " ) ; } let n = 5 , k = 1 ; isKthBitSet ( n , k ) ;
Reverse string without using any temporary variable | Reversing a string using reverse ( ) ; Reverse str [ beign . . end ]
function reverseString ( str ) { return str . split ( " " ) . reverse ( ) . join ( " " ) ; } var str = ( " " ) ; document . write ( reverseString ( str ) ) ;
Recursive function to check if a string is palindrome | A recursive function that check a str ( s . . e ) is palindrome or not . ; If there is only one character ; If first and last characters do not match ; If there are more than two characters , check if middle substring is also palindrome or not . ; An empty string is considered as palindrome ; Driver Code
function isPalRec ( str , s , e ) { if ( s == e ) return true ; if ( ( str . charAt ( s ) ) != ( str . charAt ( e ) ) ) return false ; if ( s < e + 1 ) return isPalRec ( str , s + 1 , e - 1 ) ; return true ; } function isPalindrome ( str ) { var n = str . length ; if ( n == 0 ) return true ; return isPalRec ( str , 0 , n - 1 ) ; } var str = " " ; if ( isPalindrome ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Count substrings with same first and last characters | assuming lower case only ; Calculating frequency of each character in the string . ; Computing result using counts ; Driver function
var MAX_CHAR = 26 ; function countSubstringWithEqualEnds ( s ) { var result = 0 ; var n = s . length ; var count = Array . from ( { length : MAX_CHAR } , ( _ , i ) => 0 ) ; for ( var i = 0 ; i < n ; i ++ ) count [ s . charAt ( i ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( var i = 0 ; i < MAX_CHAR ; i ++ ) result += ( count [ i ] * ( count [ i ] + 1 ) / 2 ) ; return result ; } var s = " " ; document . write ( countSubstringWithEqualEnds ( s ) ) ;
Maximum consecutive repeating character in string | function to find out the maximum repeating character in given string ; Traverse string except last character ; If current character matches with next ; If doesn 't match, update result  (if required) and reset count ; Driver code
function maxRepeating ( str ) { var n = str . length ; var count = 0 ; var res = str [ 0 ] ; var cur_count = 1 ; for ( var i = 0 ; i < n ; i ++ ) { if ( i < n - 1 && str [ i ] == str [ i + 1 ] ) cur_count ++ ; else { if ( cur_count > count ) { count = cur_count ; res = str [ i ] ; } cur_count = 1 ; } } return res ; } var str = " " ; document . write ( maxRepeating ( str ) ) ;
Queries on subsequence of string | Javascript program to answer subsequence queries for a given string . ; Precompute the position of each character from each position of String S ; Computing position of each character from each position of String S ; Print " Yes " if T is subsequence of S , else " No " ; Traversing the string T ; If next position is greater than length of S set flag to false . ; Setting position of next character ; Driver Code
let MAX = 10000 ; let CHAR_SIZE = 26 ; function precompute ( mat , str , len ) { for ( let i = 0 ; i < CHAR_SIZE ; ++ i ) mat [ len ] [ i ] = len ; for ( let i = len - 1 ; i >= 0 ; -- i ) { for ( let j = 0 ; j < CHAR_SIZE ; ++ j ) mat [ i ] [ j ] = mat [ i + 1 ] [ j ] ; mat [ i ] [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] = i ; } } function query ( mat , str , len ) { let pos = 0 ; for ( let i = 0 ; i < str . length ; ++ i ) { if ( mat [ pos ] [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] >= len ) return false ; else pos = mat [ pos ] [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] + 1 ; } return true ; } let S = " " ; let len = S . length ; let mat = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { mat [ i ] = new Array ( CHAR_SIZE ) ; for ( let j = 0 ; j < CHAR_SIZE ; j ++ ) { mat [ i ] [ j ] = 0 ; } } precompute ( mat , S , len ) ; let get = query ( mat , " " , len ) ? " " : " " ; document . write ( get + " " ) ; get = query ( mat , " " , len ) ? " " : " " ; document . write ( get + " " ) ; get = query ( mat , " " , len ) ? " " : " " ; document . write ( get + " " ) ; get = query ( mat , " " , len ) ? " " : " " ; document . write ( get + " " ) ;
Queries for characters in a repeated string | Print whether index i and j have same element or not ; Finding relative position of index i , j . ; Checking is element are same at index i , j ; Driver Code
function query ( s , i , j ) { let n = s . length ; i %= n ; j %= n ; if ( s [ i ] == s [ j ] ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } let X = " " ; query ( X , 0 , 8 ) ; query ( X , 8 , 13 ) ; query ( X , 6 , 15 ) ;
Count of character pairs at same distance as in English alphabets | Method to count pairs ; Increment count if characters are at same distance ; Driver code
function countPairs ( str ) { let result = 0 ; let n = str . length ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( str [ i ] . charCodeAt ( ) - str [ j ] . charCodeAt ( ) ) == Math . abs ( i - j ) ) result ++ ; return result ; } let str = " " ; document . write ( countPairs ( str ) ) ;
Longest common subsequence with permutations allowed | Javascript program to find LCS with permutations allowed ; Function to calculate longest String str1 -- > first String str2 -- > second String count1 [ ] -- > hash array to calculate frequency of characters in str1 count [ 2 ] -- > hash array to calculate frequency of characters in str2 result -- > resultant longest String whose permutations are sub - sequence of given two strings ; calculate frequency of characters ; Now traverse hash array ; append character ( ' a ' + i ) in resultant String ' result ' by min ( count1 [ i ] , count2i ] ) times ; Driver Code
function min ( a , b ) { if ( a < b ) return a ; else return b ; } function longestString ( str1 , str2 ) { var count1 = new Array ( 26 ) ; var count2 = new Array ( 26 ) ; count1 . fill ( 0 ) ; count2 . fill ( 0 ) ; for ( var i = 0 ; i < str1 . length ; i ++ ) { count1 [ str1 . charCodeAt ( i ) - 97 ] ++ ; } for ( var i = 0 ; i < str2 . length ; i ++ ) { count2 [ str2 . charCodeAt ( i ) - 97 ] ++ ; } var result = " " ; for ( var i = 0 ; i < 26 ; i ++ ) { for ( var j = 1 ; j <= min ( count1 [ i ] , count2 [ i ] ) ; j ++ ) { result += String . fromCharCode ( 97 + i ) ; } } document . write ( result ) ; } var str1 = " " ; var str2 = " " ; longestString ( str1 , str2 ) ;
Check if string follows order of characters defined by a pattern or not | Set 1 | Function to check if characters in the input string follows the same order as determined by characters present in the given pattern ; len stores length of the given pattern ; if length of pattern is more than length of input string , return false ; ; x , y are two adjacent characters in pattern ; find index of last occurrence of character x in the input string ; find index of first occurrence of character y in the input string ; return false if x or y are not present in the input string OR last occurrence of x is after the first occurrence of y in the input string ; return true if string matches the pattern ; Driver code
function checkPattern ( str , pattern ) { var len = pattern . length ; if ( str . length < len ) { return false ; } for ( var i = 0 ; i < len - 1 ; i ++ ) { var x = pattern [ i ] ; var y = pattern [ i + 1 ] ; var last = str . lastIndexOf ( x ) ; var first = str . indexOf ( y ) ; if ( last === - 1 first === - 1 last > first ) { return false ; } } return true ; } var str = " " ; var pattern = " " ; document . write ( checkPattern ( str , pattern ) ) ;
Calculate sum of all numbers present in a string | Function to calculate sum of all numbers present in a string containing alphanumeric characters ; A temporary string ; holds sum of all numbers present in the string ; read each character in input string ; if current character is a digit ; if current character is an alphabet ; increment sum by number found earlier ( if any ) ; reset temporary string to empty ; atoi ( temp . c_str ( ) ) takes care of trailing numbers ; input alphanumeric string ; Function call
function findSum ( str ) { let temp = " " ; let sum = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { let ch = str [ i ] ; if ( ! isNaN ( String ( ch ) * 1 ) ) temp += ch ; else { sum += parseInt ( temp ) ; temp = " " ; } } return sum + parseInt ( temp ) ; } let str = " " ; document . write ( findSum ( str ) ) ;
Count number of substrings with exactly k distinct characters | Function to count number of substrings with exactly k unique characters ; Initialize result ; To store count of characters from ' a ' to ' z ' ; Consider all substrings beginning with str [ i ] ; Consider all substrings between str [ i . . j ] ; If this is a new character for this substring , increment dist_count . ; Increment count of current character ; If distinct character count becomes k , then increment result . ; Driver Program
function countkDist ( str , k ) { var res = 0 ; var n = str . length ; var cnt = Array . from ( { length : 26 } , ( _ , i ) => 0 ) ; for ( i = 0 ; i < n ; i ++ ) { var dist_count = 0 ; for ( j = i ; j < n ; j ++ ) { if ( cnt [ str . charAt ( j ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 0 ) dist_count ++ ; cnt [ str . charAt ( j ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; if ( dist_count == k ) res ++ ; } } return res ; } var ch = " " ; var k = 3 ; document . write ( " " + k + " " + countkDist ( ch , k ) ) ;