codeparrot/xlcost-text-to-code huggingface dataset:
Remove minimum elements from either side such that 2 * min becomes more than max | Returns the minimum number of removals from either end in arr [ l . . h ] so that 2 * min becomes greater than max . ; Initialize starting and ending indexes of the maximum sized subarray with property 2 * min > max ; Choose different elements as starting point ; Initialize min and max for the current start ; Choose different ending points for current start ; Update min and max if necessary ; If the property is violated , then no point to continue for a bigger array ; Update longest_start and longest_end if needed ; If not even a single element follow the property , then return n ; Return the number of elements to be removed ; Driver program to test above functions
function minRemovalsDP ( arr , n ) { let longest_start = - 1 , longest_end = 0 ; for ( let start = 0 ; start < n ; start ++ ) { let min = Number . MAX_VALUE , max = Number . MIN_VALUE ; for ( let end = start ; end < n ; end ++ ) { let val = arr [ end ] ; if ( val < min ) { min = val ; } if ( val > max ) { max = val ; } if ( 2 * min <= max ) { break ; } if ( end - start > longest_end - longest_start longest_start == - 1 ) { longest_start = start ; longest_end = end ; } } } if ( longest_start == - 1 ) { return n ; } return ( n - ( longest_end - longest_start + 1 ) ) ; } let arr = [ 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 ] ; let n = arr . length ; document . write ( minRemovalsDP ( arr , n ) ) ;
Longest Arithmetic Progression | DP | The function returns true if there exist three elements in AP Assumption : set [ 0. . n - 1 ] is sorted . The code strictly implements the algorithm provided in the reference . ; One by fix every element as middle element ; Initialize i and k for the current j ; Find if there exist i and k that form AP with j as middle element
function arithmeticThree ( set , n ) { for ( let j = 1 ; j < n - 1 ; j ++ ) { let i = j - 1 , k = j + 1 ; while ( i >= 0 && k <= n - 1 ) { if ( set [ i ] + set [ k ] == 2 * set [ j ] ) return true ; ( set [ i ] + set [ k ] < 2 * set [ j ] ) ? k ++ : i -- ; } } return false ; }
Optimal Strategy for a Game | DP | Returns optimal value possible that a player can collect from an array of coins of size n . Note than n must be even ; Create a table to store solutions of subproblems ; Fill table using above recursive formula . Note that the tableis filled in diagonal fashion ( similar to http : goo . gl / PQqoS ) , from diagonal elements to table [ 0 ] [ n - 1 ] which is the result . ; Here x is value of F ( i + 2 , j ) , y is F ( i + 1 , j - 1 ) and z is F ( i , j - 2 ) in above recursive formula ; Driver code
function optimalStrategyOfGame ( arr , n ) { let table = new Array ( n ) ; let gap , i , j , x , y , z ; for ( let d = 0 ; d < n ; d ++ ) { table [ d ] = new Array ( n ) ; } for ( gap = 0 ; gap < n ; ++ gap ) { for ( i = 0 , j = gap ; j < n ; ++ i , ++ j ) { x = ( ( i + 2 ) <= j ) ? table [ i + 2 ] [ j ] : 0 ; y = ( ( i + 1 ) <= ( j - 1 ) ) ? table [ i + 1 ] [ j - 1 ] : 0 ; z = ( i <= ( j - 2 ) ) ? table [ i ] [ j - 2 ] : 0 ; table [ i ] [ j ] = Math . max ( arr [ i ] + Math . min ( x , y ) , arr [ j ] + Math . min ( y , z ) ) ; } } return table [ 0 ] [ n - 1 ] ; } let arr1 = [ 8 , 15 , 3 , 7 ] ; let n = arr1 . length ; document . write ( " " + optimalStrategyOfGame ( arr1 , n ) + " " ) ; let arr2 = [ 2 , 2 , 2 , 2 ] ; n = arr2 . length ; document . write ( " " + optimalStrategyOfGame ( arr2 , n ) + " " ) ; let arr3 = [ 20 , 30 , 2 , 2 , 2 , 10 ] ; n = arr3 . length ; document . write ( " " + optimalStrategyOfGame ( arr3 , n ) ) ;
Maximum Sum Increasing Subsequence | DP | maxSumIS ( ) returns the maximum sum of increasing subsequence in arr [ ] of size n ; Initialize msis values for all indexes ; Compute maximum sum values in bottom up manner ; Pick maximum of all msis values ; Driver Code
function maxSumIS ( arr , n ) { let i , j , max = 0 ; let msis = new Array ( n ) ; for ( i = 0 ; i < n ; i ++ ) msis [ i ] = arr [ i ] ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && msis [ i ] < msis [ j ] + arr [ i ] ) msis [ i ] = msis [ j ] + arr [ i ] ; for ( i = 0 ; i < n ; i ++ ) if ( max < msis [ i ] ) max = msis [ i ] ; return max ; } let arr = [ 1 , 101 , 2 , 3 , 100 , 4 , 5 ] ; let n = arr . length ; document . write ( " " + " " + maxSumIS ( arr , n ) ) ;
Find position i to split Array such that prefix sum till i | Function to check if there is an element forming G . P . series having common ratio k ; If size of array is less than three then return - 1 ; Initialize the variables ; Calculate total sum of array ; Calculate Middle element of G . P . series ; Iterate over the range ; Store the first element of G . P . series in the variable temp ; Return position of middle element of the G . P . series if the first element is in G . P . of common ratio k ; Else return 0 ; if middle element is not found in arr [ ] ; Given array
function checkArray ( arr , N , k ) { if ( N < 3 ) return - 1 ; let i , Sum = 0 , temp = 0 ; for ( i = 0 ; i < N ; i ++ ) Sum += arr [ i ] ; let R = ( k * k + k + 1 ) ; if ( Sum % R != 0 ) return 0 ; let Mid = k * ( Sum / R ) ; for ( i = 1 ; i < N - 1 ; i ++ ) { temp += arr [ i - 1 ] ; if ( arr [ i ] == Mid ) { if ( temp == Mid / k ) return i + 1 ; else return 0 ; } } return 0 ; } let arr = [ 5 , 1 , 4 , 20 , 6 , 15 , 9 , 10 ] ; let N = arr . length ; let K = 2 ; document . write ( checkArray ( arr , N , K ) + " " ) ;
Repeat last occurrence of each alphanumeric character to their position in character family times | Function to encode the given string ; Variable string to store the result ; Arrays to store the last occuring index of every character in the string ; Length of the string ; Iterate over the range ; If str [ i ] is between 0 and 9 ; If str [ i ] is between a and z ; If str [ i ] is between A and Z ; Iterate over the range ; If str [ i ] is between a and z and i is the last occurence in str ; If str [ i ] is between A and Z and i is the last occurence in str ; If str [ i ] is between 0 and 9 and i is the last occurence in str ; Print the result ; Driver Code
function encodeString ( str ) { let res = " " ; let small = new Array ( 26 ) . fill ( 0 ) , capital = new Array ( 26 ) . fill ( 0 ) , num = new Array ( 26 ) . fill ( 0 ) ; let n = str . length ; for ( let i = 0 ; i < n ; i ++ ) { if ( str [ i ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) { num [ str [ i ] . charCodeAt ( 0 ) - 48 ] = i ; } else if ( str [ i ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) { small [ str [ i ] . charCodeAt ( 0 ) - 97 ] = i ; } else if ( str [ i ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) { capital [ str [ i ] . charCodeAt ( 0 ) - 65 ] = i ; } } for ( let i = 0 ; i < n ; i ++ ) { if ( ( str [ i ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) && small [ str [ i ] . charCodeAt ( 0 ) - 97 ] == i ) { let occ = str [ i ] . charCodeAt ( 0 ) - 96 ; while ( occ -- ) { res += str [ i ] ; } } else if ( ( str [ i ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) && capital [ str [ i ] . charCodeAt ( 0 ) - 65 ] == i ) { let occ = str [ i ] . charCodeAt ( 0 ) - 64 ; while ( occ -- ) { res += str [ i ] ; } } else if ( ( str [ i ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && str [ i ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) && num [ str [ i ] . charCodeAt ( 0 ) - 48 ] == i ) { let occ = str [ i ] . charCodeAt ( 0 ) - 48 ; while ( occ -- ) { res += str [ i ] ; } } else { res += str [ i ] ; } } document . write ( res ) ; } let str = " " ; encodeString ( str ) ;
Make Array elements equal by replacing adjacent elements with their XOR | Function to check if it is possible to make all the array elements equal using the given operation ; Stores the prefix XOR array ; Calculate prefix [ i ] ; Case 1 , check if the XOR of the input array is 0 ; Case 2 Iterate over all the ways to divide the array into three non empty subarrays ; XOR of Middle Block ; XOR of Left Block ; XOR of Right Block ; Not Possible ; Driver Code ; Function Call
function possibleEqualArray ( A , N ) { let pref = new Array ( N ) ; pref [ 0 ] = A [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { pref [ i ] = pref [ i - 1 ] ^ A [ i ] ; } if ( pref [ N - 1 ] == 0 ) { document . write ( " " ) ; return ; } let cur_xor = 0 ; for ( let i = N - 1 ; i >= 0 ; i -- ) { cur_xor ^= A [ i ] ; for ( let j = 0 ; j < i ; j ++ ) { if ( j ) { let middle_xor = pref [ j - 1 ] ^ pref [ i - 1 ] ; let left_xor = pref [ j - 1 ] ; let right_xor = cur_xor ; if ( left_xor == middle_xor && middle_xor == right_xor ) { document . write ( " " ) ; return ; } } } } document . write ( " " ) ; } let A = [ 0 , 2 , 2 ] ; let N = A . length ; possibleEqualArray ( A , N ) ;
Construct an array whose Prefix XOR array starting from X is an N | Function to print the required array ; Iteratie from 1 to N ; Print the i - th element ; Update prev_xor to i ; Given Input ; Function Call
function GenerateArray ( N , X ) { let prev_xor = X ; for ( let i = 1 ; i <= N ; i ++ ) { document . write ( ( i ^ prev_xor ) ) ; if ( i != N ) { document . write ( " " ) ; } prev_xor = i ; } } let N = 4 , X = 3 ; document . write ( " " ) ; GenerateArray ( N , X ) ;
Check if elements of a Binary Matrix can be made alternating | Function to create the possible grids ; Function to test if any one of them matches with the given 2 - D array ; Function to print the grid , if possible ; Function to check if the grid can be made alternating or not ; Grids to store the possible grids ; Driver Code
function createGrid ( grid , is1 , N , M ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( is1 ) { grid [ i ] [ j ] = " " ; is1 = false ; } else { grid [ i ] [ j ] = " " ; is1 = true ; } } if ( M % 2 == 0 ) is1 = ! is1 ; } } function testGrid ( testGrid , Grid , N , M ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( Grid [ i ] [ j ] != " " ) { if ( Grid [ i ] [ j ] != testGrid [ i ] [ j ] ) { return false ; } } } } return true ; } function printGrid ( grid , N , M ) { for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { document . write ( grid [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } function findPossibleGrid ( N , M , grid ) { let gridTest1 = new Array ( N ) . fill ( 0 ) . map ( ( ) => new Array ( 1001 ) ) ; let gridTest2 = new Array ( N ) . fill ( 0 ) . map ( ( ) => new Array ( 1001 ) ) ; createGrid ( gridTest1 , true , N , M ) ; createGrid ( gridTest2 , false , N , M ) ; if ( testGrid ( gridTest1 , grid , N , M ) ) { document . write ( " " ) ; printGrid ( gridTest1 , N , M ) ; } else if ( testGrid ( gridTest2 , grid , N , M ) ) { document . write ( " " ) ; printGrid ( gridTest2 , N , M ) ; } else { document . write ( " " ) ; } } let N = 4 , M = 4 ; let grid = [ [ " " , " " , " " , " " ] , [ " " , " " , " " , " " ] , [ " " , " " , " " , " " ] , [ " " , " " , " " , " " ] , ] ; findPossibleGrid ( N , M , grid ) ;
Find non | JavaScript program for the above approach ; Function to find the possible output array ; Base case for the recursion ; If ind becomes half of the size then print the array . ; Exit the function . ; Iterate in the range . ; Put the values in the respective indices . ; Call the function to find values for other indices . ; Driver Code
const INF64 = 1000000000000000000 ; const N = 200 * 1000 + 13 ; let n ; let arr = Array ( N ) ; let brr = Array ( N ) ; function brute ( ind , l , r ) { if ( ind == n / 2 ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( brr [ i ] + " " ) ; exit ( 0 ) ; } for ( let i = l ; i <= arr [ ind ] / 2 ; ++ i ) if ( arr [ ind ] - i <= r ) { brr [ ind ] = i ; brr [ n - ind - 1 ] = arr [ ind ] - i ; brute ( ind + 1 , i , arr [ ind ] - i ) ; } } n = 2 ; n *= 2 ; arr [ 0 ] = 5 ; arr [ 1 ] = 6 ; brute ( 0 , 0 , INF64 ) ;
Find and Replace all occurrence of a substring in the given String | Function to replace all the occurrences of the sublet S1 to S2 in let S ; Stores the resultant let ; Traverse the let s ; If the first character of let s1 matches with the current character in let s ; If the complete let matches or not ; If complete let matches then replace it with the let s2 ; Otherwise ; Otherwise ; Print the resultant let ; Driver Code
function modifylet ( s , s1 , s2 ) { let ans = " " ; for ( let i = 0 ; i < s . length ; i ++ ) { let k = 0 ; if ( s [ i ] == s1 [ k ] && i + s1 . length <= s . length ) { let j ; for ( j = i ; j < i + s1 . length ; j ++ ) { if ( s [ j ] != s1 [ k ] ) { break ; } else { k = k + 1 ; } } if ( j == i + s1 . length ) { ans = ans + s2 ; i = j - 1 ; } else { ans = ans + s [ i ] ; } } else { ans = ans + s [ i ] ; } } document . write ( ans ) ; } let S = " " ; let S1 = " " ; let S2 = " " ; modifylet ( S , S1 , S2 ) ;
Count of minimum numbers having K as the last digit required to obtain sum N | JavaScript program for the above approach ; Stores the smallest number that ends with digit i ( 0 , 9 ) ; Stores the minimum number of steps to create a number ending with digit i ; Initialize elements as infinity ; Minimum number ending with digit i ; Minimum steps to create a number ending with digit i ; If N < SmallestNumber then , return - 1 ; Otherwise , return answer ; Driver Code
function minCount ( N , K ) { let SmallestNumber = new Array ( 10 ) ; let MinimumSteps = new Array ( 10 ) ; for ( let i = 0 ; i <= 9 ; i ++ ) { SmallestNumber [ i ] = Number . MAX_VALUE ; MinimumSteps [ i ] = Number . MAX_VALUE ; } for ( let i = 1 ; i <= 10 ; i ++ ) { let num = K * i ; SmallestNumber [ num % 10 ] = Math . min ( SmallestNumber [ num % 10 ] , num ) ; MinimumSteps [ num % 10 ] = Math . min ( MinimumSteps [ num % 10 ] , i ) ; } if ( N < SmallestNumber [ N % 10 ] ) { return - 1 ; } else { return MinimumSteps [ N % 10 ] ; } } let N = 42 , K = 7 ; document . write ( minCount ( N , K ) ) ;
Minimum number of operations required to make an array non | Function to count the minimum number of steps required to make arr non - decreasing ; Stores differences ; Stores the max number ; Traverse the array arr [ ] ; Update mx ; Update val ; Stores the result ; Iterate until 2 ^ res - 1 is less than val ; Return the answer ; Given input ; Function call
function countMinSteps ( arr , N ) { let val = 0 ; let mx = Number . MIN_SAFE_INTEGER ; for ( let i = 0 ; i < N ; i ++ ) { let curr = arr [ i ] ; mx = Math . max ( mx , curr ) ; val = Math . max ( val , mx - curr ) ; } let res = 0 ; while ( ( 1 << res ) - 1 < val ) { ++ res ; } return res ; } let arr = [ 1 , 7 , 6 , 5 ] ; let N = arr . length ; document . write ( countMinSteps ( arr , N ) ) ;
Minimum XOR of at most K elements in range [ L , R ] | Function for K = 2 ; Function for K = 2 ; Function for K = 2 ; Function to calculate the minimum XOR of at most K elements in [ L , R ] ; Input ; Function call
function func2 ( L , R , K ) { if ( R - L >= 2 ) return 1 ; return Mat . min ( L , L ^ R ) ; } function func3 ( L , R , K ) { if ( ( R ^ L ) > L && ( R ^ L ) < R ) return 0 ; return func2 ( L , R , K ) ; } function func4 ( L , R , K ) { if ( R - L >= 4 ) return 0 ; var minval = L ^ ( L + 1 ) ^ ( L + 2 ) ^ ( L + 3 ) ; return Math . min ( minval , func3 ( L , R , K ) ) ; } function minimumXor ( L , R , K ) { if ( K > 4 ) return 0 ; else if ( K == 4 ) return func4 ( L , R , K ) ; else if ( K == 3 ) return func3 ( L , R , K ) ; else if ( K == 2 ) return func2 ( L , R , K ) ; else return L ; } var L = 1 , R = 3 , K = 3 ; document . write ( minimumXor ( L , R , K ) ) ;
Nth term of Ruler Function Series | Function to count the number of set bits in the number N ; Store the number of setbits ; Update the value of n ; Update the count ; Return the total count ; Function to find the Nth term of the Ruler Function Series ; Store the result ; Print the result ; Driver code
function setBits ( n ) { var count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } function findNthTerm ( N ) { var x = setBits ( N ^ ( N - 1 ) ) ; document . write ( x ) ; } var N = 8 ; findNthTerm ( N ) ;
Quadratic equation whose roots are reciprocal to the roots of given equation | Function to find the quadratic equation having reciprocal roots ; Print quadratic equation ; Given coefficients ; Function call to find the quadratic equation having reciprocal roots
function findEquation ( A , B , C ) { document . write ( " " + C + " " + " " + B + " " + A + " " ) } let A = 1 , B = - 5 , C = 6 ; findEquation ( A , B , C ) ;
Minimize subtraction followed by increments of adjacent elements required to make all array elements equal | Function to find the minimum number of moves required to make all array elements equal ; Store the total sum of the array ; Calculate total sum of the array ; If the sum is not divisible by N , then print " - 1" ; Stores the average ; Stores the count of operations ; Traverse the array arr [ ] ; Update number of moves required to make current element equal to avg ; Update the overall count ; Return the minimum operations required ; Driver Code
function findMinMoves ( arr , N ) { let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum % N != 0 ) return - 1 ; let avg = sum / N ; let total = 0 ; let needCount = 0 ; for ( let i = 0 ; i < N ; i ++ ) { needCount += ( arr [ i ] - avg ) ; total = Math . max ( Math . max ( Math . abs ( needCount ) , arr [ i ] - avg ) , total ) ; } return total ; } let arr = [ 1 , 0 , 5 ] ; let N = arr . length ; document . write ( findMinMoves ( arr , N ) )
Maximum amount of money that can be collected by a player in a game of coins | Function to calculate the maximum amount collected by A ; Stores the money obtained by A ; Stores mid elements of odd sized rows ; Size of current row ; Increase money collected by A ; Add coins at even indices to the amount collected by A ; Print the amount ; Driver Code ; Function call to calculate the amount of coins collected by A
function find ( N , Arr ) { var amount = 0 ; var mid_odd = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { var siz = Arr [ i ] . length ; for ( var j = 0 ; j < siz / 2 ; j ++ ) amount = amount + Arr [ i ] [ j ] ; if ( siz % 2 == 1 ) mid_odd . push ( Arr [ i ] [ siz / 2 ] ) ; } mid_odd . sort ( ( a , b ) => a - b ) for ( var i = 0 ; i < mid_odd . length ; i ++ ) if ( i % 2 == 0 ) amount = amount + mid_odd [ i ] ; document . write ( amount + " " ) ; } var N = 2 ; var Arr = [ [ 5 , 2 , 3 , 4 ] , [ 1 , 6 ] ] ; find ( N , Arr ) ;
Check if an array can be split into K consecutive non | Function to check if array can be split into K consecutive and non - overlapping subarrays of length M consisting of a single distinct element ; Traverse over the range [ 0 , N - M - 1 ] ; Check if arr [ i ] is the same as arr [ i + m ] ; Increment current length t of pattern matched by 1 ; Check if t is equal to m , increment count of total repeated pattern ; Return true if length of total repeated pattern is k ; Update length of the current pattern ; Update count to 1 ; Finally return false if no pattern found ; Driver Code
function checkPattern ( arr , m , k , n ) { let count = 1 , t = 0 ; for ( let i = 0 ; i < n - m ; i ++ ) { if ( arr [ i ] == arr [ i + m ] ) { t ++ ; if ( t == m ) { t = 0 ; count ++ ; if ( count == k ) { return " " ; } } } else { t = 0 ; count = 1 ; } } return " " ; } let arr = [ 6 , 1 , 3 , 3 , 3 , 3 ] ; let M = 1 , K = 3 ; let N = arr . length ; document . write ( checkPattern ( arr , M , K , N ) ) ;
Move all digits to the beginning of a given string | Function to move all the digit to the beginning of the string ; Calculate the string length ; Stores the digits ; Stores the non - numeric character ; Traverse the string and check if there is a digit ; If character is a digit , add it to the string digits ; Otherwise , add it to the string nonNumericCharacter ; Append both the strings ; Print the string ; Given String str
function moveAllDigitAtBeginning ( str ) { var len = str . length ; var digits = [ ] ; var nonNumericCharacter = [ ] ; var temp = str . split ( " " ) ; for ( const c of temp ) { if ( c . charCodeAt ( 0 ) >= 48 && c . charCodeAt ( 0 ) <= 57 ) { digits . push ( c ) ; } else { nonNumericCharacter . push ( c ) ; } } digits . push ( nonNumericCharacter . join ( " " ) ) ; document . write ( digits . join ( " " ) + " " ) ; } var str = " " ; moveAllDigitAtBeginning ( str ) ;
Remove all occurrences of a word from a given string using Z | Function to fill the Z - array for str ; L Stores start index of window which matches with prefix of str ; R Stores end index of window which matches with prefix of str ; Iterate over the characters of str ; If i is greater thn R ; Update L and R ; If subString match with prefix ; Update R ; Update Z [ i ] ; Update R ; Update k ; if Z [ k ] is less than remaining interval ; Update Z [ i ] ; Start from R and check manually ; Update R ; Update Z [ i ] ; Update R ; Function to remove all the occurrences of word from str ; Create concatenated String " P $ T " ; Store Z array of concat ; Stores String , str by removing all the occurrences of word from str ; Stores length of word ; Traverse the array , Z [ ] ; if Z [ i + pSize + 1 ] equal to length of word ; Update i ; Driver Code
function getZarr ( str , Z ) { let n = str . length ; let k ; let L = 0 ; let R = 0 ; for ( let i = 1 ; i < n ; ++ i ) { if ( i > R ) { L = R = i ; while ( R < n && str [ R - L ] == str [ R ] ) { R ++ ; } Z [ i ] = R - L ; R -- ; } else { k = i - L ; if ( Z [ k ] < R - i + 1 ) { Z [ i ] = Z [ k ] ; } else { L = i ; while ( R < n && str [ R - L ] == str [ R ] ) { R ++ ; } Z [ i ] = R - L ; R -- ; } } } } function goodStr ( str , word ) { let concat = word + " " + str ; let l = concat . length ; let Z = new Array ( l ) ; getZarr ( concat , Z ) ; let res = " " ; let pSize = word . length ; for ( let i = 0 ; i < l ; ++ i ) { if ( i + pSize < l - 1 && Z [ i + pSize + 1 ] == pSize ) { i += pSize - 1 ; } else if ( i < str . length ) { res += str [ i ] ; } } return res ; } let str = " " ; let word = " " ; document . write ( goodStr ( str , word ) ) ;
Count of substrings of a string containing another given string as a substring | Set 2 | Function to count the substrings of string containing another given string as a substring ; Store length of string S ; Store length of string T ; Store the required count of substrings ; Store the starting index of last occurence of T in S ; Iterate in range [ 0 , n1 - n2 ] ; Check if substring from i to i + n2 is equal to T ; Check if substring from i to i + n2 is equal to T ; Mark chk as false and break the loop ; If chk is true ; Add ( i + 1 - last ) * ( n1 - ( i + n2 - 1 ) ) to answer ; Update the last to i + 1 ; Prlet the answer ; Driver Code ; Function Call
function findOccurrences ( S , T ) { let n1 = S . length ; let n2 = T . length ; let ans = 0 ; let last = 0 ; for ( let i = 0 ; i <= n1 - n2 ; i ++ ) { let chk = true ; for ( let j = 0 ; j < n2 ; j ++ ) { if ( T [ j ] != S [ i + j ] ) { chk = false ; break ; } } if ( chk ) { ans += ( i + 1 - last ) * ( n1 - ( i + n2 - 1 ) ) ; last = i + 1 ; } } document . write ( ans ) ; } let S = " " , T = " " ; findOccurrences ( S , T ) ;