codeparrot/xlcost-text-to-code huggingface dataset:
Binary String of given length that without a palindrome of size 3 | JavaScript program find a binary String of given length that doesn 't contain a palindrome of size 3. ; Printing the character according to i ; Driver code
function generateString ( n ) { var s = " " ; for ( var i = 0 ; i < n ; i ++ ) s += ( ( i & 2 ) > 1 ? ' ' : ' ' ) ; document . write ( s + " " ) ; } var n = 5 ; generateString ( n ) ; n = 8 ; generateString ( n ) ; n = 10 ; generateString ( n ) ;
Print all subsequences of a string | Iterative Method | function to find subsequence ; Check if jth bit in binary is 1 ; If jth bit is 1 , include it in subsequence ; Function to print all subsequences ; map to store subsequence lexicographically by length ; Total number of non - empty subsequence in string is 2 ^ len - 1 ; i = 0 , corresponds to empty subsequence ; Subsequence for binary pattern i ; Storing sub in map ; it . first is length of Subsequence it . second is set < string > ; ii is iterator of type set < string > ; Driver code
function subsequence ( s , binary , len ) { let sub = " " ; for ( let j = 0 ; j < len ; j ++ ) if ( binary & ( 1 << j ) ) sub += s [ j ] ; return sub ; } function possibleSubsequences ( s ) { let sorted_subsequence = new Map ( ) ; let len = s . length ; let limit = Math . pow ( 2 , len ) ; for ( let i = 1 ; i <= limit - 1 ; i ++ ) { let sub = subsequence ( s , i , len ) ; if ( ! sorted_subsequence . has ( sub . length ) ) sorted_subsequence . set ( sub . length , new Set ( ) ) ; sorted_subsequence . get ( sub . length ) . add ( sub ) ; } for ( let it of sorted_subsequence ) { document . write ( " " + it [ 0 ] + " " + " " ) ; for ( let ii of it [ 1 ] ) document . write ( ii + " " ) ; document . write ( " " ) ; } } let s = " " ; possibleSubsequences ( s ) ;
Program to find remainder when large number is divided by 11 | Function to return remainder ; len is variable to store the length of number string . ; loop that find remainder ; Driver code
function remainder ( str ) { let len = str . length ; let num ; let rem = 0 ; for ( let i = 0 ; i < len ; i ++ ) { num = rem * 10 + ( str [ i ] - ' ' ) ; rem = num % 11 ; } return rem ; } let str = " " ; document . write ( remainder ( str ) ) ;
Longest subsequence of the form 0 * 1 * 0 * in a binary string | Returns length of the longest subsequence of the form 0 * 1 * 0 * ; Precomputing values in three arrays pre_count_0 [ i ] is going to store count of 0 s in prefix str [ 0. . i - 1 ] pre_count_1 [ i ] is going to store count of 1 s in prefix str [ 0. . i - 1 ] post_count_0 [ i ] is going to store count of 0 s in suffix str [ i - 1. . n - 1 ] ; String is made up of all 0 s or all 1 s ; Compute result using precomputed values ; Driver code
function longestSubseq ( s ) { let n = s . length ; let pre_count_0 = new Array ( n + 2 ) ; let pre_count_1 = new Array ( n + 1 ) ; let post_count_0 = new Array ( n + 2 ) ; pre_count_0 [ 0 ] = 0 ; post_count_0 [ n + 1 ] = 0 ; pre_count_1 [ 0 ] = 0 ; for ( let j = 1 ; j <= n ; j ++ ) { pre_count_0 [ j ] = pre_count_0 [ j - 1 ] ; pre_count_1 [ j ] = pre_count_1 [ j - 1 ] ; post_count_0 [ n - j + 1 ] = post_count_0 [ n - j + 2 ] ; if ( s [ j - 1 ] == ' ' ) pre_count_0 [ j ] ++ ; else pre_count_1 [ j ] ++ ; if ( s [ n - j ] == ' ' ) post_count_0 [ n - j + 1 ] ++ ; } if ( pre_count_0 [ n ] == n pre_count_0 [ n ] == 0 ) return n ; let ans = 0 ; for ( let i = 1 ; i <= n ; i ++ ) for ( let j = i ; j <= n ; j ++ ) ans = Math . max ( pre_count_0 [ i - 1 ] + pre_count_1 [ j ] - pre_count_1 [ i - 1 ] + post_count_0 [ j + 1 ] , ans ) ; return ans ; } let s = " " ; document . write ( longestSubseq ( s ) ) ;
Distinct permutations of the string | Set 2 | Returns true if str [ curr ] does not matches with any of the characters after str [ start ] ; Prints all distinct permutations in str [ 0. . n - 1 ] ; Proceed further for str [ i ] only if it doesn 't match with any of the characters  after str[index] ; Driver code
function shouldSwap ( str , start , curr ) { for ( let i = start ; i < curr ; i ++ ) { if ( str [ i ] == str [ curr ] ) { return false ; } } return true ; } function findPermutations ( str , index , n ) { if ( index >= n ) { document . write ( str ) ; document . write ( " " ) return ; } for ( let i = index ; i < n ; i ++ ) { let check = shouldSwap ( str , index , i ) ; if ( check ) { swap ( str , index , i ) ; findPermutations ( str , index + 1 , n ) ; swap ( str , index , i ) ; } } } function swap ( str , i , j ) { let c = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = c ; } let str = [ ' ' , ' ' , ' ' , ' ' ] ; let n = str . length ; findPermutations ( str , 0 , n ) ;
Generate permutations with only adjacent swaps allowed | JavaScript program to generate permutations with only one swap allowed . ; don 't swap the current position ; Swap with the next character and revert the changes . As explained above , swapping with previous is is not needed as it anyways happens for next character . ; Driver code
function findPermutations ( str , index , n ) { if ( index >= n index + 1 >= n ) { document . write ( str . join ( " " ) + " " ) ; return ; } findPermutations ( str , index + 1 , n ) ; swap ( str , index ) ; findPermutations ( str , index + 2 , n ) ; swap ( str , index ) ; } function swap ( arr , index ) { var temp = arr [ index ] ; arr [ index ] = arr [ index + 1 ] ; arr [ index + 1 ] = temp ; } var str = " " . split ( " " ) ; var n = str . length ; findPermutations ( str , 0 , n ) ;
Decode a median string to the original string | function to calculate the median back string ; length of string ; initialize a blank string ; Flag to check if length is even or odd ; traverse from first to last ; if len is even then add first character to beginning of new string and second character to end ; if current length is odd and is greater than 1 ; add first character to end and second character to beginning ; if length is 1 , add character to end ; Driver code
function decodeMedianString ( s ) { var l = s . length ; var s1 = " " ; var isEven = ( l % 2 == 0 ) ? true : false ; for ( i = 0 ; i < l ; i += 2 ) { if ( isEven ) { s1 = s . charAt ( i ) + s1 ; s1 += s . charAt ( i + 1 ) ; } else { if ( l - i > 1 ) { s1 += s . charAt ( i ) ; s1 = s . charAt ( i + 1 ) + s1 ; } else { s1 += s . charAt ( i ) ; } } } return s1 ; } var s = " " ; document . write ( decodeMedianString ( s ) ) ;
Maximum number of characters between any two same character in a string | Simple Javascript program to find maximum number of characters between two occurrences of same character ; Driver code
function maximumChars ( str ) { let n = str . length ; let res = - 1 ; for ( let i = 0 ; i < n - 1 ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) if ( str [ i ] == str [ j ] ) res = Math . max ( res , Math . abs ( j - i - 1 ) ) ; return res ; } let str = " " ; document . write ( maximumChars ( str ) ) ;
Maximum number of characters between any two same character in a string | Efficient javascript program to find maximum number of characters between two occurrences of same character ; Initialize all indexes as - 1. ; If this is first occurrence ; Else find distance from previous occurrence and update result ( if required ) . ; Driver code
let MAX_CHAR = 256 ; function maximumChars ( str ) { let n = str . length ; let res = - 1 ; let firstInd = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) firstInd [ i ] = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { let first_ind = firstInd [ str [ i ] . charCodeAt ( 0 ) ] ; if ( first_ind == - 1 ) firstInd [ str [ i ] . charCodeAt ( 0 ) ] = i ; else res = Math . max ( res , Math . abs ( i - first_ind - 1 ) ) ; } return res ; } let str = " " ; document . write ( maximumChars ( str ) ) ;
Check if an encoding represents a unique binary string | javascript program to check if given encoding represents a single string . ; Return true if sum becomes k ; Driver Code
function isUnique ( a , n , k ) { var sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += a [ i ] ; sum += n - 1 ; return ( sum == k ) ; } var a = [ 3 , 3 , 3 ] ; var n = a . length ; var k = 12 ; if ( isUnique ( a , n , k ) ) document . write ( " " ) ; else document . write ( " " ) ;
Lexicographically next string | Javascript program to find lexicographically next string ; If string is empty . ; Find first character from right which is not z . ; If all characters are ' z ' , append an ' a ' at the end . ; If there are some non - z characters ; Driver code
function nextWord ( s ) { if ( s == " " ) return " " ; var i = s . length - 1 ; while ( s [ i ] == ' ' && i >= 0 ) i -- ; if ( i == - 1 ) s = s + ' ' ; else s [ i ] = String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) + 1 ) ; return s . join ( ' ' ) ; } var str = " " . split ( ' ' ) ; document . write ( nextWord ( str ) ) ;
Length of the longest substring with equal 1 s and 0 s | Function to check if a string contains equal number of one and zeros or not ; Function to find the length of the longest balanced substring ; Driver code ; Function call
function isValid ( p ) { var n = p . length ; var c1 = 0 , c0 = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( p [ i ] == ' ' ) c0 ++ ; if ( p [ i ] == ' ' ) c1 ++ ; } return ( c0 == c1 ) ? true : false ; } function longestSub ( s ) { var max_len = 0 ; var n = s . length ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i ; j < n ; j ++ ) { if ( isValid ( s . substr ( i , j - i + 1 ) ) && max_len < j - i + 1 ) max_len = j - i + 1 ; } } return max_len ; } var s = " " ; document . write ( longestSub ( s ) ) ;
Number of positions where a letter can be inserted such that a string becomes palindrome | Javascript code to find the no . of positions where a letter can be inserted to make it a palindrome ; Function to check if the string is palindrome ; to know the length of string ; if the given string is a palindrome ( Case - I ) ; Sub - case - III ) ; if ( n % 2 == 0 ) if the length is even ; count = 2 * count + 1 ; sub - case - I ; count = 2 * count + 2 ; sub - case - II ; insertion point ; Case - I ; Case - II ; Driver code
function isPalindrome ( s , i , j ) { let p = j ; for ( let k = i ; k <= p ; k ++ ) { if ( s [ k ] != s [ p ] ) return false ; p -- ; } return true ; } function countWays ( s ) { let n = s . length ; let count = 0 ; if ( isPalindrome ( s , 0 , n - 1 ) ) { for ( let i = n / 2 ; i < n ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) count ++ ; else break ; } { count ++ ; } else } else { for ( let i = 0 ; i < n / 2 ; i ++ ) { if ( s [ i ] != s [ n - 1 - i ] ) { let j = n - 1 - i ; if ( isPalindrome ( s , i , n - 2 - i ) ) { for ( let k = i - 1 ; k >= 0 ; k -- ) { if ( s [ k ] != s [ j ] ) break ; count ++ ; } count ++ ; } if ( isPalindrome ( s , i + 1 , n - 1 - i ) ) { for ( let k = n - i ; k < n ; k ++ ) { if ( s [ k ] != s [ i ] ) break ; count ++ ; } count ++ ; } break ; } } } return count ; } let s = " " ; document . write ( countWays ( s ) ) ;
Count of substrings of a binary string containing K ones | Method returns total number of substring having K ones ; Initialize index having zero sum as 1 ; Loop over binary characters of string ; Update countOfOne variable with value of ith character ; If value reaches more than K , then update result ; Add frequency of indices , having sum ( current sum - K ) , to the result ; Update frequency of one 's count ; Driver code
function countOfSubstringWithKOnes ( s , K ) { let N = s . length ; let res = 0 ; let countOfOne = 0 ; let freq = new Array ( N + 1 ) ; freq . fill ( 0 ) ; freq [ 0 ] = 1 ; for ( let i = 0 ; i < N ; i ++ ) { countOfOne += ( s [ i ] - ' ' ) ; if ( countOfOne >= K ) { res += freq [ countOfOne - K ] ; } freq [ countOfOne ] ++ ; } return res ; } let s = " " ; let K = 1 ; document . write ( countOfSubstringWithKOnes ( s , K ) ) ;
Generate two output strings depending upon occurrence of character in input string . | javascript program to print two strings made of character occurring once and multiple times ; function to print two strings generated from single string one with characters occurring onces other with character occurring multiple of times ; initialize hashtable with zero entry ; perform hashing for input string ; generate string ( str1 ) consisting char occurring once and string ( str2 ) consisting char occurring multiple times ; print both strings ; driver program
var MAX_CHAR = 256 ; function printDuo ( str ) { var countChar = Array ( MAX_CHAR ) . fill ( 0 ) ; var n = str . length ; for ( var i = 0 ; i < n ; i ++ ) countChar [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; var str1 = " " , str2 = " " ; for ( var i = 0 ; i < MAX_CHAR ; i ++ ) { if ( countChar [ i ] > 1 ) str2 = str2 + String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; else if ( countChar [ i ] == 1 ) str1 = str1 + String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; } document . write ( " " + " " ) ; document . write ( str1 + " " ) ; document . write ( " " + " " ) ; document . write ( str2 + " " ) ; } var str = " " ; printDuo ( str ) ;
Next higher palindromic number using the same set of digits | Function to reverse the digits in the range i to j in ' num ' ; Function to find next higher palindromic number using the same set of digits ; If length of number is less than '3' then no higher palindromic number can be formed ; Find the index of last digit in the 1 st half of ' num ' ; Start from the ( mid - 1 ) th digit and find the first digit that is smaller than the digit next to it . ; If no such digit is found , then all digits are in descending order which means there cannot be a greater palindromic number with same set of digits ; Find the smallest digit on right side of ith digit which is greater than num [ i ] up to index ' mid ' ; Swap num [ i ] with num [ smallest ] ; As the number is a palindrome , the same swap of digits should be performed in the 2 nd half of ' num ' ; Reverse digits in the range ( i + 1 ) to mid ; If n is even , then reverse digits in the range mid + 1 to n - i - 2 ; Else if n is odd , then reverse digits in the range mid + 2 to n - i - 2 ; Required next higher palindromic number ; Driver Code
function reverse ( num , i , j ) { while ( i < j ) { var temp = num [ i ] ; num [ i ] = num [ j ] ; num [ j ] = temp ; i ++ ; j -- ; } } function nextPalin ( num , n ) { if ( n <= 3 ) { document . write ( " " ) ; return ; } var temp ; var mid = n / 2 - 1 ; var i , j ; for ( i = mid - 1 ; i >= 0 ; i -- ) if ( num [ i ] < num [ i + 1 ] ) break ; if ( i < 0 ) { document . write ( " " ) ; return ; } var smallest = i + 1 ; for ( j = i + 2 ; j <= mid ; j ++ ) if ( num [ j ] > num [ i ] && num [ j ] <= num [ smallest ] ) smallest = j ; temp = num [ i ] ; num [ i ] = num [ smallest ] ; num [ smallest ] = temp ; temp = num [ n - i - 1 ] ; num [ n - i - 1 ] = num [ n - smallest - 1 ] ; num [ n - smallest - 1 ] = temp ; reverse ( num , i + 1 , mid ) ; if ( n % 2 == 0 ) reverse ( num , mid + 1 , n - i - 2 ) ; else reverse ( num , mid + 2 , n - i - 2 ) ; var result = num . join ( ' ' ) ; document . write ( " " + result ) ; } var str = " " ; var num = str . split ( ' ' ) ; var n = str . length ; nextPalin ( num , n ) ;
Print N | function to generate n digit numbers ; if number generated ; Append 1 at the current number and reduce the remaining places by one ; If more ones than zeros , append 0 to the current number and reduce the remaining places by one ; driver function ; Function call
function printRec ( number , extraOnes , remainingPlaces ) { if ( 0 == remainingPlaces ) { document . write ( number + " " ) ; return ; } printRec ( number + " " , extraOnes + 1 , remainingPlaces - 1 ) ; if ( 0 < extraOnes ) printRec ( number + " " , extraOnes - 1 , remainingPlaces - 1 ) ; } function printNums ( n ) { let str = " " ; printRec ( str , 0 , n ) ; } let n = 4 ; printNums ( n ) ;
Make a string from another by deletion and rearrangement of characters | Javascript program to find if it is possible to make a string from characters present in other string . ; Returns true if it is possible to make s1 from characters present in s2 . ; Count occurrences of all characters present in s2 . . ; For every character , present in s1 , reduce its count if count is more than 0. ; Driver code
let MAX_CHAR = 256 ; function isPossible ( s1 , s2 ) { let count = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < count . length ; i ++ ) { count [ i ] = 0 ; } for ( let i = 0 ; i < s2 . length ; i ++ ) count [ s2 [ i ] . charCodeAt ( 0 ) ] ++ ; for ( let i = 0 ; i < s1 . length ; i ++ ) { if ( count [ s1 [ i ] . charCodeAt ( 0 ) ] == 0 ) return false ; count [ s1 [ i ] . charCodeAt ( 0 ) ] -- ; } return true ; } let s1 = " " , s2 = " " ; if ( isPossible ( s1 , s2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Next higher number using atmost one swap operation | function to find the next higher number using atmost one swap operation ; to store the index of the largest digit encountered so far from the right ; to store the index of rightmost digit which has a digit greater to it on its right side ; finding the ' index ' of rightmost digit which has a digit greater to it on its right side ; required digit found , store its ' index ' and break ; if no such digit is found which has a larger digit on its right side ; to store the index of the smallest digit greater than the digit at ' index ' and right to it ; finding the index of the smallest digit greater than the digit at ' index ' and right to it ; swapping the digits ; required number ; Driver program to test above
function nextHighUsingAtMostOneSwap ( st ) { var num = st . split ( ' ' ) ; var l = num . length ; var posRMax = l - 1 ; var index = - 1 ; for ( var i = l - 2 ; i >= 0 ; i -- ) { if ( num [ i ] >= num [ posRMax ] ) posRMax = i ; else { index = i ; break ; } } if ( index == - 1 ) return " " ; var greatSmallDgt = - 1 ; for ( var i = l - 1 ; i > index ; i -- ) { if ( num [ i ] > num [ index ] ) { if ( greatSmallDgt == - 1 ) greatSmallDgt = i ; else if ( num [ i ] <= num [ greatSmallDgt ] ) greatSmallDgt = i ; } } var temp = num [ index ] ; num [ index ] = num [ greatSmallDgt ] ; num [ greatSmallDgt ] = temp ; return ( num . join ( ' ' ) ) ; } var num = " " ; document . write ( " " + num ) ; document . write ( " " + nextHighUsingAtMostOneSwap ( num ) ) ;
Longest substring of vowels | Javascript program to find the longest substring of vowels . ; Increment current count if s [ i ] is vowel ; Check previous value is greater then or not ; Driver code
function isVowel ( c ) { return ( c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' ) ; } function longestVowel ( str ) { let count = 0 , res = 0 ; let s = str . split ( " " ) ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( isVowel ( s [ i ] ) ) count ++ ; else { res = Math . max ( res , count ) ; count = 0 ; } } return Math . max ( res , count ) ; } let s = " " ; document . write ( longestVowel ( s ) ) ;
Number of substrings with count of each character as k | Javascript program to count number of substrings with counts of distinct characters as k . ; Returns true if all values in freq [ ] are either 0 or k . ; Returns count of substrings where frequency of every present character is k ; Initialize result ; Pick a starting point ; Initialize all frequencies as 0 for this starting point ; One by one pick ending points ; Increment frequency of current char ; If frequency becomes more than k , we can 't have more substrings  starting with i ; If frequency becomes k , then check other frequencies as well . ; Driver code
let MAX_CHAR = 26 ; function check ( freq , k ) { for ( let i = 0 ; i < MAX_CHAR ; i ++ ) if ( freq [ i ] != 0 && freq [ i ] != k ) return false ; return true ; } function substrings ( s , k ) { let res = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { let freq = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < freq . length ; i ++ ) { freq [ i ] = 0 ; } for ( let j = i ; j < s . length ; j ++ ) { let index = s [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; freq [ index ] ++ ; if ( freq [ index ] > k ) break ; else if ( freq [ index ] == k && check ( freq , k ) == true ) res ++ ; } } return res ; } let s = " " ; let k = 2 ; document . write ( substrings ( s , k ) + " " ) ; s = " " ; k = 2 ; document . write ( substrings ( s , k ) + " " ) ;
Number of substrings with count of each character as k |
function have_same_frequency ( freq , k ) { for ( let i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] != 0 && freq [ i ] != k ) { return false ; } } return true ; } function count_substrings ( s , k ) { let count = 0 ; let distinct = 0 ; let have = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { have [ i ] = false ; } for ( let i = 0 ; i < s . length ; i ++ ) { have [ ( ( s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) ] = true ; } for ( let i = 0 ; i < 26 ; i ++ ) { if ( have [ i ] ) { distinct ++ ; } } for ( let length = 1 ; length <= distinct ; length ++ ) { let window_length = length * k ; let freq = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) freq [ i ] = 0 ; let window_start = 0 ; let window_end = window_start + window_length - 1 ; for ( let i = window_start ; i <= Math . min ( window_end , s . length - 1 ) ; i ++ ) { freq [ ( ( s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) ] ++ ; } while ( window_end < s . length ) { if ( have_same_frequency ( freq , k ) ) { count ++ ; } freq [ ( ( s [ window_start ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) ] -- ; window_start ++ ; window_end ++ ; if ( window_end < s . length ) { freq [ ( s [ window_end ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ] ++ ; } } } return count ; } let s = " " ; let k = 2 ; document . write ( count_substrings ( s , k ) + " " ) ; s = " " ; k = 2 ; document . write ( count_substrings ( s , k ) + " " ) ;
Longest subsequence where every character appears at | Javascript program to find longest subsequence where every character appears at - least k times ; Count frequencies of all characters ; Traverse given string again and print all those characters whose frequency is more than or equal to k . ; Driver code
let MAX_CHARS = 26 ; function longestSubseqWithK ( str , k ) { let n = str . length ; let freq = new Array ( MAX_CHARS ) ; for ( let i = 0 ; i < freq . length ; i ++ ) { freq [ i ] = 0 } for ( let i = 0 ; i < n ; i ++ ) { freq [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } for ( let i = 0 ; i < n ; i ++ ) { if ( freq [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] >= k ) { document . write ( str [ i ] ) ; } } } let str = " " ; let k = 2 ; longestSubseqWithK ( str , k ) ;
Generating distinct subsequences of a given string in lexicographic order | Finds and stores result in st for a given string s . ; If current string is not already present . ; Traverse current string , one by one remove every character and recur . ; Driver code
function generate ( st , s ) { if ( s . length == 0 ) return st ; if ( ! st . has ( s ) ) { st . add ( s ) ; for ( let i = 0 ; i < s . length ; i ++ ) { let t = s ; t = t . substr ( 0 , i ) + t . substr ( i + 1 ) ; st = generate ( st , t ) ; } } return st ; } let s = " " ; let st = new Set ( ) ; st = generate ( st , s ) ; let str = ' ' ; console . log ( st ) for ( item of st . values ( ) ) str += item + ' ' document . write ( str ) ;
Recursive solution to count substrings with same first and last characters | Function to count substrings with same first and last characters ; Base cases ; Driver code
function countSubstrs ( str , i , j , n ) { if ( n == 1 ) return 1 ; if ( n <= 0 ) return 0 ; let res = countSubstrs ( str , i + 1 , j , n - 1 ) + countSubstrs ( str , i , j - 1 , n - 1 ) - countSubstrs ( str , i + 1 , j - 1 , n - 2 ) ; if ( str [ i ] == str [ j ] ) res ++ ; return res ; } let str = " " ; let n = str . length ; document . write ( countSubstrs ( str , 0 , n - 1 , n ) ) ;