codeparrot/xlcost-text-to-code huggingface dataset:
Check if 2 * K + 1 non | C # program for the above approach ; Function to check if the string S can be obtained by ( K + 1 ) non - empty substrings whose concatenation and concatenation of the reverse of these K strings ; Stores the size of the string ; If n is less than 2 * k + 1 ; Stores the first K characters ; Stores the last K characters ; Reverse the string ; If both the strings are equal ; Driver Code
using System ; class GFG { static void checkString ( string s , int k ) { int n = s . Length ; if ( 2 * k + 1 > n ) { Console . Write ( " No " ) ; return ; } string a = s . Substring ( 0 , k ) ; string b = s . Substring ( n - k , k ) ; char [ ] arr = b . ToCharArray ( ) ; Array . Reverse ( arr ) ; b = new String ( arr ) ; if ( a == b ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( ) { string S = " qwqwq " ; int K = 1 ; checkString ( S , K ) ; } }
Minimum number of socks required to picked to have at least K pairs of the same color | C # program for the above approach ; Function to count the minimum number of socks to be picked ; Stores the total count of pairs of socks ; Find the total count of pairs ; If K is greater than pairs ; Otherwise ; Driver Code
using System ; class GFG { public static int findMin ( int [ ] arr , int N , int k ) { int pairs = 0 ; for ( int i = 0 ; i < N ; i ++ ) { pairs += arr [ i ] / 2 ; } if ( k > pairs ) return - 1 ; else return 2 * k + N - 1 ; } public static void Main ( string [ ] args ) { int [ ] arr = { 4 , 5 , 6 } ; int K = 3 ; int N = arr . Length ; Console . WriteLine ( findMin ( arr , N , K ) ) ; } }
Minimum number of bits required to be flipped such that Bitwise OR of A and B is equal to C | C # program for the above approach ; Function to count the number of bit flips required on A and B such that Bitwise OR of A and B is C ; Stores the count of flipped bit ; Iterate over the range [ 0 , 32 ] ; Check if i - th bit of A is set ; Check if i - th bit of B is set or not ; Check if i - th bit of C is set or not ; If i - th bit of C is unset ; Check if i - th bit of A is set or not ; Check if i - th bit of B is set or not ; Check if i - th bit of C is set or not ; Check if i - th bit of both A and B is set ; Return the count of bits flipped ; Driver Code
using System ; class GFG { static int minimumFlips ( int A , int B , int C ) { int res = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { int x = 0 , y = 0 , z = 0 ; if ( ( A & ( 1 << i ) ) != 0 ) { x = 1 ; } if ( ( B & ( 1 << i ) ) != 0 ) { y = 1 ; } if ( ( C & ( 1 << i ) ) != 0 ) { z = 1 ; } if ( z == 0 ) { if ( x == 1 ) { res ++ ; } if ( y == 1 ) { res ++ ; } } if ( z == 1 ) { if ( x == 0 && y == 0 ) { res ++ ; } } } return res ; } public static void Main ( string [ ] args ) { int A = 2 , B = 6 , C = 5 ; Console . WriteLine ( minimumFlips ( A , B , C ) ) ; } }
Queries to calculate Bitwise AND of an array with updates | C # program for the above approach ; Store the number of set bits at each position ; Function to precompute the prefix count array ; Iterate over the range [ 0 , 31 ] ; Set the bit at position i if arr [ 0 ] is set at position i ; Traverse the array and take prefix sum ; Update prefixCount [ i ] [ j ] ; Function to find the Bitwise AND of all array elements ; Stores the required result ; Iterate over the range [ 0 , 31 ] ; Stores the number of set bits at position i ; If temp is N , then set ith position in the result ; Print the result ; Function to update the prefix count array in each query ; Iterate through all the bits of the current number ; Store the bit at position i in the current value and the new value ; If bit2 is set and bit1 is unset , then increase the set bits at position i by 1 ; If bit1 is set and bit2 is unset , then decrease the set bits at position i by 1 ; Function to find the bitwise AND of the array after each query ; Fill the prefix count array ; Traverse the queries ; Store the index and the new value ; Store the current element at the index ; Update the array element ; Apply the changes to the prefix count array ; Print the bitwise AND of the modified array ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int [ , ] prefixCount = new int [ 32 , 10000 ] ; static void findPrefixCount ( List < int > arr , int size ) { for ( int i = 0 ; i < 32 ; i ++ ) { prefixCount [ i , 0 ] = ( ( arr [ 0 ] >> i ) & 1 ) ; for ( int j = 1 ; j < size ; j ++ ) { prefixCount [ i , j ] = ( ( arr [ j ] >> i ) & 1 ) ; prefixCount [ i , j ] += prefixCount [ i , j - 1 ] ; } } } static void arrayBitwiseAND ( int size ) { int result = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { int temp = prefixCount [ i , size - 1 ] ; if ( temp == size ) result = ( result | ( 1 << i ) ) ; } Console . Write ( result + " ▁ " ) ; } static void applyQuery ( int currentVal , int newVal , int size ) { for ( int i = 0 ; i < 32 ; i ++ ) { int bit1 = ( ( currentVal >> i ) & 1 ) ; int bit2 = ( ( newVal >> i ) & 1 ) ; if ( bit2 > 0 && bit1 == 0 ) prefixCount [ i , size - 1 ] ++ ; else if ( bit1 > 0 && bit2 == 0 ) prefixCount [ i , size - 1 ] -- ; } } static void findbitwiseAND ( int [ , ] queries , List < int > arr , int N , int M ) { findPrefixCount ( arr , N ) ; for ( int i = 0 ; i < M ; i ++ ) { int id = queries [ i , 0 ] ; int newVal = queries [ i , 1 ] ; int currentVal = arr [ id ] ; arr [ id ] = newVal ; applyQuery ( currentVal , newVal , N ) ; arrayBitwiseAND ( N ) ; } } public static void Main ( ) { List < int > arr = new List < int > ( ) { 1 , 2 , 3 , 4 , 5 } ; int [ , ] queries = new int [ 3 , 2 ] { { 0 , 2 } , { 3 , 3 } , { 4 , 2 } } ; int N = arr . Count ; int M = 3 ; findbitwiseAND ( queries , arr , N , M ) ; } }
Find the winner of a game of donating i candies in every i | C # program for the above approach ; Function to find the winning player in a game of donating i candies to opponent in i - th move ; Steps in which number of candies of player A finishes ; Steps in which number of candies of player B finishes ; If A 's candies finishes first ; Otherwise ; Driver code ; Candies possessed by player A ; Candies possessed by player B
using System ; class GFG { static void stepscount ( int a , int b ) { int chanceA = 2 * a - 1 ; int chanceB = 2 * b ; if ( chanceA < chanceB ) { Console . Write ( " B " ) ; } else if ( chanceB < chanceA ) { Console . Write ( " A " ) ; } } static void Main ( ) { int A = 2 ; int B = 3 ; stepscount ( A , B ) ; } }
Check if a point is inside , outside or on a Hyperbola | C # program for the above approach ; Function to check if the point ( x , y ) lies inside , on or outside the given hyperbola ; Stores the value of the equation ; Generate output based on value of p ; Driver Code
using System ; class GFG { static void checkpoint ( int h , int k , int x , int y , int a , int b ) { int p = ( int ) ( Math . Pow ( ( x - h ) , 2 ) / Math . Pow ( a , 2 ) ) - ( int ) ( Math . Pow ( ( y - k ) , 2 ) / Math . Pow ( b , 2 ) ) ; if ( p > 1 ) { Console . WriteLine ( " Outside " ) ; } else if ( p == 1 ) { Console . WriteLine ( " On ▁ the ▁ Hyperbola " ) ; } else { Console . WriteLine ( " Inside " ) ; } } public static void Main ( string [ ] args ) { int h = 0 , k = 0 , x = 2 ; int y = 1 , a = 4 , b = 5 ; checkpoint ( h , k , x , y , a , b ) ; } }
Count subarrays having even Bitwise XOR | C # program for the above approach ; Function to count the number of subarrays having even Bitwise XOR ; Store the required result ; Generate subarrays with arr [ i ] as the first element ; Store XOR of current subarray ; Generate subarrays with arr [ j ] as the last element ; Calculate Bitwise XOR of the current subarray ; If XOR is even , increase ans by 1 ; Print the result ; Driver Code ; Given array ; Stores the size of the array ; Function Call
using System ; class GFG { static void evenXorSubarray ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int XOR = 0 ; for ( int j = i ; j < n ; j ++ ) { XOR = XOR ^ arr [ j ] ; if ( ( XOR & 1 ) == 0 ) ans ++ ; } } Console . WriteLine ( ans ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; evenXorSubarray ( arr , N ) ; } }
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order | C # program for the above approach ; Function to find the Kth element in the required permutation ; Store the required result ; If K is in the first N / 2 elements , print K * 2 ; Otherwise , K is greater than N / 2 ; If N is even ; If N is odd ; Print the required result ; Driver code ; functions call
using System ; class GFG { static void findKthElement ( int N , int K ) { int ans = 0 ; if ( K <= N / 2 ) { ans = K * 2 ; } else { if ( N % 2 == 0 ) { ans = ( K * 2 ) - N - 1 ; } else { ans = ( K * 2 ) - N ; } } Console . Write ( ans ) ; } static void Main ( ) { int N = 10 , K = 3 ; findKthElement ( N , K ) ; } }
Minimize replacements with values up to K to make sum of two given arrays equal | C # program to implement the above approach ; Function to find minimum number of replacements required to make the sum of two arrays equal ; Stores the sum of a [ ] ; Stores the sum of b [ ] ; Calculate sum of the array a [ ] ; Calculate sum of the array b [ ] ; Stores the difference between a [ ] and b [ ] ; Left and Right pointer to traverse the array a [ ] ; Left and Right pointer to traverse the array b [ ] ; Stores the count of moves ; Sort the arrays in ascending order ; Iterate while diff != 0 and l1 <= r1 or l2 <= r2 ; If diff is greater than 0 ; If all pointers are valid ; Otherwise , if only pointers of array a [ ] is valid ; Otherwise ; If diff is less than 0 ; If all pointers are valid ; Otherwise , if only pointers of array a [ ] is valid ; Otherwise ; Increment count of res by one ; If diff is 0 , then return res ; Otherwise , return - 1 ; Driver Code
using System ; public class GFG { public static int makeSumEqual ( int [ ] a , int [ ] b , int K , int M , int N ) { int sum1 = 0 ; int sum2 = 0 ; foreach ( int el in a ) sum1 += el ; foreach ( int el in b ) sum2 += el ; int diff = sum1 - sum2 ; int l1 = 0 , r1 = M - 1 ; int l2 = 0 , r2 = N - 1 ; int res = 0 ; Array . Sort ( a ) ; Array . Sort ( b ) ; while ( l1 <= r1 l2 <= r2 ) { if ( diff == 0 ) { break ; } if ( diff > 0 ) { if ( l2 <= r2 && l1 <= r1 ) { if ( K - b [ l2 ] < a [ r1 ] - 1 ) { int sub = Math . Min ( a [ r1 ] - 1 , diff ) ; diff -= sub ; a [ r1 ] -= sub ; r1 -- ; } else { int sub = Math . Min ( K - b [ l2 ] , diff ) ; diff -= sub ; b [ l2 ] += sub ; l2 ++ ; } } else if ( l1 <= r1 ) { int sub = Math . Min ( a [ r1 ] - 1 , diff ) ; diff -= sub ; a [ r1 ] -= sub ; r1 -- ; } else { int sub = Math . Min ( K - b [ l2 ] , diff ) ; diff -= sub ; b [ l2 ] += sub ; l2 ++ ; } } else { if ( l1 <= r1 && l2 <= r2 ) { if ( K - a [ l1 ] < b [ r2 ] - 1 ) { int sub = Math . Min ( b [ r2 ] - 1 , - 1 * diff ) ; diff += sub ; b [ r2 ] -= sub ; r2 -- ; } else { int sub = Math . Min ( K - a [ l1 ] , - 1 * diff ) ; diff += sub ; a [ l1 ] -= sub ; l1 ++ ; } } else if ( l2 <= r2 ) { int sub = Math . Min ( b [ r2 ] - 1 , - 1 * diff ) ; diff += sub ; b [ r2 ] -= sub ; r2 -- ; } else { int sub = Math . Min ( K - a [ l1 ] , diff ) ; diff += sub ; a [ l1 ] += sub ; l1 ++ ; } } res ++ ; } if ( diff == 0 ) return res ; else return - 1 ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 4 , 3 } ; int [ ] B = { 6 , 6 , 6 } ; int M = A . Length ; int N = B . Length ; int K = 6 ; Console . WriteLine ( makeSumEqual ( A , B , K , M , N ) ) ; } }
Modify a Binary String by flipping characters such that any pair of indices consisting of 1 s are neither co | C # program to implement the above approach ; Function to modify a string such that there doesn 't exist any pair  of indices consisting of 1s, whose  GCD is 1 and are divisible by each other ; Flips characters at indices 4 N , 4 N - 2 , 4 N - 4 . ... upto N terms ; Print the string ; Driver Code ; Initialize the string S ; function call
using System ; public class GFG { public static void findString ( char [ ] S , int N ) { int strLen = 4 * N ; for ( int i = 1 ; i <= N ; i ++ ) { S [ strLen - 1 ] = '1' ; strLen -= 2 ; } Console . WriteLine ( S ) ; } public static void Main ( String [ ] args ) { int N = 2 ; char [ ] S = new char [ 4 * N ] ; for ( int i = 0 ; i < 4 * N ; i ++ ) S [ i ] = '0' ; findString ( S , N ) ; } }
Check if a string can be split into two substrings with equal number of vowels | C # program for the above approach ; Function to check if any character is a vowel or not ; Lowercase vowels ; Uppercase vowels ; Otherwise ; Function to check if string S can be split into two substrings with equal number of vowels ; Stores the count of vowels in the string S ; Traverse over the string ; If S [ i ] is vowel ; Stores the count of vowels upto the current index ; Traverse over the string ; If S [ i ] is vowel ; If vowelsTillNow and totalVowels are equal ; Otherwise ; Driver Code
using System ; class GFG { public static bool isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return true ; if ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) return true ; return false ; } public static String containsEqualStrings ( string S ) { int totalVowels = 0 ; for ( int i = 0 ; i < S . Length ; i ++ ) { if ( isVowel ( S [ i ] ) ) totalVowels ++ ; } int vowelsTillNow = 0 ; for ( int i = 0 ; i < S . Length ; i ++ ) { if ( isVowel ( S [ i ] ) ) { vowelsTillNow ++ ; totalVowels -- ; if ( vowelsTillNow == totalVowels ) { return " Yes " ; } } } return " No " ; } static public void Main ( ) { string S = " geeks " ; Console . WriteLine ( containsEqualStrings ( S ) ) ; } }
Maximize matrix sum by repeatedly multiplying pairs of adjacent elements with | C # program to implement the above approach ; Function to calculate maximum sum possible of a matrix by multiplying pairs of adjacent elements with - 1 any number of times ( possibly zero ) ; Store the maximum sum of matrix possible ; Stores the count of negative values in the matrix ; Store minimum absolute value present in the matrix ; Traverse the matrix row - wise ; Update sum ; If current element is negative , increment the negative count ; If current value is less than the overall minimum in A [ ] , update the overall minimum ; If there are odd number of negative values , then the answer will be sum of the absolute values of all elements - 2 * minVal ; Print maximum sum ; Driver Code ; Given matrix ; Dimensions of matrix
using System ; class GFG { static void getMaxSum ( int [ , ] A , int M , int N ) { int sum = 0 ; int negative = 0 ; int minVal = Int32 . MaxValue ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { sum += Math . Abs ( A [ i , j ] ) ; if ( A [ i , j ] < 0 ) { negative ++ ; } minVal = Math . Min ( minVal , Math . Abs ( A [ i , j ] ) ) ; } } if ( negative % 2 != 0 ) { sum -= 2 * minVal ; } Console . Write ( sum ) ; } public static void Main ( ) { int [ , ] A = { { 4 , - 8 , 6 } , { 3 , 7 , 2 } } ; int M = A . GetLength ( 0 ) ; int N = A . GetLength ( 1 ) ; getMaxSum ( A , M , N ) ; } }
Generate a matrix with each row and column of given sum | C # program for the above approach ; Function to generate a matrix with sum of each row equal to sum of r [ ] and sum of each column equal to sum of c [ ] ; Initialize a matrix ; Traverse each cell ( i , j ) of the matrix ; Assign the minimum of the row or column value ; Subtract the minimum from both row and column sum ; Print the matrix obtained ; Driver Code
using System ; public class GFG { static int [ , ] restoreGem ( int [ ] r , int [ ] c ) { int [ , ] dp = new int [ r . Length , c . Length ] ; for ( int i = 0 ; i < r . Length ; i ++ ) { for ( int j = 0 ; j < c . Length ; j ++ ) { int m = Math . Min ( r [ i ] , c [ j ] ) ; dp [ i , j ] = m ; r [ i ] -= m ; c [ j ] -= m ; } } return dp ; } static void printMatrix ( int [ , ] ans , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { Console . Write ( ans [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int [ ] rowSum = { 5 , 7 , 10 } ; int [ ] colSum = { 8 , 6 , 8 } ; int [ , ] ans = restoreGem ( rowSum , colSum ) ; printMatrix ( ans , rowSum . Length , colSum . Length ) ; } }
Maximize sum of count of distinct prime factors of K array elements | Function to find the maximum sum of count of distinct prime factors of K array elements ; Stores the count of distinct primes ; Stores 1 and 0 at prime and non - prime indices respectively ; Initialize the count of factors to 0 ; Sieve of Eratosthenes ; Count of factors of a prime number is 1 ; Increment CountDistinct of all multiples of i ; Mark its multiples non - prime ; Stores the maximum sum of distinct prime factors of K array elements ; Stores the count of all distinct prime factors ; Traverse the array to find count of all array elements ; Maximum sum of K prime factors of array elements ; Check for the largest prime factor ; Increment sum ; Decrement its count and K ; Print the maximum sum ; Driver code ; Given array ; Size of the array ; Given value of K
using System ; public class GFG { public static int MAX = 1000000 ; static void maxSumOfDistinctPrimeFactors ( int [ ] arr , int N , int K ) { int [ ] CountDistinct = new int [ MAX + 1 ] ; bool [ ] prime = new bool [ MAX + 1 ] ; for ( int i = 0 ; i <= MAX ; i ++ ) { CountDistinct [ i ] = 0 ; prime [ i ] = true ; } for ( int i = 2 ; i <= MAX ; i ++ ) { if ( prime [ i ] == true ) { CountDistinct [ i ] = 1 ; for ( int j = i * 2 ; j <= MAX ; j += i ) { CountDistinct [ j ] ++ ; prime [ j ] = false ; } } } int sum = 0 ; int [ ] PrimeFactor = new int [ 20 ] ; for ( int i = 0 ; i < N ; i ++ ) { PrimeFactor [ CountDistinct [ arr [ i ] ] ] ++ ; } for ( int i = 19 ; i >= 1 ; i -- ) { while ( PrimeFactor [ i ] > 0 ) { sum += i ; PrimeFactor [ i ] -- ; K -- ; if ( K == 0 ) break ; } if ( K == 0 ) break ; } Console . Write ( sum ) ; } static public void Main ( ) { int [ ] arr = { 6 , 9 , 12 } ; int N = arr . Length ; int K = 2 ; maxSumOfDistinctPrimeFactors ( arr , N , K ) ; } }
Minimum replacements such that no palindromic substring of length exceeding 1 is present in the given string | C # Program to implement the above approach ; Function to count the changes required such that no palindromic subString of length exceeding 1 is present in the String ; Base Case ; Stores the count ; Iterate over the String ; Palindromic SubString of Length 2 ; Replace the next character ; Increment changes ; Palindromic SubString of Length 3 ; Replace the next character ; Increment changes ; Driver Code
using System ; public class GFG { static int maxChange ( char [ ] str ) { if ( str . Length <= 1 ) { return 0 ; } int minChanges = 0 ; for ( int i = 0 ; i < str . Length - 1 ; i ++ ) { if ( str [ i ] == str [ i + 1 ] ) { str [ i + 1 ] = ' N ' ; minChanges += 1 ; } else if ( i > 0 && str [ i - 1 ] == str [ i + 1 ] ) { str [ i + 1 ] = ' N ' ; minChanges += 1 ; } } return minChanges ; } public static void Main ( String [ ] args ) { String str = " bbbbbbb " ; Console . Write ( maxChange ( str . ToCharArray ( ) ) ) ; } }
Smallest positive integer K such that all array elements can be made equal by incrementing or decrementing by at most K | C # program for the above approach ; Function to find smallest integer K such that incrementing or decrementing each element by K at most once makes all elements equal ; Store distinct array elements ; Traverse the array , A [ ] ; Count elements into the set ; If M is greater than 3 ; If M is equal to 3 ; Stores the first smallest element ; Stores the second smallest element ; Stores the largest element ; IF difference between B_2 and B_1 is equal to B_3 and B_2 ; If M is equal to 2 ; Stores the smallest element ; Stores the largest element ; If difference is an even ; If M is equal to 1 ; Driver code ; Given array ; Given size ; Print the required smallest integer
using System ; using System . Collections . Generic ; class GFG { static void findMinKToMakeAllEqual ( int N , int [ ] A ) { var B = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { B . Add ( A [ i ] ) ; } List < int > b = new List < int > ( B ) ; int M = b . Count ; int j = 0 ; if ( M > 3 ) { Console . Write ( " - 1" ) ; } else if ( M == 3 ) { int B_1 = b [ j ++ ] ; int B_2 = b [ j ++ ] ; int B_3 = b [ j ++ ] ; if ( B_2 - B_1 == B_3 - B_2 ) { Console . Write ( B_2 - B_1 ) ; } else { Console . Write ( " - 1" ) ; } } else if ( M == 2 ) { int B_1 = b [ j ++ ] ; int B_2 = b [ j ++ ] ; if ( ( B_2 - B_1 ) % 2 == 0 ) { Console . Write ( ( B_2 - B_1 ) / 2 ) ; } else { Console . Write ( B_2 - B_1 ) ; } } else { Console . Write ( 0 ) ; } } public static void Main ( string [ ] args ) { int [ ] A = { 1 , 3 , 5 , 1 } ; int N = A . Length ; findMinKToMakeAllEqual ( N , A ) ; } }
Maximum number of times a given string needs to be concatenated to form a substring of another string | C # program for the above approach ; Function to find lps [ ] for given pattern pat [ 0. . M - 1 ] ; Length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; Iterate string to calculate lps [ i ] ; If the current character of the pattern matches ; Otherwise ; Otherwise ; Function to implement KMP algorithm ; Stores the longest prefix and suffix values for pattern ; Preprocess the pattern and find the lps [ ] array ; Index for txt [ ] ; Index for pat [ ] ; If pattern is found return 1 ; Mismatch after j matches ; Don 't match lps[0, lps[j - 1]]  characters they will  match anyway ; Return 0 if the pattern is not found ; Function to find the maximum value K for which string S2 concatenated K times is a substring of string S1 ; Store the required maximum number ; Create a temporary string to store string word ; Store the maximum number of times string S2 can occur in string S1 ; Traverse in range [ 0 , numWords - 1 ] ; If curWord is found in sequence ; Concatenate word to curWord ; Increment resCount by 1 ; Otherwise break the loop ; Print the answer ; Driver Code ; Function Call
using System ; class GFG { static void computeLPSArray ( String pat , int M , int [ ] lps ) { int len = 0 ; lps [ 0 ] = 0 ; int i = 1 ; while ( i < M ) { if ( pat [ i ] == pat [ len ] ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } else { lps [ i ] = 0 ; i ++ ; } } } } static int KMPSearch ( String pat , String txt ) { int M = pat . Length ; int N = txt . Length ; int [ ] lps = new int [ M ] ; computeLPSArray ( pat , M , lps ) ; int i = 0 ; int j = 0 ; while ( i < N ) { if ( pat [ j ] == txt [ i ] ) { j ++ ; i ++ ; } if ( j == M ) { return 1 ; else if ( i < N && [ j ] != txt [ i ] ) { if ( j != 0 ) j = lps [ j - 1 ] ; else = i + 1 ; } } return 0 ; } static void ( String seq , String word ) { int resCount = 0 ; curWord = word ; int numWords = seq . Length / word . Length ; for ( int i = 0 ; i < numWords ; ++ ) { if ( KMPSearch ( curWord , seq ) == 1 ) { curWord += word ; ++ ; } else break ; } Console . Write ( resCount ) ; } public static void ( String [ ] args ) { String S1 = " ababc " , S2 = " ab " ; ( , S2 ) ; } }
Count all possible strings that can be generated by placing spaces | C # program to implement the above approach ; Function to count the number of strings that can be generated by placing spaces between pair of adjacent characters ; Count of positions for spaces ; Count of possible strings ; Driver Code
using System ; class GFG { static long countNumberOfStrings ( String s ) { int n = s . Length - 1 ; long count = ( long ) ( Math . Pow ( 2 , n ) ) ; return count ; } public static void Main ( String [ ] args ) { string S = " ABCD " ; Console . WriteLine ( countNumberOfStrings ( S ) ) ; } }