codeparrot/xlcost-text-to-code huggingface dataset:
Sum of Bitwise AND of each array element with the elements of another array | C # program for the above approach ; Driver code ; Given arr1 [ ] ; Given arr2 [ ] ; Size of arr1 [ ] ; Size of arr2 [ ] ; Function Call ; Function to compute the AND sum for each element of an array ; Declaring an array of size 32 for storing the count of each bit ; Traverse the array arr2 [ ] and store the count of a bit in frequency array ; Current bit position ; While num is greater than 0 ; Checks if ith bit is set or not ; Increment the count of bit by one ; Increment the bit position by one ; Right shift the num by one ; Traverse in the arr2 [ ] ; Store the ith bit value ; Total required sum ; Traverse in the range [ 0 , 31 ] ; Checks if current bit is set ; Increment the bitwise sum by frequency [ bit_position ] * value_at_that_bit ; ; Right shift num by one ; Left shift vale_at_that_bit by one ; Print the sum obtained for ith number in arr1 [ ]
using System ; class GFG { static public void Main ( ) { int [ ] arr1 = { 1 , 2 , 3 } ; int [ ] arr2 = { 1 , 2 , 3 } ; int N = arr1 . Length ; int M = arr2 . Length ; Bitwise_AND_sum_i ( arr1 , arr2 , M , N ) ; } static void Bitwise_AND_sum_i ( int [ ] arr1 , int [ ] arr2 , int M , int N ) { int [ ] frequency = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { int bit_position = 0 ; int num = arr1 [ i ] ; while ( num != 0 ) { if ( ( num & 1 ) != 0 ) { frequency [ bit_position ] += 1 ; } bit_position += 1 ; num >>= 1 ; } } for ( int i = 0 ; i < M ; i ++ ) { int num = arr2 [ i ] ; int value_at_that_bit = 1 ; int bitwise_AND_sum = 0 ; for ( int bit_position = 0 ; bit_position < 32 ; bit_position ++ ) { if ( ( num & 1 ) != 0 ) { bitwise_AND_sum += frequency [ bit_position ] * value_at_that_bit ; } num >>= 1 ; value_at_that_bit <<= 1 ; } Console . Write ( bitwise_AND_sum + " ▁ " ) ; } } }
Turn off the rightmost set bit | Set 2 | C # program to unset the rightmost set bit ; Unsets the rightmost set bit of n and returns the result ; Checking whether bit position is set or not ; If bit position is found set , we flip this bit by xoring given number and number with bit position set ; Driver code
using System ; class GFG { static void FlipBits ( int n ) { for ( int bit = 0 ; bit < 32 ; bit ++ ) { if ( ( n >> bit ) % 2 > 0 ) { n = n ^ ( 1 << bit ) ; break ; } } Console . Write ( " The ▁ number ▁ after ▁ unsetting ▁ the ▁ " ) ; Console . Write ( " rightmost ▁ set ▁ bit ▁ " + n ) ; } static void Main ( ) { int N = 12 ; FlipBits ( N ) ; } }
Bitwise AND of all the odd numbers from 1 to N | C # implementation of the approach ; Function to return the bitwise AND of all the odd integers from the range [ 1 , n ] ; Initialize result to 1 ; Starting from 3 , bitwise AND all the odd integers less than or equal to n ; Driver code
using System ; class GFG { static int bitwiseAndOdd ( int n ) { int result = 1 ; for ( int i = 3 ; i <= n ; i = i + 2 ) { result = ( result & i ) ; } return result ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( bitwiseAndOdd ( n ) ) ; } }
Bitwise AND of all the odd numbers from 1 to N | C # implementation of the approach ; Function to return the bitwise AND of all the odd integers from the range [ 1 , n ] ; Driver code
using System ; class GFG { static int bitwiseAndOdd ( int n ) { return 1 ; } public static void Main ( ) { int n = 10 ; Console . WriteLine ( bitwiseAndOdd ( n ) ) ; } }
Reverse actual bits of the given number | C # implementation to reverse bits of a number ; function to reverse bits of a number ; traversing bits of ' n ' from the right ; bitwise left shift ' rev ' by 1 ; if current bit is '1' ; bitwise right shift ' n ' by 1 ; required number ; Driver code
using System ; class GFG { public static int reverseBits ( int n ) { int rev = 0 ; while ( n > 0 ) { rev <<= 1 ; if ( ( int ) ( n & 1 ) == 1 ) rev ^= 1 ; n >>= 1 ; } return rev ; } public static void Main ( ) { int n = 11 ; Console . WriteLine ( reverseBits ( n ) ) ; } }
Ways to split array into two groups of same XOR value | C # Program to count number of ways to split array into two groups such that each group has equal XOR value ; Return the count number of ways to split array into two groups such that each group has equal XOR value . ; We can split only if XOR is 0. Since XOR of all is 0 , we can consider all subsets as one group . ; Driver program
using System ; class GFG { static int countgroup ( int [ ] a , int n ) { int xs = 0 ; for ( int i = 0 ; i < n ; i ++ ) xs = xs ^ a [ i ] ; if ( xs == 0 ) return ( 1 << ( n - 1 ) ) - 1 ; return 0 ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 3 } ; int n = a . Length ; Console . WriteLine ( countgroup ( a , n ) ) ; } }
Extract ' k ' bits from a given position in a number . | C # program to extract k bits from a given position . ; Function to extract k bits from p position and returns the extracted value as integer ; Driver code
using System ; class GFG { static int bitExtracted ( int number , int k , int p ) { return ( ( ( 1 << k ) - 1 ) & ( number >> ( p - 1 ) ) ) ; } public static void Main ( ) { int number = 171 , k = 5 , p = 2 ; Console . WriteLine ( " The ▁ extracted ▁ number ▁ is ▁ " + bitExtracted ( number , k , p ) ) ; } }
Maximize a given unsigned number number by swapping bits at it 's extreme positions. | C # program to find maximum number by swapping extreme bits . ; Traverse bits from both extremes ; Obtaining i - th and j - th bits ; Swapping the bits if lesser significant is greater than higher significant bit and accordingly modifying the number ; Driver code
using System ; public class GFG { static int findMax ( int num ) { byte size_of_int = 4 ; int num_copy = num ; int j = size_of_int * 8 - 1 ; int i = 0 ; while ( i < j ) { int m = ( num_copy >> i ) & 1 ; int n = ( num_copy >> j ) & 1 ; if ( m > n ) { int x = ( 1 << i 1 << j ) ; num = num ^ x ; } i ++ ; j -- ; } return num ; } static public void Main ( ) { int num = 4 ; Console . Write ( findMax ( num ) ) ; } }
Efficiently check whether n is a multiple of 4 or not | C # implementation to efficiently check whether n is a multiple of 4 or not ; method to check whether ' n ' is a multiple of 4 or not ; if true , then ' n ' is a multiple of 4 ; else ' n ' is not a multiple of 4 ; Driver method
using System ; class GFG { static bool isAMultipleOf4 ( int n ) { if ( ( n & 3 ) == 0 ) return true ; return false ; } public static void Main ( ) { int n = 16 ; Console . WriteLine ( isAMultipleOf4 ( n ) ? " Yes " : " No " ) ; } }
Calculate square of a number without using * , / and pow ( ) | C # Simple solution to calculate square without using * and pow ( ) ; handle negative input ; Initialize result ; Add n to res n - 1 times ; Driver code
using System ; class GFG { public static int square ( int n ) { if ( n < 0 ) n = - n ; int res = n ; for ( int i = 1 ; i < n ; i ++ ) res += n ; return res ; } public static void Main ( ) { for ( int n = 1 ; n <= 5 ; n ++ ) Console . WriteLine ( " n ▁ = ▁ " + n + " , ▁ n ^ 2 ▁ = ▁ " + square ( n ) ) ; } }
Find a point that lies inside exactly K given squares | C # implementation of the approach ; Driver Code
using System ; class GFG { static int PointInKSquares ( int n , int [ ] a , int k ) { Array . Sort ( a ) ; return a [ n - k ] ; } public static void Main ( String [ ] args ) { int k = 2 ; int [ ] a = { 1 , 2 , 3 , 4 } ; int n = a . Length ; int x = PointInKSquares ( n , a , k ) ; Console . WriteLine ( " ( " + x + " , ▁ " + x + " ) " ) ; } }
Number of n digit stepping numbers | Space optimized solution | C # program to calculate the number of n digit stepping numbers . ; function that calculates the answer ; dp [ j ] stores count of i digit stepping numbers ending with digit j . ; To store result of length i - 1 before updating dp [ j ] for length i . ; if n is 1 then answer will be 10. ; Initialize values for count of digits equal to 1. ; Compute values for count of digits more than 1. ; If ending digit is 0 ; If ending digit is 9 ; For other digits . ; stores the final answer ; Driver code
using System ; class GFG { static long answer ( int n ) { int [ ] dp = new int [ 10 ] ; int [ ] prev = new int [ 10 ] ; if ( n == 1 ) return 10 ; for ( int j = 0 ; j <= 9 ; j ++ ) dp [ j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { prev [ j ] = dp [ j ] ; } for ( int j = 0 ; j <= 9 ; j ++ ) { if ( j == 0 ) dp [ j ] = prev [ j + 1 ] ; else if ( j == 9 ) dp [ j ] = prev [ j - 1 ] ; else dp [ j ] = prev [ j - 1 ] + prev [ j + 1 ] ; } } long sum = 0 ; for ( int j = 1 ; j <= 9 ; j ++ ) sum += dp [ j ] ; return sum ; } static void Main ( ) { int n = 2 ; Console . WriteLine ( answer ( n ) ) ; } }
Minimum changes required to make a Catalan Sequence | C # implementation of the approach ; To store first N Catalan numbers ; Function to find first n Catalan numbers ; Initialize first two values in table ; Filong entries in catalan [ ] using recursive formula ; Function to return the minimum changes required ; Find first n Catalan Numbers ; a and b are first two Catalan Sequence numbers ; Insert first n catalan elements to set ; If catalan element is present in the array then remove it from set ; Return the remaining number of elements in the set ; Driver code
using System ; using System . Collections . Generic ; class GFG1 { static int MAX = 100000 ; static long [ ] catalan = new long [ MAX ] ; static void catalanDP ( long n ) { catalan [ 0 ] = catalan [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { catalan [ i ] = 0 ; for ( int j = 0 ; j < i ; j ++ ) { catalan [ i ] += catalan [ j ] * catalan [ i - j - 1 ] ; } } } static int CatalanSequence ( int [ ] arr , int n ) { catalanDP ( n ) ; HashSet < int > s = new HashSet < int > ( ) ; int a = 1 , b = 1 ; s . Add ( a ) ; if ( n >= 2 ) { s . Add ( b ) ; } for ( int i = 2 ; i < n ; i ++ ) { s . Add ( ( int ) catalan [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( s . Contains ( arr [ i ] ) ) { s . Remove ( arr [ i ] ) ; } } return s . Count ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 5 , 41 } ; int n = arr . Length ; Console . WriteLine ( CatalanSequence ( arr , n ) ) ; } }
Sum of all the Composite Numbers from Odd indices of the given array | C # implementation to find the sum of all the composite numbers from odd indices of the given array ; Function to check for composite numbers ; Check if the factors are greater than 2 ; Check if the number is composite or not ; Function to print the sum of all composite numbers in the array ; Iterate for odd indices in the array ; Check if the number is composite then add it to sum ; return the sum ; Driver code
using System ; class GFG { static int composite ( int n ) { int flag = 0 ; int c = 0 ; for ( int j = 1 ; j <= n ; j ++ ) { if ( n % j == 0 ) { c += 1 ; } } if ( c >= 3 ) flag = 1 ; return flag ; } static void odd_indices ( int [ ] arr , int n ) { int sum = 0 ; for ( int k = 0 ; k < n ; k += 2 ) { int check = composite ( arr [ k ] ) ; if ( check == 1 ) sum += arr [ k ] ; } Console . Write ( sum + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 13 , 5 , 8 , 16 , 25 } ; int n = arr . Length ; odd_indices ( arr , n ) ; } }
Queries on count of points lie inside a circle | C # Code for Queries on count of points lie inside a circle ; Computing the x ^ 2 + y ^ 2 for each given points and sorting them . ; Return count of points lie inside or on circumference of circle using binary search on p [ 0. . n - 1 ] ; Driver program to test above function ; Compute distances of all points and keep the distances sorted so that query can work in O ( logn ) using Binary Search . ; Print number of points in a circle of radius 3. ; Print number of points in a circle of radius 32.
using System ; class GFG { public static void preprocess ( int [ ] p , int [ ] x , int [ ] y , int n ) { for ( int i = 0 ; i < n ; i ++ ) p [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] ; Array . Sort ( p ) ; } public static int query ( int [ ] p , int n , int rad ) { int start = 0 , end = n - 1 ; while ( ( end - start ) > 1 ) { int mid = ( start + end ) / 2 ; double tp = Math . Sqrt ( p [ mid ] ) ; if ( tp > ( rad * 1.0 ) ) end = mid - 1 ; else start = mid ; } double tp1 = Math . Sqrt ( p [ start ] ) ; double tp2 = Math . Sqrt ( p [ end ] ) ; if ( tp1 > ( rad * 1.0 ) ) return 0 ; else if ( tp2 <= ( rad * 1.0 ) ) return end + 1 ; else return start + 1 ; } public static void Main ( ) { int [ ] x = { 1 , 2 , 3 , - 1 , 4 } ; int [ ] y = { 1 , 2 , 3 , - 1 , 4 } ; int n = x . Length ; int [ ] p = new int [ n ] ; preprocess ( p , x , y , n ) ; Console . WriteLine ( query ( p , n , 3 ) ) ; Console . WriteLine ( query ( p , n , 32 ) ) ; } }
Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices | C # program to Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices ; function to find total number of ways ; No of odd indices in n - digit number ; No of even indices in n - digit number ; No of ways of arranging prime number digits in odd indices ; No of ways of arranging odd number digits in even indices ; returning the total number of ways ; drive code
using System ; using System . Collections . Generic ; class GFG { static int find_Numb_ways ( int n ) { int odd_indices = n / 2 ; int even_indices = ( n / 2 ) + ( n % 2 ) ; int arr_odd = ( int ) Math . Pow ( 4 , odd_indices ) ; int arr_even = ( int ) Math . Pow ( 5 , even_indices ) ; return arr_odd * arr_even ; } public static void Main ( ) { int n = 4 ; Console . Write ( find_Numb_ways ( n ) ) ; } }
Check if a given array is sorted in Spiral manner or not | C # program to implement the above approach ; Function to check if the array is spirally sorted or not ; Stores start index of the array ; Stores end index of an array ; If arr [ start ] greater than arr [ end ] ; Update start ; If arr [ end ] greater than arr [ start ] ; Update end ; Driver code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static bool isSpiralSorted ( int [ ] arr , int n ) { int start = 0 ; int end = n - 1 ; while ( start < end ) { if ( arr [ start ] > arr [ end ] ) { return false ; } start ++ ; if ( arr [ end ] > arr [ start ] ) { return false ; } end -- ; } return true ; } static void Main ( ) { int [ ] arr = { 1 , 10 , 14 , 20 , 18 , 12 , 5 } ; int N = arr . Length ; if ( isSpiralSorted ( arr , N ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Print all strings from given array that can be typed using keys from a single row of a QWERTY keyboard | C # program to implement the above approach ; Function to print all strings that can be typed using keys of a single row in a QWERTY Keyboard ; Stores row number of all possible character of the strings ; Traverse the array ; If current string is not an empty string ; Sets true / false if a string can be typed using keys of a single row or not ; Stores row number of the first character of current string ; Stores length of word ; Traverse current string ; If current character can 't be  typed using keys of rowNum only ; Update flag ; If current string can be typed using keys from rowNum only ; Print the string ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void findWordsSameRow ( List < string > arr ) { Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; mp . Add ( ' q ' , 1 ) ; mp . Add ( ' w ' , 1 ) ; mp . Add ( ' e ' , 1 ) ; mp . Add ( ' r ' , 1 ) ; mp . Add ( ' t ' , 1 ) ; mp . Add ( ' y ' , 1 ) ; mp . Add ( ' u ' , 1 ) ; mp . Add ( ' i ' , 1 ) ; mp . Add ( ' o ' , 1 ) ; mp . Add ( ' p ' , 1 ) ; mp . Add ( ' a ' , 2 ) ; mp . Add ( ' s ' , 2 ) ; mp . Add ( ' d ' , 2 ) ; mp . Add ( ' f ' , 2 ) ; mp . Add ( ' g ' , 2 ) ; mp . Add ( ' h ' , 2 ) ; mp . Add ( ' j ' , 2 ) ; mp . Add ( ' k ' , 2 ) ; mp . Add ( ' l ' , 2 ) ; mp . Add ( ' z ' , 3 ) ; mp . Add ( ' x ' , 3 ) ; mp . Add ( ' c ' , 3 ) ; mp . Add ( ' v ' , 3 ) ; mp . Add ( ' b ' , 3 ) ; mp . Add ( ' n ' , 3 ) ; mp . Add ( ' m ' , 3 ) ; foreach ( string word in arr ) { if ( word . Length != 0 ) { bool flag = true ; int rowNum = mp [ char . ToLower ( word [ 0 ] ) ] ; int M = word . Length ; for ( int i = 1 ; i < M ; i ++ ) { if ( mp [ Char . ToLower ( word [ i ] ) ] != rowNum ) { flag = false ; break ; } } if ( flag ) { Console . Write ( word + " ▁ " ) ; } } } } public static void Main ( String [ ] args ) { List < string > words = new List < string > ( new string [ ] { " Yeti " , " Had " , " GFG " , " comment " } ) ; findWordsSameRow ( words ) ; } }
Count quadruples of given type from given array | C # program of the above approach ; Function to find the count of the subsequence of given type ; Stores the count of quadruples ; Generate all possible combinations of quadruples ; Check if 1 st element is equal to 3 rd element ; Check if 2 nd element is equal to 4 th element ; Driver Code
using System ; class GFG { static int countSubsequece ( int [ ] a , int n ) { int i , j , k , l ; int answer = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { for ( k = j + 1 ; k < n ; k ++ ) { for ( l = k + 1 ; l < n ; l ++ ) { if ( a [ j ] == a [ l ] && a [ i ] == a [ k ] ) { answer ++ ; } } } } } return answer ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 3 , 2 , 1 , 3 , 2 } ; Console . WriteLine ( countSubsequece ( a , 7 ) ) ; } }
Smallest character in a string having minimum sum of distances between consecutive repetitions | C # program for the above approach ; Function to find the character repeats with minimum distance ; Stores the first and last index ; Initialize with - 1 ; Get the values of last and first occurence ; Update the first index ; Update the last index ; Initialize min ; Get the minimum ; Values must not be same ; Update the minimum distance ; return ans ; Driver Code ; Function call
using System ; using System . Collections . Generic ; class GFG { static char minDistChar ( char [ ] s ) { int n = s . Length ; int [ ] first = new int [ 26 ] ; int [ ] last = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { first [ i ] = - 1 ; last [ i ] = - 1 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( first [ s [ i ] - ' a ' ] == - 1 ) { first [ s [ i ] - ' a ' ] = i ; } last [ s [ i ] - ' a ' ] = i ; } int min = int . MaxValue ; char ans = '1' ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( last [ i ] == first [ i ] ) continue ; if ( min > last [ i ] - first [ i ] ) { min = last [ i ] - first [ i ] ; ans = ( char ) ( i + ' a ' ) ; } } return ans ; } public static void Main ( string [ ] args ) { String str = " geeksforgeeks " ; Console . Write ( minDistChar ( str . ToCharArray ( ) ) ) ; } }
Minimum steps required to reach the end of a matrix | Set 2 | C # implementation of the approach ; Function to return the minimum steps required to reach the end of the matrix ; Array to determine whether a cell has been visited before ; Queue for bfs ; Initializing queue ; To store the depth of search ; BFS algorithm ; Current queue size ; Top - most element of queue ; To store index of cell for simplicity ; Base case ; If we reach ( n - 1 , n - 1 ) ; Marking the cell visited ; Pushing the adjacent cells in the queue that can be visited from the current cell ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int n = 3 ; public class Pair { public int first , second ; public Pair ( int a , int b ) { first = a ; second = b ; } } static int minSteps ( int [ , ] arr ) { Boolean [ , ] v = new Boolean [ n , n ] ; Queue < Pair > q = new Queue < Pair > ( ) ; q . Enqueue ( new Pair ( 0 , 0 ) ) ; int depth = 0 ; while ( q . Count != 0 ) { int x = q . Count ; while ( x -- > 0 ) { Pair y = q . Peek ( ) ; int i = y . first , j = y . second ; q . Dequeue ( ) ; if ( v [ i , j ] ) continue ; if ( i == n - 1 && j == n - 1 ) return depth ; v [ i , j ] = true ; if ( i + arr [ i , j ] < n ) q . Enqueue ( new Pair ( i + arr [ i , j ] , j ) ) ; if ( j + arr [ i , j ] < n ) q . Enqueue ( new Pair ( i , j + arr [ i , j ] ) ) ; } depth ++ ; } return - 1 ; } public static void Main ( ) { int [ , ] arr = { { 1 , 1 , 1 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; Console . WriteLine ( minSteps ( arr ) ) ; } }
Largest gap in an array | A C # program to find largest gap between two elements in an array . ; function to solve the given problem ; Driver Code
using System ; class GFG { static int solve ( int [ ] a , int n ) { int max1 = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( Math . Abs ( a [ i ] - a [ j ] ) > max1 ) { max1 = Math . Abs ( a [ i ] - a [ j ] ) ; } } } return max1 ; } static public void Main ( ) { int [ ] arr = { - 1 , 2 , 3 , - 4 , - 10 , 22 } ; int size = arr . Length ; Console . WriteLine ( " Largest ▁ gap ▁ is ▁ : ▁ " + solve ( arr , size ) ) ; } }
Largest gap in an array | A C # program to find largest gap between two elements in an array . ; function to solve the given problem ; finding maximum and minimum of an array ; Driver code
using System ; class GFG { static int solve ( int [ ] a , int n ) { int min1 = a [ 0 ] ; int max1 = a [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > max1 ) max1 = a [ i ] ; if ( a [ i ] < min1 ) min1 = a [ i ] ; } return Math . Abs ( min1 - max1 ) ; } public static void Main ( ) { int [ ] arr = { - 1 , 2 , 3 , 4 , - 10 } ; int size = arr . Length ; Console . WriteLine ( " Largest ▁ gap ▁ is ▁ : ▁ " + solve ( arr , size ) ) ; } }
Print reverse string after removing vowels | C # Program for removing characters from reversed string where vowels are present in original string ; Function for replacing the string ; initialize a string of length n ; Traverse through all characters of string ; assign the value to string r from last index of string s ; if s [ i ] is a consonant then print r [ i ] ; Driver code
using System ; class GFG { static void replaceOriginal ( String s , int n ) { char [ ] r = new char [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { r [ i ] = s [ n - 1 - i ] ; if ( s [ i ] != ' a ' && s [ i ] != ' e ' && s [ i ] != ' i ' && s [ i ] != ' o ' && s [ i ] != ' u ' ) { Console . Write ( r [ i ] ) ; } } Console . WriteLine ( " " ) ; } public static void Main ( String [ ] args ) { String s = " geeksforgeeks " ; int n = s . Length ; replaceOriginal ( s , n ) ; } }
Check if a string can be made equal to another string by swapping or replacement of characters | C # program for the above approach ; Function to find if given Strings are same or not ; Base Condition ; Stores frequency of characters of the String str1 and str2 ; Traverse Strings str1 & str2 and store frequencies in a [ ] and b [ ] ; Check if both Strings have same characters or not ; If a character is present in one String and is not in another String , return false ; Sort the array a [ ] and b [ ] ; Check arrays a and b contain the same frequency or not ; If the frequencies are not the same after sorting ; At this point , str1 can be converted to str2 ; Driver Code
using System ; class GFG { static bool sameStrings ( string str1 , string str2 ) { int N = str1 . Length ; int M = str2 . Length ; if ( N != M ) { return false ; } int [ ] a = new int [ 256 ] ; int [ ] b = new int [ 256 ] ; for ( int j = 0 ; j < N ; j ++ ) { a [ str1 [ j ] - ' a ' ] ++ ; b [ str2 [ j ] - ' a ' ] ++ ; } int i = 0 ; while ( i < 256 ) { if ( ( a [ i ] == 0 && b [ i ] == 0 ) || ( a [ i ] != 0 && b [ i ] != 0 ) ) { i ++ ; } else { return false ; } } Array . Sort ( a ) ; Array . Sort ( b ) ; for ( int j = 0 ; j < 256 ; j ++ ) { if ( a [ j ] != b [ j ] ) return false ; } return true ; } static public void Main ( ) { string S1 = " cabbba " , S2 = " abbccc " ; if ( sameStrings ( S1 , S2 ) ) Console . Write ( " YES " + " STRNEWLINE " ) ; else Console . Write ( " ▁ NO " + " STRNEWLINE " ) ; } }