codeparrot/xlcost-text-to-code huggingface dataset:
Sub | Function that counts all the sub - strings of length ' k ' which have all identical characters ; count of sub - strings , length , initial position of sliding window ; map to store the frequency of the characters of sub - string ; increase the frequency of the character and length of the sub - string ; if the length of the sub - string is greater than K ; remove the character from the beginning of sub - string ; if the length of the sub string is equal to k and frequency of one of its characters is equal to the length of the sub - string i . e . all the characters are same increase the count ; display the number of valid sub - strings ; Driver code
function solve ( s , k ) { var count = 0 , length = 0 , pos = 0 ; var m = new Map ( ) ; for ( var i = 0 ; i < s . length ; i ++ ) { if ( ! m . has ( s [ i ] ) ) { m . set ( s [ i ] , 0 ) ; } m . set ( s [ i ] , m . get ( s [ i ] ) + 1 ) ; length ++ ; if ( length > k ) { if ( ! m . has ( s [ pos ] ) ) { m . set ( s [ pos ] , 0 ) ; } m . set ( s [ pos ] , m [ s [ pos ] ] - 1 ) ; pos += 1 ; length -- ; } if ( length == k && m . get ( s [ i ] ) == length ) count ++ ; } document . write ( count ) ; } var s = " " ; var k = 4 ; solve ( s , k ) ;
Program to replace every space in a string with hyphen | Get the String ; Traverse the string character by character . ; Changing the ith character to ' - ' if it 's a space. ; Print the modified string .
var str = " " ; var newStr = str . split ( " " ) ; for ( var i = 0 ; i < newStr . length ; ++ i ) { if ( newStr [ i ] === " " ) { newStr [ i ] = " " ; } } document . write ( newStr . join ( " " ) + " " ) ;
Minimum operation require to make first and last character same | Javascript program to find minimum operation require to make first and last character same ; Function to find minimum operation require to make first and last character same ; Base conditions ; If answer found ; If string is already visited ; Decrement ending index only ; Increment starting index only ; Increment starting index and decrement index ; Store the minimum value ; Driver code ; Function call
let MAX = Number . MAX_VALUE ; let m = new Map ( ) ; let Min = Number . MAX_VALUE ; function minOperation ( s , i , j , count ) { if ( ( i >= s . length && j < 0 ) || ( i == j ) ) { return MAX ; } if ( s [ i ] == s [ j ] || ( count >= Min ) ) { return count ; } let str = ( i ) . toString ( ) + " " + ( j ) . toString ( ) ; if ( ! m . has ( str ) ) { if ( i >= s . length ) { m . set ( str , minOperation ( s , i , j - 1 , count + 1 ) ) ; } else if ( j < 0 ) { m . set ( str , minOperation ( s , i + 1 , j , count + 1 ) ) ; } else { m . set ( str , Math . min ( minOperation ( s , i , j - 1 , count + 1 ) , minOperation ( s , i + 1 , j , count + 1 ) ) ) ; } } if ( m . get ( str ) < Min ) { Min = m . get ( str ) ; } return m . get ( str ) ; } let s = " " ; let ans = minOperation ( s , 0 , s . length - 1 , 0 ) ; if ( ans == MAX ) { document . write ( - 1 ) ; } else { document . write ( ans ) ; }
Check if any permutation of a large number is divisible by 8 | Function to check if any permutation of a large number is divisible by 8 ; Less than three digit number can be checked directly . ; check for the reverse of a number ; Stores the Frequency of characters in the n . ; Iterates for all three digit numbers divisible by 8 ; stores the frequency of all single digit in three - digit number ; check if the original number has the digit ; when all are checked its not possible ; Utility reverse function ; Driver Code
function solve ( n , l ) { if ( l < 3 ) { if ( parseInt ( n ) % 8 === 0 ) return true ; n = reverse ( n ) ; if ( parseInt ( n ) % 8 === 0 ) return true ; return false ; } var hash = new Array ( 10 ) . fill ( 0 ) ; for ( var i = 0 ; i < l ; i ++ ) hash [ parseInt ( n [ i ] ) - 0 ] ++ ; for ( var i = 104 ; i < 1000 ; i += 8 ) { var dup = i ; var freq = new Array ( 10 ) . fill ( 0 ) ; freq [ parseInt ( dup % 10 ) ] ++ ; dup = dup / 10 ; freq [ parseInt ( dup % 10 ) ] ++ ; dup = dup / 10 ; freq [ parseInt ( dup % 10 ) ] ++ ; dup = i ; if ( freq [ parseInt ( dup % 10 ) ] > hash [ parseInt ( dup % 10 ) ] ) continue ; dup = dup / 10 ; if ( freq [ parseInt ( dup % 10 ) ] > hash [ parseInt ( dup % 10 ) ] ) continue ; dup = dup / 10 ; if ( freq [ parseInt ( dup % 10 ) ] > hash [ parseInt ( dup % 10 ) ] ) continue ; return true ; } return false ; } function reverse ( input ) { var a = input . split ( " " ) ; var l , r = 0 ; r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { var temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return a . join ( " " ) ; } var number = " " ; var l = number . length ; if ( solve ( number , l ) ) document . write ( " " ) ; else document . write ( " " ) ;
Lexicographically smallest string formed by appending a character from the first K characters of a given string | Function to find the new string thus formed by removing characters ; new string ; Remove characters until the string is empty ; Traverse to find the smallest character in the first k characters ; append the smallest character ; removing the lexicographically smallest character from the string ; Driver code
function newString ( s , k ) { var X = " " ; while ( s . length > 0 ) { var temp = s [ 0 ] ; for ( var i = 1 ; i < k && i < s . length ; i ++ ) { if ( s [ i ] < temp ) { temp = s [ i ] ; } } X = X + temp ; for ( var i = 0 ; i < k ; i ++ ) { if ( s [ i ] === temp ) { s = s . substring ( 0 , i ) + s . substring ( i + 1 ) ; break ; } } } return X ; } var s = " " ; var k = 3 ; document . write ( newString ( s , k ) ) ;
Given a binary tree , how do you remove all the half nodes ? | Binary tree node ; For inorder traversal ; Removes all nodes with only one child and returns new root ( note that root may change ) ; * if current nodes is a half node with left child NULL left , then it 's right  * child is returned and replaces it in the given tree   ; * if current nodes is a half node with right child NULL right , then it 's right  * child is returned and replaces it in the given tree   ; Driver program
class Node { constructor ( item ) { this . data = item ; this . left = this . right = null ; } } var root ; function printInorder ( node ) { if ( node != null ) { printInorder ( node . left ) ; document . write ( node . data + " " ) ; printInorder ( node . right ) ; } } function RemoveHalfNodes ( node ) { if ( node == null ) return null ; node . left = RemoveHalfNodes ( node . left ) ; node . right = RemoveHalfNodes ( node . right ) ; if ( node . left == null && node . right == null ) return node ; if ( node . left == null ) { new_root = node . right ; return new_root ; } if ( node . right == null ) { new_root = node . left ; return new_root ; } return node ; } NewRoot = null ; root = new Node ( 2 ) ; root . left = new Node ( 7 ) ; root . right = new Node ( 5 ) ; root . left . right = new Node ( 6 ) ; root . left . right . left = new Node ( 1 ) ; root . left . right . right = new Node ( 11 ) ; root . right . right = new Node ( 9 ) ; root . right . right . left = new Node ( 4 ) ; document . write ( " " ) ; printInorder ( root ) ; NewRoot = RemoveHalfNodes ( root ) ; document . write ( " " ) ; printInorder ( NewRoot ) ; script
Convert String into Binary Sequence | utility function ; convert each char to ASCII value ; Convert ASCII value to binary ; Utility reverse function ; Driver code
function strToBinary ( s ) { let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { let val = ( s [ i ] ) . charCodeAt ( 0 ) ; let bin = " " ; while ( val > 0 ) { if ( val % 2 == 1 ) { bin += ' ' ; } else bin += ' ' ; val = Math . floor ( val / 2 ) ; } bin = reverse ( bin ) ; document . write ( bin + " " ) ; } } function reverse ( input ) { a = input . split ( " " ) ; let l , r = 0 ; r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { let temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return ( a ) . join ( " " ) ; } let s = " " ; strToBinary ( s ) ;
Sparse Search | Binary Search in an array with blanks ; Modified Part ; Search for both side for a non empty string ; No non - empty string on both sides ; Normal Binary Search ; Driver code
function binarySearch ( arr , low , high , x ) { if ( low > high ) return - 1 ; let mid = Math . floor ( ( low + high ) / 2 ) ; if ( arr [ mid ] == " " ) { let left = mid - 1 ; let right = mid + 1 ; while ( true ) { if ( left < low && right > high ) return - 1 ; if ( left >= low && arr [ left ] != " " ) { mid = left ; break ; } else if ( right <= high && arr [ right ] != " " ) { mid = right ; break ; } left -- ; right ++ ; } } if ( arr [ mid ] == x ) return mid ; else if ( arr [ mid ] > x ) return binarySearch ( arr , low , mid - 1 , x ) ; else return binarySearch ( arr , mid + 1 , high , x ) ; } function sparseSearch ( arr , x , n ) { return binarySearch ( arr , 0 , n - 1 , x ) ; } let arr = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let x = " " ; let n = x . length ; let index = sparseSearch ( arr , x , n ) ; if ( index != - 1 ) document . write ( x + " " + index ) ; else document . write ( x + " " ) ;
Print distinct sorted permutations with duplicates allowed in input | Calculating factorial of a number ; Method to print the array ; Method to find total number of permutations ; Building HashMap to store frequencies of all characters . ; Traversing hashmap and finding duplicate elements . ; Start traversing from the end and find position ' i - 1' of the first character which is greater than its successor . ; Finding smallest character after ' i - 1' and greater than temp [ i - 1 ] ; Swapping the above found characters . ; Sort all digits from position next to ' i - 1' to end of the string . ; Print the String ; Sorting String ; Print first permutation ; Finding the total permutations ; Driver Code
function factorial ( n ) { let f = 1 ; for ( let i = 1 ; i <= n ; i ++ ) f = f * i ; return f ; } function print ( temp ) { for ( let i = 0 ; i < temp . length ; i ++ ) document . write ( temp [ i ] ) ; document . write ( " " ) ; } function calculateTotal ( temp , n ) { let f = factorial ( n ) ; let hm = new Map ( ) ; for ( let i = 0 ; i < temp . length ; i ++ ) { if ( hm . has ( temp [ i ] ) ) hm . set ( temp [ i ] , hm . get ( temp [ i ] ) + 1 ) ; else hm . set ( temp [ i ] , 1 ) ; } for ( let [ key , value ] of hm . entries ( ) ) { let x = value ; if ( x > 1 ) { let temp5 = factorial ( x ) ; f = Math . floor ( f / temp5 ) ; } } return f ; } function nextPermutation ( temp ) { let i ; for ( i = temp . length - 1 ; i > 0 ; i -- ) if ( temp [ i ] > temp [ i - 1 ] ) break ; let min = i ; let j , x = temp [ i - 1 ] ; for ( j = i + 1 ; j < temp . length ; j ++ ) if ( ( temp [ j ] < temp [ min ] ) && ( temp [ j ] > x ) ) min = j ; let temp_to_swap ; temp_to_swap = temp [ i - 1 ] ; temp [ i - 1 ] = temp [ min ] ; temp [ min ] = temp_to_swap ; temp = temp . slice ( 0 , i ) . sort ( ) . concat ( temp . slice ( i , temp . length ) ) ; print ( temp ) ; } function printAllPermutations ( s ) { let temp = s . split ( " " ) ; temp . sort ( ) ; print ( temp ) ; let total = calculateTotal ( temp , temp . length ) ; for ( let i = 1 ; i < total ; i ++ ) nextPermutation ( temp ) ; } let s = " " ; printAllPermutations ( s ) ;
Convert a sentence into its equivalent mobile numeric keypad sequence | Function which computes the sequence ; length of input string ; Checking for space ; Calculating index for each character ; Output sequence ; storing the sequence in array
function printSequence ( arr , input ) { let output = " " ; let n = input . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( input [ i ] == ' ' ) output = output + " " . charCodeAt ( 0 ) ; else { let position = input [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; output = output + arr [ position ] ; } } return output ; } let str = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] let input = " " ; document . write ( printSequence ( str , input ) ) ;
Longest Uncommon Subsequence | function to calculate length of longest uncommon subsequence ; creating an unordered map to map strings to their frequency ; traversing all elements of vector strArr ; Creating all possible subsequences , i . e 2 ^ n ; ( ( i >> j ) & 1 ) determines which character goes into string t ; If common subsequence is found , increment its frequency ; traversing the map ; if frequency equals 1 ; input strings
function findLUSlength ( a , b ) { let map = new Map ( ) ; let strArr = [ ] ; strArr . push ( a ) ; strArr . push ( b ) ; for ( let s = 0 ; s < strArr . length ; s ++ ) { for ( let i = 0 ; i < ( 1 << strArr [ s ] . length ) ; i ++ ) { let t = " " ; for ( let j = 0 ; j < strArr [ s ] . length ; j ++ ) { if ( ( ( i >> j ) & 1 ) != 0 ) t += strArr [ s ] [ j ] ; } if ( map . has ( t ) ) map . set ( t , map . get ( t ) + 1 ) ; else map . set ( t , 1 ) ; } } let res = 0 ; for ( let [ key , value ] of map . entries ( ) ) { if ( value == 1 ) res = Math . max ( res , key . length ) ; } return res ; } let a = " " , b = " " ; document . write ( findLUSlength ( a , b ) ) ;
Round the given number to nearest multiple of 10 | Program to round the number to the nearest number having one 's digit 0 ; Last character is 0 then return the original string ; If last character is 1 or 2 or 3 or 4 or 5 make it 0 ; process carry ; Return final string ; Driver code ; Function Call
function round ( s , n ) { let c = s . split ( ' ' ) ; if ( c [ n - 1 ] == ' ' ) return s ; else if ( c [ n - 1 ] == ' ' c [ n - 1 ] == ' ' c [ n - 1 ] == ' ' c [ n - 1 ] == ' ' c [ n - 1 ] == ' ' ) { c [ n - 1 ] = ' ' ; return c . join ( " " ) ; } else { c [ n - 1 ] = ' ' ; for ( let i = n - 2 ; i >= 0 ; i -- ) { if ( c [ i ] == ' ' ) c [ i ] = ' ' ; else { let t = c [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) + 1 ; c [ i ] = String . fromCharCode ( 48 + t ) ; break ; } } } let s1 = c . join ( " " ) ; if ( s1 [ 0 ] == ' ' ) s1 = " " + s1 ; return s1 ; } let s = " " ; let n = s . length ; document . write ( round ( s , n ) ) ;
Check whether given floating point number is even or odd | Function to check even or odd . ; Loop to traverse number from LSB ; We ignore trailing 0 s after dot ; If it is ' . ' we will check next digit and it means decimal part is traversed . ; If digit is divisible by 2 means even number . ; Driver Code
function isEven ( s ) { let l = s . length ; let dotSeen = false ; for ( let i = l - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == ' ' && dotSeen == false ) continue ; if ( s [ i ] == ' ' ) { dotSeen = true ; continue ; } if ( ( s [ i ] - ' ' ) % 2 == 0 ) return true ; return false ; } } let s = " " ; if ( isEven ( s ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum cost to construct a string | JavaScript Program to find minimum cost to construct a string ; Initially all characters are un - seen ; Marking seen characters ; Count total seen character , and that is the cost ; s is the string that needs to be constructed
function minCost ( s ) { var alphabets = new Array ( 26 ) . fill ( 0 ) ; for ( var i = 0 ; i < s . length ; i ++ ) { alphabets [ s [ i ] . charCodeAt ( 0 ) - 97 ] = true ; } var count = 0 ; for ( var i = 0 ; i < 26 ; i ++ ) { if ( alphabets [ i ] ) { count ++ ; } } return count ; } var s = " " ; document . write ( " " + s + " " + minCost ( s . split ( " " ) ) ) ;
Possibility of moving out of maze | Function to check whether it will stay inside or come out ; marks all the positions that is visited ; Initial starting point ; initial assumption is it comes out ; runs till it is inside or comes out ; if the movement is towards left then we move left . The start variable and mark that position as visited if not visited previously . Else we break out ; It will be inside forever ; If the movement is towards right , then we move right . The start variable and mark that position as visited if not visited previously else we break out ; it will be inside forever ; Driver code
function checkingPossibility ( a , n , s ) { let mark = new Array ( a [ 0 ] * n ) ; mark . fill ( 0 ) ; let start = 0 ; let possible = 1 ; while ( start >= 0 && start < n ) { if ( s == " " ) { if ( mark [ start ] == 0 ) { mark [ start ] = 1 ; start -= a [ start ] ; } else { possible = 0 ; break ; } } else { if ( mark [ start ] == 0 ) { mark [ start ] = 1 ; start += a [ start ] ; } else { possible = 0 ; break ; } } } if ( possible == 0 ) document . write ( " " + " " ) ; else document . write ( " " ) ; } let n = 2 ; let s = " " ; let a = [ 1 , 2 ] ; checkingPossibility ( a , n , s ) ;
Simplify the directory path ( Unix like ) | function to simplify a Unix - styled absolute path ; using vector in place of stack ; forming the current directory . ; if " . . " , we pop . ; do nothing ( added for better understanding . ) ; push the current directory into the vector . ; forming the ans ; vector is empty ; absolute path which we have to simplify .
function simplify ( path ) { let v = [ ] ; let n = path . length ; let ans = " " ; for ( let i = 0 ; i < n ; i ++ ) { let dir = " " ; while ( i < n && path [ i ] != ' ' ) { dir += path [ i ] ; i ++ ; } if ( dir == " " ) { if ( v . length > 0 ) v . pop ( ) ; } else if ( dir == " " dir == " " ) { } else { v . push ( dir ) ; } } for ( let i of v ) { ans += " " + i ; } if ( ans == " " ) return " " ; return ans ; } let Str = " " ; let res = simplify ( Str ) ; document . write ( res ) ;
Length of the smallest sub | Javascript program to find the length of the smallest substring consisting of maximum distinct characters ; Find maximum distinct characters in any string ; Initialize all character 's  count with 0 ; Increase the count in array if a character is found ; Size of given string ; Find maximum distinct characters in any string ; Result ; Brute force approach to find all substrings ; We have to check here both conditions together 1. substring ' s ▁ distinct ▁ characters ▁ is ▁ equal ▁ ▁ to ▁ maximum ▁ distinct ▁ characters ▁ ▁ 2 . ▁ substring ' s length should be minimum ; Input string
let NO_OF_CHARS = 256 ; function max_distinct_char ( str , n ) { let count = new Array ( NO_OF_CHARS ) ; for ( let i = 0 ; i < count . length ; i ++ ) { count [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { count [ str [ i ] . charCodeAt ( 0 ) ] ++ ; } let max_distinct = 0 ; for ( let i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( count [ i ] != 0 ) { max_distinct ++ ; } } return max_distinct ; } function smallesteSubstr_maxDistictChar ( str ) { let n = str . length ; let max_distinct = max_distinct_char ( str , n ) ; let minl = n ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { let subs = null ; if ( i < j ) subs = str . substring ( i , j ) ; else subs = str . substring ( j , i ) ; let subs_lenght = subs . length ; let sub_distinct_char = max_distinct_char ( subs , subs_lenght ) ; if ( subs_lenght < minl && max_distinct == sub_distinct_char ) { minl = subs_lenght ; } } } return minl ; } let str = " " ; let len = smallesteSubstr_maxDistictChar ( str ) ; document . write ( " " + " " + " " + len ) ;
Evaluate a boolean expression represented as string | Evaluates boolean expression and returns the result ; Traverse all operands by jumping a character after every iteration . ; If operator next to current operand is AND . ; If operator next to current operand is OR . ; If operator next to current operand is XOR ( Assuming a valid input ) ; Driver code
function evaluateBoolExpr ( s ) { let n = s . length ; for ( let i = 0 ; i < n ; i += 2 ) { if ( i + 1 < n && i + 2 < n ) { if ( s [ i + 1 ] == ' ' ) { if ( s [ i + 2 ] == ' ' s [ i ] == 0 ) { s [ i + 2 ] = ' ' ; } else { s [ i + 2 ] = ' ' ; } } else if ( ( i + 1 ) < n && s [ i + 1 ] == ' ' ) { if ( s [ i + 2 ] == ' ' s [ i ] == ' ' ) { s [ i + 2 ] = ' ' ; } else { s [ i + 2 ] = ' ' ; } } else { if ( s [ i + 2 ] == s [ i ] ) { s [ i + 2 ] = ' ' ; } else { s [ i + 2 ] = ' ' ; } } } } return ( s [ n - 1 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; } let s = " " ; let sb = s . split ( ' ' ) ; document . write ( evaluateBoolExpr ( sb ) + " " ) ;
Efficiently find first repeated character in a string without using any additional data structure in one traversal | Returns - 1 if all characters of str are unique . Assumptions : ( 1 ) str contains only characters from ' a ' to ' z ' ( 2 ) integers are stored using 32 bits ; An integer to store presence / absence of 26 characters using its 32 bits . ; If bit corresponding to current character is already set ; set bit in checker ; Driver code
function FirstRepeated ( str ) { let checker = 0 ; for ( let i = 0 ; i < str . length ; ++ i ) { let val = ( str [ i ] - ' ' ) ; if ( ( checker & ( 1 << val ) ) > 0 ) return i ; checker |= ( 1 << val ) ; } return - 1 ; } let s = " " ; let i = FirstRepeated ( s ) ; if ( i != - 1 ) document . write ( " " + s [ i ] + " " + i ) ; else document . write ( " " ) ;
Check if a string is Pangrammatic Lipogram | collection of letters ; Function to check for a Pangrammatic Lipogram ; Convert string to lowercase ; Variable to keep count of all the letters not found in the string ; Traverses the string for every letter of the alphabet ; If character not found in string then increment count ; Driver code
let alphabets = " " ; function panLipogramChecker ( s ) { for ( let i = 0 ; i < s . length ; i ++ ) { s [ i ] = s [ i ] . toLowerCase ; } let counter = 0 ; for ( let i = 0 ; i < 26 ; i ++ ) { let pos = s . search ( alphabets [ i ] ) ; if ( pos < 0 pos > s . length ) counter += 1 ; } if ( counter == 0 ) document . write ( " " ) ; else if ( counter >= 2 ) document . write ( " " + " " ) ; else document . write ( " " ) ; } let str = " " + " " ; panLipogramChecker ( str ) ; document . write ( " " ) ; str = " " + " " ; panLipogramChecker ( str ) ; document . write ( " " ) ; str = " " + " " ; panLipogramChecker ( str ) ;
Split numeric , alphabetic and special symbols from a String | Javascript program to split an alphanumeric string using stringbuffer ; Driver method
function splitString ( str ) { let alpha = " " ; let num = " " ; let special = " " ; for ( let i = 0 ; i < str . length ; i ++ ) { if ( ! isNaN ( String ( str [ i ] ) * 1 ) ) num += str [ i ] ; else if ( ( str [ i ] >= ' ' && str [ i ] <= ' ' ) || ( str [ i ] >= ' ' && str [ i ] <= ' ' ) ) alpha += str [ i ] ; else special += str [ i ] ; } document . write ( alpha + " " ) ; document . write ( num + " " ) ; document . write ( special + " " ) ; } let str = " " ; splitString ( str ) ;
Program to print all substrings of a given string | Function to print all substring ; Pick starting point in outer loop and lengths of different strings for a given starting point ; Driver code
function SubString ( str , n ) { for ( var i = 0 ; i < n ; i ++ ) for ( var j = i + 1 ; j <= n ; j ++ ) document . write ( str . substring ( i , j ) + " " ) ; } var str = " " ; SubString ( str , str . length ) ;
Program to print all substrings of a given string | JavaScript program for the above approach ; finding the length of the string ; outermost for loop this is for the selection of starting point ; 2 nd for loop is for selection of ending point ; 3 rd loop is for printing from starting point to ending point ; changing the line after printing from starting point to ending point ; Driver Code ; calling method for printing substring
function printSubstrings ( str ) { var n = str . length ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i ; j < n ; j ++ ) { for ( var k = i ; k <= j ; k ++ ) { document . write ( str . charAt ( k ) ) ; } document . write ( " " ) ; } } } var str = " " ; printSubstrings ( str ) ;
Maximum Tip Calculator | JavaScript implementation of the approach ; Recursive function to calculate sum of maximum tip order taken by X and Y ; When all orders have been taken ; When X cannot take more orders ; When Y cannot take more orders ; When both can take order calculate maximum out of two ; Driver code
let n ; function solve ( i , X , Y , a , b , n ) { if ( i == n ) return 0 ; if ( X <= 0 ) return b [ i ] + solve ( i + 1 , X , Y - 1 , a , b , n ) ; if ( Y <= 0 ) return a [ i ] + solve ( i + 1 , X - 1 , Y , a , b , n ) ; else return Math . max ( a [ i ] + solve ( i + 1 , X - 1 , Y , a , b , n ) , b [ i ] + solve ( i + 1 , X , Y - 1 , a , b , n ) ) ; } let a = [ 1 , 2 , 3 , 4 , 5 ] ; let b = [ 5 , 4 , 3 , 2 , 1 ] ; n = a . length ; let x = 3 , y = 3 ; document . write ( solve ( 0 , x , y , a , b , n ) ) ;