codeparrot/xlcost-text-to-code huggingface dataset:
Remove odd frequency characters from the string | C # program to remove the characters having odd frequencies in the string ; Function to remove the characters which have odd frequencies in the string ; Create a map to store the frequency of each character ; To store the new string ; Remove the characters which have odd frequencies ; If the character has odd frequency then skip ; Else concatenate the character to the new string ; Return the modified string ; Driver code ; Remove the characters which have odd frequencies
using System ; using System . Collections . Generic ; class GFG { static string removeOddFrequencyCharacters ( string s ) { Dictionary < char , int > m = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { char p = s [ i ] ; if ( m . ContainsKey ( p ) ) { m [ p ] ++ ; } else { m [ p ] = 1 ; } } string new_string = " " ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ( m [ s [ i ] ] & 1 ) == 1 ) continue ; new_string += s [ i ] ; } return new_string ; } public static void Main ( string [ ] args ) { string str = " geeksforgeeks " ; str = removeOddFrequencyCharacters ( str ) ; Console . Write ( str ) ; } }
Product of nodes at k | C # implementation to find product of elements at k - th level ; Recursive Function to find product of elements at k - th level ; if subtree is null , just like if root == null ; Consider only level k node to be part of the product ; Recur for Left Subtree ; Recur for Right Subtree ; Taking care of ' ) ' after left and right subtree ; Driver Code
using System ; class GFG { static int i ; static int productAtKthLevel ( String tree , int k , int level ) { if ( tree [ i ++ ] == ' ( ' ) { if ( tree [ i ] == ' ) ' ) return 1 ; int product = 1 ; if ( level == k ) product = tree [ i ] - '0' ; ++ i ; int leftproduct = productAtKthLevel ( tree , k , level + 1 ) ; ++ i ; int rightproduct = productAtKthLevel ( tree , k , level + 1 ) ; ++ i ; return product * leftproduct * rightproduct ; } return int . MinValue ; } public static void Main ( String [ ] args ) { String tree = " ( 0(5(6 ( ) ( ) ) ( 4 ( ) " + " ( 9 ( ) ( ) ) ) ) ( 7(1 ( ) ( ) ) ( 3 ( ) ( ) ) ) ) " ; int k = 2 ; i = 0 ; Console . Write ( productAtKthLevel ( tree , k , 0 ) ) ; } }
Print the most occurring character in an array of strings | C # program to print the most occurring character in an array of Strings ; Function to print the most occurring character ; Creating a hash of size 26 ; For loop to iterate through every String of the array ; For loop to iterate through every character of the String ; Incrementing the count of the character in the hash ; Finding the character with the maximum count ; Driver code ; Declaring Vector of String type
using System ; class GFG { static void findMostOccurringChar ( string [ ] str ) { int [ ] hash = new int [ 26 ] ; for ( int i = 0 ; i < str . Length ; i ++ ) { for ( int j = 0 ; j < str [ i ] . Length ; j ++ ) { hash [ str [ i ] [ j ] - 97 ] ++ ; } } int max = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { max = hash [ i ] > hash [ max ] ? i : max ; } Console . Write ( ( char ) ( max + 97 ) + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { string [ ] str = { " animal " , " zebra " , " lion " , " giraffe " } ; findMostOccurringChar ( str ) ; } }
Check whether the given floating point number is a palindrome | C # implementation of the approach ; Function that returns true if num is palindrome ; Convert the given floating point number into a string ; Pointers pointing to the first and the last character of the string ; Not a palindrome ; Update the pointers ; Driver code
using System ; class GFG { public static bool isPalindrome ( float num ) { string s = num . ToString ( ) ; int low = 0 ; int high = s . Length - 1 ; while ( low < high ) { if ( s [ low ] != s [ high ] ) return false ; low ++ ; high -- ; } return true ; } public static void Main ( ) { float n = 123.321f ; if ( isPalindrome ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Maximum number of times str1 appears as a non | C # implementation of the above approach ; Function to return the maximum number of times str1 can appear as a non - overlapping substring in str2 ; str1 cannot never be substring of str2 ; Store the frequency of the characters of str1 ; Store the frequency of the characters of str2 ; To store the required count of substrings ; Current character doesn 't appear in str1 ; Frequency of the current character in str1 is greater than its frequency in str2 ; Update the count of possible substrings ; Driver code
using System ; class GFG { readonly static int MAX = 26 ; static int maxSubStr ( char [ ] str1 , int len1 , char [ ] str2 , int len2 ) { if ( len1 > len2 ) return 0 ; int [ ] freq1 = new int [ MAX ] ; for ( int i = 0 ; i < len1 ; i ++ ) freq1 [ i ] = 0 ; for ( int i = 0 ; i < len1 ; i ++ ) freq1 [ str1 [ i ] - ' a ' ] ++ ; int [ ] freq2 = new int [ MAX ] ; for ( int i = 0 ; i < len2 ; i ++ ) freq2 [ i ] = 0 ; for ( int i = 0 ; i < len2 ; i ++ ) freq2 [ str2 [ i ] - ' a ' ] ++ ; int minPoss = int . MaxValue ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq1 [ i ] == 0 ) continue ; if ( freq1 [ i ] > freq2 [ i ] ) return 0 ; minPoss = Math . Min ( minPoss , freq2 [ i ] / freq1 [ i ] ) ; } return minPoss ; } public static void Main ( String [ ] args ) { String str1 = " geeks " , str2 = " gskefrgoekees " ; int len1 = str1 . Length ; int len2 = str2 . Length ; Console . WriteLine ( maxSubStr ( str1 . ToCharArray ( ) , len1 , str2 . ToCharArray ( ) , len2 ) ) ; } }
Number of ways to insert two pairs of parentheses into a string of N characters | C # implementation of the approach ; Function to return the number of ways to insert the bracket pairs ; Driver code
using System ; class GFG { static int cntWays ( String str , int n ) { int x = n + 1 ; int ways = x * x * ( x * x - 1 ) / 12 ; return ways ; } public static void Main ( String [ ] args ) { String str = " ab " ; int n = str . Length ; Console . WriteLine ( cntWays ( str , n ) ) ; } }
Minimum number of substrings the given string can be splitted into that satisfy the given conditions | C # implementation of above approach ; Set to store all the Strings from the given array ; To store the required count ; Recursive function to find the count of subStrings that can be splitted starting from the index start such that all the subStrings are present in the map ; All the chosen subStrings are present in the map ; Update the minimum count of subStrings ; Starting from the subStrings of length 1 that start with the given index ; Get the subString ; If the subString is present in the set ; Recursive call for the rest of the String ; Function that inserts all the Strings from the given array in a set and calls the recursive function to find the minimum count of subStrings str can be splitted into that satisfy the given condition ; Insert all the Strings from the given array in a set ; Find the required count ; Driver code
using System ; using System . Collections . Generic ; class GFG { static HashSet < String > uSet = new HashSet < String > ( ) ; static int minCnt = int . MaxValue ; static void findSubStr ( String str , int cnt , int start ) { if ( start == str . Length ) { minCnt = Math . Min ( cnt , minCnt ) ; } for ( int len = 1 ; len <= ( str . Length - start ) ; len ++ ) { String subStr = str . Substring ( start , len ) ; if ( uSet . Contains ( subStr ) ) { findSubStr ( str , cnt + 1 , start + len ) ; } } } static void findMinSubStr ( String [ ] arr , int n , String str ) { for ( int i = 0 ; i < n ; i ++ ) uSet . Add ( arr [ i ] ) ; findSubStr ( str , 0 , 0 ) ; } public static void Main ( String [ ] args ) { String str = "123456" ; String [ ] arr = { "1" , "12345" , "2345" , "56" , "23" , "456" } ; int n = arr . Length ; findMinSubStr ( arr , n , str ) ; Console . WriteLine ( minCnt ) ; } }
Number of substrings that start with " geeks " and end with " for " | C # implementation of the approach ; Function to return the count of required substrings ; For every index of the string ; If the substring starting at the current index is " geeks " ; If the substring is " for " ; Driver code
using System ; public class GFG { static int countSubStr ( String s , int n ) { int c1 = 0 , c2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n - 5 && " geeks " . Equals ( s . Substring ( i , 5 ) ) ) { c1 ++ ; } if ( i < n - 3 && " for " . Equals ( s . Substring ( i , 3 ) ) ) { c2 = c2 + c1 ; } } return c2 ; } public static void Main ( String [ ] args ) { String s = " geeksforgeeksisforgeeks " ; int n = s . Length ; Console . WriteLine ( countSubStr ( s , n ) ) ; } }
InfyTQ 2019 : Find the position from where the parenthesis is not balanced | C # implementation of the approach ; Defining the string ; Storing opening braces in list lst1 ; Storing closing braces in list lst2 ; Creating an empty list lst ; Creating dictionary to map closing braces to opening ones ; If first position of string contain any closing braces return 1 ; If characters of string are opening braces then append them in a list ; When size of list is 0 and new closing braces is encountered then print its index starting from 1 ; As we encounter closing braces we map them with theircorresponding opening braces using dictionary and check if it is same as last opened braces ( last element in list ) if yes then we delete that elememt from list ; Otherwise we return the index ( starting from 1 ) at which nesting is found wrong ; At end if the list is empty it means the string is perfectly nested
using System ; using System . Collections . Generic ; class GFG { static void Main ( ) { string String = " { [ ( ) ] } [ ] " ; char [ ] lst1 = { ' { ' , ' ( ' , ' [ ' } ; char [ ] lst2 = { ' } ' , ' ) ' , ' ] ' } ; List < char > lst = new List < char > ( ) ; Dictionary < char , char > Dict = new Dictionary < char , char > ( ) ; Dict [ ' ) ' ] = ' ( ' ; Dict [ ' } ' ] = ' { ' ; Dict [ ' ] ' ] = ' [ ' ; int a = 0 , b = 0 , c = 0 ; if ( Array . Exists ( lst2 , element => element == String [ 0 ] ) ) { Console . WriteLine ( 1 ) ; } else { int k = 0 ; for ( int i = 0 ; i < String . Length ; i ++ ) { if ( Array . Exists ( lst1 , element => element == String [ i ] ) ) { lst . Add ( String [ i ] ) ; k = i + 2 ; } else { if ( lst . Count == 0 && Array . Exists ( lst2 , element => element == String [ i ] ) ) { Console . WriteLine ( ( i + 1 ) ) ; c = 1 ; break ; } else { if ( lst . Count > 0 && Dict [ String [ i ] ] == lst [ lst . Count - 1 ] ) { lst . RemoveAt ( lst . Count - 1 ) ; } else { a = 1 ; break ; } } } } if ( lst . Count == 0 && c == 0 ) { Console . WriteLine ( 0 ) ; b = 1 ; } if ( a == 0 && b == 0 && c == 0 ) { Console . WriteLine ( k ) ; } } } }
Encrypt the given string with the following operations | C # implementation of the above approach : ; Function to return the encrypted string ; Reduce x because rotation of length 26 is unnecessary ; calculate the frequency of characters ; If the frequency of current character is even then increment it by x ; Else decrement it by x ; Return the count ; Driver code
using System ; class GFG { static int MAX = 26 ; public static char [ ] encryptStr ( String str , int n , int x ) { x = x % MAX ; char [ ] arr = str . ToCharArray ( ) ; int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq [ arr [ i ] - ' a ' ] % 2 == 0 ) { int pos = ( arr [ i ] - ' a ' + x ) % MAX ; arr [ i ] = ( char ) ( pos + ' a ' ) ; } else { int pos = ( arr [ i ] - ' a ' - x ) ; if ( pos < 0 ) pos += MAX ; arr [ i ] = ( char ) ( pos + ' a ' ) ; } } return arr ; } public static void Main ( String [ ] args ) { String s = " abcda " ; int n = s . Length ; int x = 3 ; Console . WriteLine ( encryptStr ( s , n , x ) ) ; } }
Rearrange characters in a string such that no two adjacent are same using hashing | C # implementation of the approach ; Function that returns true if it is possible to rearrange the characters of the string such that no two consecutive characters are same ; To store the frequency of each of the character ; To store the maximum frequency so far ; If possible ; Driver code
using System ; using System . Collections . Generic ; class GFG { static Boolean isPossible ( char [ ] str ) { Dictionary < char , int > freq = new Dictionary < char , int > ( ) ; int max_freq = 0 ; for ( int j = 0 ; j < ( str . Length ) ; j ++ ) { if ( freq . ContainsKey ( str [ j ] ) ) { var v = freq [ str [ j ] ] + 1 ; freq . Remove ( str [ j ] ) ; freq . Add ( str [ j ] , v ) ; if ( freq [ str [ j ] ] > max_freq ) max_freq = freq [ str [ j ] ] ; } else { freq . Add ( str [ j ] , 1 ) ; if ( freq [ str [ j ] ] > max_freq ) max_freq = freq [ str [ j ] ] ; } } if ( max_freq <= ( str . Length - max_freq + 1 ) ) return true ; return false ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; if ( isPossible ( str . ToCharArray ( ) ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find uncommon characters of the two strings | Set 2 | C # implementation of the approach ; Function to print the uncommon characters in the given string in sorted order ; Converting character to ASCII code ; Bit operation ; Converting character to ASCII code ; Bit operation ; XOR operation leaves only uncommon characters in the ans variable ; Driver code
using System ; class GFG { static void printUncommon ( string str1 , string str2 ) { int a1 = 0 , a2 = 0 ; for ( int i = 0 ; i < str1 . Length ; i ++ ) { int ch = ( str1 [ i ] - ' a ' ) ; a1 = a1 | ( 1 << ch ) ; } for ( int i = 0 ; i < str2 . Length ; i ++ ) { int ch = ( str2 [ i ] - ' a ' ) ; a2 = a2 | ( 1 << ch ) ; } int ans = a1 ^ a2 ; int j = 0 ; while ( j < 26 ) { if ( ans % 2 == 1 ) { Console . Write ( ( char ) ( ' a ' + j ) ) ; } ans = ans / 2 ; j ++ ; } } public static void Main ( ) { string str1 = " geeksforgeeks " ; string str2 = " geeksquiz " ; printUncommon ( str1 , str2 ) ; } }
Minimum number of bracket reversals needed to make an expression balanced | Set | C # program to find minimum number of reversals required to balance an expression ; Returns count of minimum reversals for making expr balanced . Returns - 1 if expr cannot be balanced . ; length of expression must be even to make it balanced by using reversals . ; To store number of reversals required . ; To store number of unbalanced opening brackets . ; To store number of unbalanced closing brackets . ; If current bracket is open then increment open count . ; If current bracket is close , check if it balances opening bracket . If yes then decrement count of unbalanced opening bracket else increment count of closing bracket . ; For the case : " } { " or when one closing and one opening bracket remains for pairing , then both need to be reversed . ; Driver Code
using System ; class GFG { static int countMinReversals ( String expr ) { int len = expr . Length ; if ( len % 2 != 0 ) return - 1 ; int ans = 0 ; int i ; int open = 0 ; int close = 0 ; for ( i = 0 ; i < len ; i ++ ) { if ( expr [ i ] == ' { ' ) open ++ ; else { if ( open == 0 ) close ++ ; else open -- ; } } ans = ( close / 2 ) + ( open / 2 ) ; close %= 2 ; open %= 2 ; if ( close != 0 ) ans += 2 ; return ans ; } public static void Main ( String [ ] args ) { String expr = " } } { { " ; Console . WriteLine ( countMinReversals ( expr ) ) ; } }
Character pairs from two strings with even sum | C # implementation of the approach ; Function to return the total number of valid pairs ; Count total number of even and odd ascii values for string s1 ; Count total number of even and odd ascii values for string s2 ; Return total valid pairs ; Driver code
using System ; class GfG { static int totalPairs ( String s1 , String s2 ) { int a1 = 0 , b1 = 0 ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { if ( ( int ) s1 [ i ] % 2 != 0 ) a1 ++ ; else b1 ++ ; } int a2 = 0 , b2 = 0 ; for ( int i = 0 ; i < s2 . Length ; i ++ ) { if ( ( int ) s2 [ i ] % 2 != 0 ) a2 ++ ; else b2 ++ ; } return ( ( a1 * a2 ) + ( b1 * b2 ) ) ; } public static void Main ( String [ ] args ) { String s1 = " geeks " , s2 = " for " ; Console . WriteLine ( totalPairs ( s1 , s2 ) ) ; } }
Maximum occurrence of prefix in the Array | C # program to find the number of occurrences of prefix which occurs maximum no . of time ; Function to return the count of the required prefix ; Find the frequency of first character of string ; Driver code
using System ; class GFG { static int prefixOccurrences ( string str ) { char c = str [ 0 ] ; int countc = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == c ) countc ++ ; } return countc ; } public static void Main ( ) { string str = " abbcdabbcd " ; Console . WriteLine ( prefixOccurrences ( str ) ) ; } }
Minimum number of given operations required to convert a string to another string | C # implementation of the approach ; Function to return the minimum operations of the given type required to convert string s to string t ; Characters are already equal ; Increment count of 0 s ; Increment count of 1 s ; Driver code
using System ; class GFG { static int minOperations ( string s , string t , int n ) { int ct0 = 0 , ct1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == t [ i ] ) continue ; if ( s [ i ] == '0' ) ct0 ++ ; else ct1 ++ ; } return Math . Max ( ct0 , ct1 ) ; } public static void Main ( ) { string s = "010" , t = "101" ; int n = s . Length ; Console . Write ( minOperations ( s , t , n ) ) ; } }
Decrypt a string encrypted by repeating i | C # implementation of the approach ; Function to return the decrypted string ; Initial jump will be 1 ; Increment jump by 1 with every character ; Driver code
using System ; class GFG { static string decryptString ( string str , int n ) { int i = 0 , jump = 1 ; string decryptedStr = " " ; while ( i < n ) { decryptedStr += str [ i ] ; i += jump ; jump ++ ; } return decryptedStr ; } public static void Main ( ) { string str = " geeeeekkkksssss " ; int n = str . Length ; Console . Write ( decryptString ( str , n ) ) ; } }
Find Bit whose minimum sequence flips makes all bits same | C # program to find which bit sequence to be flipped ; Function to check which bit is to be flipped ; variable to store first and last character of string ; Check if first and last characters are equal , if yes , then return the character which is not at last ; else return last ; Driver Code
using System ; class GfG { static char bitToBeFlipped ( String s ) { char last = s [ s . Length - 1 ] ; char first = s [ 0 ] ; if ( last == first ) { if ( last == '0' ) { return '1' ; } else { return '0' ; } } else if ( last != first ) { return last ; } return last ; } public static void Main ( ) { string s = "1101011000" ; Console . WriteLine ( bitToBeFlipped ( s ) ) ; } }
Sum and Product of Prime Frequencies of Characters in a String | C # program to find Sum and product of Prime Frequencies of Characters in a String ; Function to create Sieve to check primes ; false here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to find the sum of prime frequencies of the characters of the given string ; map is used to store character frequencies ; Traverse the map ; If the frequency is prime ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void SieveOfEratosthenes ( bool [ ] prime , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i < p_size ; i += p ) { prime [ i ] = false ; } } } } static void sumProdOfPrimeFreq ( char [ ] s ) { int i ; bool [ ] prime = new bool [ s . Length + 1 ] ; for ( i = 0 ; i < s . Length + 1 ; i ++ ) { prime [ i ] = true ; } SieveOfEratosthenes ( prime , s . Length + 1 ) ; Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; for ( i = 0 ; i < s . Length ; i ++ ) { if ( mp . ContainsKey ( s [ i ] ) ) { var val = mp [ s [ i ] ] ; mp . Remove ( s [ i ] ) ; mp . Add ( s [ i ] , val + 1 ) ; } else { mp . Add ( s [ i ] , 1 ) ; } } int sum = 0 , product = 1 ; foreach ( KeyValuePair < char , int > it in mp ) { if ( prime [ it . Value ] ) { sum += it . Value ; product *= it . Value ; } } Console . Write ( " Sum ▁ = ▁ " + sum ) ; Console . WriteLine ( " Product = " } public static void Main ( String [ ] args ) { String s = " geeksforgeeks " ; sumProdOfPrimeFreq ( s . ToCharArray ( ) ) ; } }
Check if frequency of character in one string is a factor or multiple of frequency of same character in other string | C # implementation of the approach ; Function that checks if the frequency of character are a factor or multiple of each other ; map store frequency of each character ; if any frequency is 0 , then continue as condition is satisfied ; if factor or multiple , then condition satisfied ; if condition not satisfied ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static Boolean multipleOrFactor ( String s1 , String s2 ) { Dictionary < char , int > m1 = new Dictionary < char , int > ( ) ; Dictionary < char , int > m2 = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { if ( m1 . ContainsKey ( s1 [ i ] ) ) { var x = m1 [ s1 [ i ] ] ; m1 [ s1 [ i ] ] = ++ x ; } else m1 . Add ( s1 [ i ] , 1 ) ; } for ( int i = 0 ; i < s2 . Length ; i ++ ) { if ( m2 . ContainsKey ( s2 [ i ] ) ) { var x = m2 [ s2 [ i ] ] ; m2 [ s2 [ i ] ] = ++ x ; } else m2 . Add ( s2 [ i ] , 1 ) ; } foreach ( KeyValuePair < char , int > entry in m1 ) { if ( ! m2 . ContainsKey ( entry . Key ) ) continue ; if ( m2 [ entry . Key ] != 0 && ( m2 [ entry . Key ] % entry . Value == 0 entry . Value % m2 [ entry . Key ] == 0 ) ) continue ; else return false ; } return true ; } public static void Main ( String [ ] args ) { String s1 = " geeksforgeeks " , s2 = " geeks " ; if ( multipleOrFactor ( s1 , s2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Remove even frequency characters from the string | C # implementation of the approach ; Function that removes the characters which have even frequencies in the string ; create a map to store the frequency of each character ; to store the new string ; remove the characters which have even frequencies ; if the character has even frequency then skip ; else concatenate the character to the new string ; display the modified string ; Driver code ; remove the characters which have even frequencies
using System ; using System . Collections . Generic ; class GFG { static void solve ( String s ) { Dictionary < char , int > m = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( m . ContainsKey ( s [ i ] ) ) { var val = m [ s [ i ] ] ; m . Remove ( s [ i ] ) ; m . Add ( s [ i ] , val + 1 ) ; } else m . Add ( s [ i ] , 1 ) ; } String new_string = " " ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( m [ s [ i ] ] % 2 == 0 ) continue ; new_string = new_string + s [ i ] ; } Console . WriteLine ( new_string ) ; } public static void Main ( String [ ] args ) { String s = " aabbbddeeecc " ; solve ( s ) ; } }
Remove all the palindromic words from the given sentence | C # implementation to remove all the palindromic words from the given sentence ; function to check if ' str ' is palindrome ; traversing from both the ends ; not palindrome ; palindrome ; function to remove all the palindromic words from the given sentence ; ' final _ str ' to store the final string and ' word ' to one by one store each word of ' str ' ; add space at the end of ' str ' ; traversing ' str ' ; accumulating characters of the current word ; if ' word ' is not palindrome then a add it to ' final _ str ' ; reset ; required final string ; Driver code
using System ; class GFG { static bool isPalindrome ( string str ) { int i = 0 , j = str . Length - 1 ; while ( i < j ) { if ( str [ i ++ ] != str [ j -- ] ) return false ; } return true ; } static String removePalinWords ( string str ) { string final_str = " " , word = " " ; str = str + " ▁ " ; int n = str . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] != ' ▁ ' ) word = word + str [ i ] ; else { if ( ! ( isPalindrome ( word ) ) ) final_str += word + " ▁ " ; word = " " ; } } return final_str ; } public static void Main ( ) { string str = " Text ▁ contains ▁ malayalam ▁ " + " and ▁ level ▁ words " ; Console . WriteLine ( removePalinWords ( str ) ) ; } }