codeparrot/xlcost-text-to-code huggingface dataset:
Check if a string is made up of K alternating characters | C # implementation of the approach ; Function to check if a String is made up of k alternating characters ; Check if all the characters at indices 0 to K - 1 are different ; If that bit is already set in checker , return false ; Otherwise update and continue by setting that bit in the checker ; Driver code
using System ; class GFG { static bool isKAlternating ( String s , int k ) { if ( s . Length < k ) return false ; int checker = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int bitAtIndex = s [ i ] - ' a ' ; if ( ( checker & ( 1 << bitAtIndex ) ) > 0 ) { return false ; } checker = checker | ( 1 << bitAtIndex ) ; } for ( int i = k ; i < s . Length ; i ++ ) if ( s [ i - k ] != s [ i ] ) return false ; return true ; } public static void Main ( ) { String str = " acdeac " ; int K = 4 ; if ( isKAlternating ( str , K ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find all Factors of Large Perfect Square Natural Number in O ( sqrt ( sqrt ( N ) ) | C # program to find the factors of large perfect square number in O ( Math . Sqrt ( Math . Sqrt ( N ) ) ) time ; Function that find all the prime factors of N ; Store the Math . Sqrt ( N ) in temp ; Initialise factor array with 1 as a factor in it ; Check divisibility by 2 ; Store the factors twice ; Check for other prime factors other than 2 ; If j is a prime factor ; Store the prime factor twice ; If j is prime number left other than 2 ; Store j twice ; Initialise Matrix M to to store all the factors ; tpc for rows tpr for column ; Initialise M [ 0 , 0 ] = 1 as it also factor of N ; Traversing factor array ; If current and previous factors are not same then move to next row and insert the current factor ; If current and previous factors are same then , Insert the factor with previous factor inserted in matrix M ; The arr1 [ ] and arr2 [ ] used to store all the factors of N ; Initialise arrays as 1 ; Traversing the matrix M ; Traversing till column element doesn 't become 0 ; Store the product of every element of current row with every element in arr1 [ ] ; Copying every element of arr2 [ ] in arr1 [ ] ; length of arr2 [ ] and arr1 [ ] are equal after copying ; Print all the factors ; Drivers Code
using System ; class GFG { static int MAX = 100000 ; static void findFactors ( int N ) { int temp = ( int ) Math . Sqrt ( N ) ; int [ ] factor = new int [ MAX ] ; for ( int l = 0 ; l < MAX ; l ++ ) factor [ l ] = 1 ; int i , j , k ; int len1 = 1 ; while ( temp % 2 == 0 ) { factor [ len1 ++ ] = 2 ; factor [ len1 ++ ] = 2 ; temp /= 2 ; } for ( j = 3 ; j < Math . Sqrt ( temp ) ; j += 2 ) { while ( temp % j == 0 ) { factor [ len1 ++ ] = j ; factor [ len1 ++ ] = j ; temp /= j ; } } if ( temp > 2 ) { factor [ len1 ++ ] = temp ; factor [ len1 ++ ] = temp ; } int [ , ] M = new int [ len1 , MAX ] ; int tpc = 0 , tpr = 0 ; M [ 0 , 0 ] = 1 ; j = 1 ; while ( j < len1 ) { if ( factor [ j ] != factor [ j - 1 ] ) { tpr ++ ; M [ tpr , 0 ] = factor [ j ] ; j ++ ; tpc = 1 ; } else { M [ tpr , tpc ] = M [ tpr , tpc - 1 ] * factor [ j ] ; j ++ ; tpc ++ ; } } int [ ] arr1 = new int [ MAX ] ; int [ ] arr2 = new int [ MAX ] ; int l1 , l2 ; l1 = l2 = 1 ; arr1 [ 0 ] = arr2 [ 0 ] = 1 ; for ( i = 1 ; i < tpr + 1 ; i ++ ) { for ( j = 0 ; M [ i , j ] != 0 ; j ++ ) { for ( k = 0 ; k < l1 ; k ++ ) { arr2 [ l2 ++ ] = arr1 [ k ] * M [ i , j ] ; } } for ( j = l1 ; j < l2 ; j ++ ) { arr1 [ j ] = arr2 [ j ] ; } l1 = l2 ; } for ( i = 0 ; i < l2 ; i ++ ) { Console . Write ( arr2 [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 900 ; findFactors ( N ) ; } }
Construct a matrix such that union of ith row and ith column contains every element from 1 to 2 N | C # implementation of the above approach ; Function to find the square matrix ; For Matrix of order 1 , it will contain only 1 ; For Matrix of odd order , it is not possible ; For Matrix of even order ; All diagonal elements of the matrix can be N itself . ; Assign values at desired place in the matrix ; Loop to add N in the lower half of the matrix such that it contains elements from 1 to 2 * N - 1 ; Loop to print the matrix ; Driver Code
using System ; class GFG { static int [ , ] matrix = new int [ 100 , 100 ] ; static void printRequiredMatrix ( int n ) { if ( n == 1 ) { Console . WriteLine ( "1" ) ; } else if ( n % 2 != 0 ) { Console . WriteLine ( " - 1" ) ; } else { for ( int i = 0 ; i < n ; i ++ ) { matrix [ i , i ] = n ; } int u = n - 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { matrix [ i , u ] = i + 1 ; for ( int j = 1 ; j < n / 2 ; j ++ ) { int a = ( i + j ) % ( n - 1 ) ; int b = ( i - j + n - 1 ) % ( n - 1 ) ; if ( a < b ) { int temp = a ; a = b ; b = temp ; } matrix [ b , a ] = i + 1 ; } } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) matrix [ i , j ] = matrix [ j , i ] + n ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) Console . Write ( matrix [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int n = 1 ; printRequiredMatrix ( n ) ; n = 3 ; printRequiredMatrix ( n ) ; n = 6 ; printRequiredMatrix ( n ) ; } }
Count of sub | C # implementation of the above approach ; Function to find the count of substrings with equal no . of consecutive 0 ' s ▁ and ▁ 1' s ; To store the total count of substrings ; Traversing the string ; Count of consecutive 0 ' s ▁ & ▁ 1' s ; Counting subarrays of type "01" ; Count the consecutive 0 's ; If consecutive 0 ' s ▁ ▁ ends ▁ then ▁ check ▁ for ▁ ▁ consecutive ▁ 1' s ; Counting consecutive 1 's ; Counting subarrays of type "10" ; Count consecutive 1 's ; If consecutive 1 ' s ▁ ▁ ends ▁ then ▁ check ▁ for ▁ ▁ consecutive ▁ 0' s ; Count consecutive 0 's ; Update the total count of substrings with minimum of ( cnt0 , cnt1 ) ; Return answer ; Driver code ; Function to print the count of substrings
using System ; class GFG { static int countSubstring ( string S , int n ) { int ans = 0 ; int i = 0 ; while ( i < n ) { int cnt0 = 0 , cnt1 = 0 ; if ( S [ i ] == '0' ) { while ( i < n && S [ i ] == '0' ) { cnt0 ++ ; i ++ ; } int j = i ; while ( j < n && S [ j ] == '1' ) { cnt1 ++ ; j ++ ; } } else { while ( i < n && S [ i ] == '1' ) { cnt1 ++ ; i ++ ; } int j = i ; while ( j < n && S [ j ] == '0' ) { cnt0 ++ ; j ++ ; } } ans += Math . Min ( cnt0 , cnt1 ) ; } return ans ; } static public void Main ( ) { string S = "0001110010" ; int n = S . Length ; Console . WriteLine ( countSubstring ( S , n ) ) ; } }
Print N numbers such that their sum is a Perfect Cube | C # implementation to find the N numbers such that their sum is a perfect cube ; Function to find the N numbers such that their sum is a perfect cube ; Loop to find the Ith term of the Centered Hexagonal number ; Driver Code ; Function Call
using System ; public class GFG { static void findNumbers ( int n ) { int i = 1 ; while ( i <= n ) { Console . Write ( ( 3 * i * ( i - 1 ) + 1 ) + " ▁ " ) ; i ++ ; } } public static void Main ( ) { int n = 4 ; findNumbers ( n ) ; } }
Largest N digit Octal number which is a Perfect square | C # implementation to find the maximum N - digit octal number which is perfect square ; Function to convert decimal number to a octal number ; Array to store octal number ; Counter for octal number array ; Store remainder in octal array ; Print octal number array in reverse order ; Largest n - digit perfect square ; Driver code
using System ; class GFG { static void decToOctal ( int n ) { int [ ] octalNum = new int [ 100 ] ; int i = 0 ; while ( n != 0 ) { octalNum [ i ] = n % 8 ; n = n / 8 ; i ++ ; } for ( int j = i - 1 ; j >= 0 ; j -- ) Console . Write ( octalNum [ j ] ) ; Console . WriteLine ( ) ; } static void nDigitPerfectSquares ( int n ) { int _decimal = ( int ) Math . Pow ( Math . Ceiling ( Math . Sqrt ( Math . Pow ( 8 , n ) ) ) - 1 , 2 ) ; decToOctal ( _decimal ) ; } public static void Main ( ) { int n = 2 ; nDigitPerfectSquares ( n ) ; } }
Reverse the substrings of the given String according to the given Array of indices | C # implementation to reverse the subStrings of the given String according to the given Array of indices ; Function to reverse a String ; Swap character starting from two corners ; Function to reverse the String with the given array of indices ; Reverse the String from 0 to A [ 0 ] ; Reverse the String for A [ i ] to A [ i + 1 ] ; Reverse String for A [ n - 1 ] to length ; Driver Code
using System ; class GFG { static String s ; static void reverseStr ( int l , int h ) { int n = h - l ; for ( int i = 0 ; i < n / 2 ; i ++ ) { s = swap ( i + l , n - i - 1 + l ) ; } } static void reverseString ( int [ ] A , int n ) { reverseStr ( 0 , A [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) reverseStr ( A [ i - 1 ] , A [ i ] ) ; reverseStr ( A [ n - 1 ] , s . Length ) ; } static String swap ( int i , int j ) { char [ ] ch = s . ToCharArray ( ) ; char temp = ch [ i ] ; ch [ i ] = ch [ j ] ; ch [ j ] = temp ; return String . Join ( " " , ch ) ; } public static void Main ( String [ ] args ) { s = " abcdefgh " ; int [ ] A = { 2 , 4 , 6 } ; int n = A . Length ; reverseString ( A , n ) ; Console . Write ( s ) ; } }
Check if the Depth of Parentheses is correct in the given String | Function to check if the Depth of Parentheses is correct in the given String ; Appending if the char is not integer ; Iterating till the entire Digit is read ; Check if character is ' ( ' ; Increment depth by 1 ; Increment open by 1 ; Check if character is ' ) ' ; Decrement depth by 1 ; Increment close by 1 ; Check if open parentheses NOT equals close parentheses ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool Formatted ( String s ) { List < char > k = new List < char > ( ) ; int i = 0 ; while ( i < s . Length ) { if ( s [ i ] == ' ) ' || s [ i ] == ' ( ' ) { k . Add ( s [ i ] ) ; i += 1 ; } else { char st = ' \x0000' ; while ( s [ i ] != ' ) ' && s [ i ] != ' ) ' ) { st = s [ i ] ; i = i + 1 ; } k . Add ( st ) ; } } int depth = 0 , flag = 1 ; int open = 0 , close = 0 ; foreach ( char i2 in k ) { if ( i2 == ' ( ' ) { depth += 1 ; open += 1 ; } else if ( i2 == ' ) ' ) { depth -= 1 ; close += 1 ; } else { if ( i2 - '0' != depth ) { flag = 0 ; break ; } } } if ( open != close ) flag = 0 ; return ( flag == 1 ) ? true : false ; } public static void Main ( String [ ] args ) { String s = " ( (2 ) ( (3 ) ) ) " ; bool k = Formatted ( s ) ; if ( k == true ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Maximize the sum of differences of consecutive elements after removing exactly K elements | C # implementation of the approach ; Function to return the maximized sum ; Remove any k internal elements ; Driver code
using System ; class GFG { static int findSum ( int [ ] arr , int n , int k ) { if ( k <= n - 2 ) return ( arr [ n - 1 ] - arr [ 0 ] ) ; return 0 ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; int k = 1 ; Console . WriteLine ( findSum ( arr , n , k ) ) ; } }
Split the binary string into substrings with equal number of 0 s and 1 s | C # implementation of the above approach ; Function to return the count of maximum substrings str can be divided into ; To store the count of 0 s and 1 s ; To store the count of maximum substrings str can be divided into ; It is not possible to split the string ; Driver code
using System ; class GFG { static int maxSubStr ( String str , int n ) { int count0 = 0 , count1 = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) { count0 ++ ; } else { count1 ++ ; } if ( count0 == count1 ) { cnt ++ ; } } if ( cnt == 0 ) { return - 1 ; } return cnt ; } public static void Main ( String [ ] args ) { String str = "0100110101" ; int n = str . Length ; Console . WriteLine ( maxSubStr ( str , n ) ) ; } }
Sum of the digits of square of the given number which has only 1 's as its digits | C # implementation of the approach ; Function to return the sum of the digits of num ^ 2 ; To store the number of 1 's ; Find the sum of the digits of num ^ 2 ; Driver code
using System ; class GFG { static long squareDigitSum ( String s ) { long lengthN = s . Length ; long result = ( lengthN / 9 ) * 81 + ( long ) Math . Pow ( ( lengthN % 9 ) , 2 ) ; return result ; } public static void Main ( String [ ] args ) { String s = "1111" ; Console . WriteLine ( squareDigitSum ( s ) ) ; } }
Check whether two strings contain same characters in same order | C # implementation of the approach ; String class has a constructor that allows us to specify size of String as first parameter and character to be filled in given size as second parameter . ; Function that returns true if the given Strings contain same characters in same order ; Get the first character of both Strings ; Now if there are adjacent similar character remove that character from s1 ; Now if there are adjacent similar character remove that character from s2 ; If both the strings are equal then return true ; Driver code
using System ; public class temp { static String getString ( char x ) { String s = String . Join ( " " , x ) ; return s ; } static Boolean solve ( String s1 , String s2 ) { String a = getString ( s1 [ 0 ] ) , b = getString ( s2 [ 0 ] ) ; for ( int i = 1 ; i < s1 . Length ; i ++ ) if ( s1 [ i ] != s1 [ i - 1 ] ) { a += getString ( s1 [ i ] ) ; } for ( int i = 1 ; i < s2 . Length ; i ++ ) if ( s2 [ i ] != s2 [ i - 1 ] ) { b += getString ( s2 [ i ] ) ; } if ( a == b ) return true ; return false ; } public static void Main ( String [ ] args ) { String s1 = " Geeks " , s2 = " Geks " ; if ( solve ( s1 , s2 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Count number of distinct substrings of a given length | C # implementation of the approach ; Function to find the required count ; Variable to the hash ; Finding hash of substring ( 0 , l - 1 ) using random number x ; Computing x ^ ( l - 1 ) ; Unordered set to add hash values ; Generating all possible hash values ; Print the result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int x = 26 ; static int mod = 3001 ; static void CntSubstr ( char [ ] s , int l ) { int hash = 0 ; for ( int i = 0 ; i < l ; i ++ ) { hash = ( hash * x + ( s [ i ] - 97 ) ) % mod ; } int pow_l = 1 ; for ( int i = 0 ; i < l - 1 ; i ++ ) { pow_l = ( pow_l * x ) % mod ; } HashSet < int > result = new HashSet < int > ( ) ; result . Add ( hash ) ; for ( int i = l ; i < s . Length ; i ++ ) { hash = ( ( hash - pow_l * ( s [ i - l ] - 97 ) + 2 * mod ) * x + ( s [ i ] - 97 ) ) % mod ; result . Add ( hash ) ; } Console . WriteLine ( result . Count ) ; } public static void Main ( String [ ] args ) { String s = " abcba " ; int l = 2 ; CntSubstr ( s . ToCharArray ( ) , l ) ; } }
Check if a binary string has two consecutive occurrences of one everywhere | C # implementation of the approach ; Function that returns 1 if str is valid ; Index of first appearance of ' b ' ; If str starts with ' b ' ; While ' b ' occurs in str ; If ' b ' doesn ' t ▁ appear ▁ after ▁ an ▁ ' a ' ; If ' b ' is not succeeded by another ' b ' ; If sub - string is of the type " abbb " ; If str ends with a single b ; Driver code
using System ; class GFG { private static bool isValidString ( string str , int n ) { int index = str . IndexOf ( " b " ) ; if ( index == 0 ) return false ; while ( index != - 1 ) { if ( str [ index - 1 ] != ' a ' ) return false ; if ( index + 1 < n && str [ index + 1 ] != ' b ' ) return false ; if ( index + 2 < n && str [ index + 2 ] == ' b ' ) return false ; if ( index == n - 1 ) return false ; index = str . IndexOf ( " b " , index + 2 ) ; } return true ; } public static void Main ( ) { string str = " abbaaabbabba " ; int n = str . Length ; Console . WriteLine ( isValidString ( str , n ) ) ; } }
Count strings that end with the given pattern | C # implementation of the approach ; Function that return true if str ends with pat ; Pattern is larger in length than the string ; We match starting from the end while patLen is greater than or equal to 0. ; If at any index str doesn 't match  with pattern ; If str ends with the given pattern ; Function to return the count of required strings ; If current string ends with the given pattern ; Driver code
using System ; class GFG { static bool endsWith ( string str , string pat ) { int patLen = pat . Length ; int strLen = str . Length ; if ( patLen > strLen ) return false ; patLen -- ; strLen -- ; while ( patLen >= 0 ) { if ( pat [ patLen ] != str [ strLen ] ) return false ; patLen -- ; strLen -- ; } return true ; } static int countOfStrings ( string pat , int n , string [ ] sArr ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( endsWith ( sArr [ i ] , pat ) ) count ++ ; return count ; } public static void Main ( ) { string pat = " ks " ; int n = 4 ; string [ ] sArr = { " geeks " , " geeksforgeeks " , " games " , " unit " } ; Console . WriteLine ( countOfStrings ( pat , n , sArr ) ) ; } }
Length of the longest substring with consecutive characters | C # implementation of the approach ; Function to return the ending index for the largest valid sub - string starting from index i ; If the current character appears after the previous character according to the given circular alphabetical order ; Function to return the length of the longest sub - string of consecutive characters from str ; Valid sub - string exists from index i to end ; Update the length ; Driver code
using System ; class GFG { static int getEndingIndex ( string str , int n , int i ) { i ++ ; while ( i < n ) { char curr = str [ i ] ; char prev = str [ i - 1 ] ; if ( ( curr == ' a ' && prev == ' z ' ) || ( curr - prev == 1 ) ) i ++ ; else break ; } return i - 1 ; } static int largestSubStr ( string str , int n ) { int len = 0 ; int i = 0 ; while ( i < n ) { int end = getEndingIndex ( str , n , i ) ; len = Math . Max ( end - i + 1 , len ) ; i = end + 1 ; } return len ; } public static void Main ( ) { string str = " abcabcdefabc " ; int n = str . Length ; Console . Write ( largestSubStr ( str , n ) ) ; } }
Sum of integers upto N with given unit digit ( Set 2 ) | C # implementation of the approach ; Function to return the required sum ; Decrement N ; Driver code
class GFG { static int getSum ( int n , int d ) { if ( n < d ) return 0 ; while ( n % 10 != d ) n -- ; int k = n / 10 ; return ( k + 1 ) * d + ( k * 10 + 10 * k * k ) / 2 ; } public static void Main ( ) { int n = 30 ; int d = 3 ; System . Console . WriteLine ( getSum ( n , d ) ) ; } }
Maximum possible time that can be formed from four digits | C # implementation of the approach ; Function to return the updated frequency map for the array passed as argument ; Function that returns true if the passed digit is present in the map after decrementing it 's frequency by 1 ; If map contains the digit ; Decrement the frequency of the digit by 1 ; True here indicates that the digit was found in the map ; Digit not found ; Function to return the maximum possible time in 24 - Hours format ; First digit of hours can be from the range [ 0 , 2 ] ; If no valid digit found ; If first digit of hours was chosen as 2 then the second digit of hours can be from the range [ 0 , 3 ] ; Else it can be from the range [ 0 , 9 ] ; Hours and minutes separator ; First digit of minutes can be from the range [ 0 , 5 ] ; Second digit of minutes can be from the range [ 0 , 9 ] ; Return the maximum possible time ; Driver code
using System ; using System . Collections . Generic ; class GFG { static Dictionary < int , int > getFrequencyMap ( int [ ] arr ) { Dictionary < int , int > hashMap = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( hashMap . ContainsKey ( arr [ i ] ) ) { hashMap [ arr [ i ] ] = hashMap [ arr [ i ] ] + 1 ; } else { hashMap . Add ( arr [ i ] , 1 ) ; } } return hashMap ; } static bool hasDigit ( Dictionary < int , int > hashMap , int digit ) { if ( hashMap . ContainsKey ( digit ) && hashMap [ digit ] > 0 ) { hashMap [ digit ] = hashMap [ digit ] - 1 ; return true ; } return false ; } static String getMaxTime ( int [ ] arr ) { Dictionary < int , int > hashMap = getFrequencyMap ( arr ) ; int i ; bool flag ; String time = " " ; flag = false ; for ( i = 2 ; i >= 0 ; i -- ) { if ( hasDigit ( hashMap , i ) ) { flag = true ; time += i ; break ; } } if ( ! flag ) { return " - 1" ; } flag = false ; if ( time [ 0 ] == '2' ) { for ( i = 3 ; i >= 0 ; i -- ) { if ( hasDigit ( hashMap , i ) ) { flag = true ; time += i ; break ; } } } else { for ( i = 9 ; i >= 0 ; i -- ) { if ( hasDigit ( hashMap , i ) ) { flag = true ; time += i ; break ; } } } if ( ! flag ) { return " - 1" ; } time += " : " ; flag = false ; for ( i = 5 ; i >= 0 ; i -- ) { if ( hasDigit ( hashMap , i ) ) { flag = true ; time += i ; break ; } } if ( ! flag ) { return " - 1" ; } flag = false ; for ( i = 9 ; i >= 0 ; i -- ) { if ( hasDigit ( hashMap , i ) ) { flag = true ; time += i ; break ; } } if ( ! flag ) { return " - 1" ; } return time ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 0 , 0 , 9 } ; Console . WriteLine ( getMaxTime ( arr ) ) ; } }
Most frequent word in first String which is not present in second String | C # implementation of above approach ; Function to return frequent word from S1 that isn 't  present in S2 ; create map of banned words ; find smallest and most frequent word ; check if word is not banned ; return answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static String smallestFreq ( String S1 , String S2 ) { Dictionary < String , int > banned = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < S2 . Length ; i ++ ) { String s = " " ; while ( i < S2 . Length && S2 [ i ] != ' ▁ ' ) s += S2 [ i ++ ] ; if ( banned . ContainsKey ( s ) ) { var val = banned [ s ] ; banned . Remove ( s ) ; banned . Add ( s , val + 1 ) ; } else { banned . Add ( s , 1 ) ; } } Dictionary < String , int > result = new Dictionary < String , int > ( ) ; String ans = " " ; int freq = 0 ; for ( int i = 0 ; i < S1 . Length ; i ++ ) { String s = " " ; while ( i < S1 . Length && S1 [ i ] != ' ▁ ' ) s += S1 [ i ++ ] ; if ( ! banned . ContainsKey ( s ) ) { if ( result . ContainsKey ( s ) ) { var val = result [ s ] ; result . Remove ( s ) ; result . Add ( s , val + 1 ) ; } else { result . Add ( s , 1 ) ; } if ( result [ s ] > freq || ( result [ s ] == freq && s . CompareTo ( ans ) < 0 ) ) { ans = s ; freq = result [ s ] ; } } } return ans ; } public static void Main ( String [ ] args ) { String S1 = " geeks ▁ for ▁ geeks ▁ is ▁ best ▁ place ▁ to ▁ learn " ; String S2 = " bad ▁ place " ; Console . WriteLine ( smallestFreq ( S1 , S2 ) ) ; } }
Minimum characters to be replaced to remove the given substring | C # implementation of above approach ; function to calculate minimum characters to replace ; mismatch occurs ; if all characters matched , i . e , there is a substring of ' a ' which is same as string ' b ' ; increment i to index m - 1 such that minimum characters are replaced in ' a ' ; Driver Code
using System ; class GFG { public static int replace ( string A , string B ) { int n = A . Length , m = B . Length ; int count = 0 , i , j ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( i + j >= n ) { break ; } else if ( A [ i + j ] != B [ j ] ) { break ; } } if ( j == m ) { count ++ ; i += m - 1 ; } } return count ; } public static void Main ( string [ ] args ) { string str1 = " aaaaaaaa " ; string str2 = " aaa " ; Console . WriteLine ( replace ( str1 , str2 ) ) ; } }
Check if a string is substring of another | C # program to check if a string is substring of other . ; Returns true if s1 is substring of s2 ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; Driver code
using System ; class GFG { static int isSubstring ( string s1 , string s2 ) { int M = s1 . Length ; int N = s2 . Length ; for ( int i = 0 ; i <= N - M ; i ++ ) { int j ; for ( j = 0 ; j < M ; j ++ ) if ( s2 [ i + j ] != s1 [ j ] ) break ; if ( j == M ) return i ; } return - 1 ; } public static void Main ( ) { string s1 = " for " ; string s2 = " geeksforgeeks " ; int res = isSubstring ( s1 , s2 ) ; if ( res == - 1 ) Console . Write ( " Not ▁ present " ) ; else Console . Write ( " Present ▁ at ▁ index ▁ " + res ) ; } }