codeparrot/xlcost-text-to-code huggingface dataset:
Check if two strings can be made equal by swapping one character among each other | C # implementation of the above approach ; Function that returns true if the string can be made equal after one swap ; A and B are new a and b after we omit the same elements ; Take only the characters which are different in both the strings for every pair of indices ; If the current characters differ ; The strings were already equal ; If the lengths of the strings are two ; If swapping these characters can make the strings equal ; Driver code
using System ; using System . Collections . Generic ; class GFG { static Boolean canBeEqual ( char [ ] a , char [ ] b , int n ) { List < char > A = new List < char > ( ) ; List < char > B = new List < char > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) { A . Add ( a [ i ] ) ; B . Add ( b [ i ] ) ; } } if ( A . Count == B . Count && B . Count == 0 ) return true ; if ( A . Count == B . Count && B . Count == 2 ) { if ( A [ 0 ] == A [ 1 ] && B [ 0 ] == B [ 1 ] ) return true ; } return false ; } public static void Main ( String [ ] args ) { char [ ] A = " SEEKSFORGEEKS " . ToCharArray ( ) ; char [ ] B = " GEEKSFORGEEKG " . ToCharArray ( ) ; if ( canBeEqual ( A , B , A . Length ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find the Mid | C # program to find the Mid - Alphabet for each index of the given Pair of Strings ; Function to find the mid alphabets ; For every character pair ; Get the average of the characters ; Driver code
using System ; public class GFG { static void findMidAlphabet ( String s1 , String s2 , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int mid = ( s1 [ i ] + s2 [ i ] ) / 2 ; Console . Write ( ( char ) mid ) ; } } public static void Main ( String [ ] args ) { String s1 = " akzbqzgw " ; String s2 = " efhctcsz " ; int n = s1 . Length ; findMidAlphabet ( s1 , s2 , n ) ; } }
Queries to find the count of vowels in the substrings of the given string | C # implementation of the approach ; Function that returns true if ch is a vowel ; pre [ i ] will store the count of vowels in the subString str [ 0. . . i ] ; Fill the pre [ ] array ; If current character is a vowel ; If its a consonant ; For every query ; Find the count of vowels for the current query ; Driver code
using System ; using System . Collections . Generic ; class GFG { static readonly int N = 2 ; static Boolean isVowel ( char ch ) { return ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) ; } static void performQueries ( String str , int len , int [ , ] queries , int q ) { int [ ] pre = new int [ len ] ; if ( isVowel ( str [ 0 ] ) ) pre [ 0 ] = 1 ; else pre [ 0 ] = 0 ; for ( int i = 1 ; i < len ; i ++ ) { if ( isVowel ( str [ i ] ) ) pre [ i ] = 1 + pre [ i - 1 ] ; else pre [ i ] = pre [ i - 1 ] ; } for ( int i = 0 ; i < q ; i ++ ) { if ( queries [ i , 0 ] == 0 ) { Console . WriteLine ( pre [ queries [ i , 1 ] ] ) ; } else { Console . WriteLine ( ( pre [ queries [ i , 1 ] ] - pre [ queries [ i , 0 ] - 1 ] ) ) ; } } } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . Length ; int [ , ] queries = { { 1 , 3 } , { 2 , 4 } , { 1 , 9 } } ; int q = queries . GetLength ( 0 ) ; performQueries ( str , len , queries , q ) ; } }
Check whether the given decoded string is divisible by 6 | C # implementation of the approach ; Function to return the sum of the digits of n ; Function that return true if the decoded string is divisible by 6 ; To store the sum of the digits ; For each character , get the sum of the digits ; If the sum of digits is not divisible by 3 ; Get the last digit of the number formed ; If the last digit is not divisible by 2 ; Driver code
using System ; class GFG { static int sumDigits ( int n ) { int sum = 0 ; while ( n > 0 ) { int digit = n % 10 ; sum += digit ; n /= 10 ; } return sum ; } static bool isDivBySix ( String str , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ( int ) ( str [ i ] - ' a ' + 1 ) ; } if ( sum % 3 != 0 ) return false ; int lastDigit = ( ( int ) ( str [ n - 1 ] - ' a ' + 1 ) ) % 10 ; if ( lastDigit % 2 != 0 ) return false ; return true ; } public static void Main ( String [ ] args ) { String str = " ab " ; int n = str . Length ; if ( isDivBySix ( str , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Check the divisibility of Hexadecimal numbers | C # implementation of the approach ; Function that returns true if s is divisible by m ; Map to map characters to real values ; To store the remainder at any stage ; Find the remainder ; Check the value of remainder ; Driver code
using System ; using System . Collections . Generic ; class GFG { static char [ ] CHARS = "0123456789ABCDEF " . ToCharArray ( ) ; static int DIGITS = 16 ; static bool isDivisible ( String s , int m ) { Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < DIGITS ; i ++ ) { if ( mp . ContainsKey ( CHARS [ i ] ) ) mp [ CHARS [ i ] ] = i ; else mp . Add ( CHARS [ i ] , i ) ; } int r = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { r = ( r * 16 + mp [ s [ i ] ] ) % m ; } if ( r == 0 ) return true ; return false ; } public static void Main ( String [ ] args ) { String s = "10" ; int m = 3 ; if ( isDivisible ( s , m ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Maximum number of splits of a binary number | C # implementation of the approach ; Function to return the required count ; If the splitting is not possible ; To store the final ans ; Counting the number of zeros ; Return the final answer ; Driver code
using System ; class GFG { static int cntSplits ( String s ) { if ( s [ s . Length - 1 ] == '1' ) return - 1 ; int ans = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) ans += ( s [ i ] == '0' ) ? 1 : 0 ; return ans ; } public static void Main ( String [ ] args ) { String s = "10010" ; Console . WriteLine ( cntSplits ( s ) ) ; } }
Modify the string such that every character gets replaced with the next character in the keyboard | C # implementation of the approach ; Function to return the modified String ; Map to store the next character on the keyboard for every possible lowercase character ; Update the String ; Driver code
using System ; using System . Collections . Generic ; class GFG { static String CHARS = " qwertyuiopasdfghjklzxcvbnm " ; static int MAX = 26 ; static String getString ( char [ ] str , int n ) { Dictionary < char , char > uMap = new Dictionary < char , char > ( ) ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( ! uMap . ContainsKey ( CHARS [ i ] ) ) uMap . Add ( CHARS [ i ] , CHARS [ ( i + 1 ) % MAX ] ) ; else uMap [ CHARS [ i ] ] = CHARS [ ( i + 1 ) % MAX ] ; } for ( int i = 0 ; i < n ; i ++ ) { str [ i ] = uMap [ str [ i ] ] ; } return String . Join ( " " , str ) ; } public static void Main ( String [ ] args ) { String str = " geeks " ; int n = str . Length ; Console . WriteLine ( getString ( str . ToCharArray ( ) , n ) ) ; } }
Minimum , maximum and average price values for all the items of given type | C # implementation of the approach ; To store an item ; To store the minimum and the maximum price for the item ; To store the total number of items of the current type and the total cost of buying them ; Initializing an element ; Function to find the minimum , the maximum and the average price for every item ; To store the distinct items ; For every item ; If the current item has aready been purchased earlier from a different shop ; Get the item ; Update its minimum and maximum price so far ; Increment the total count of the current item ; Add the current price to the sum ; The item has been purchased for the first time ; Print all the items with their minimum , maximum and average prices ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class Item { public int min , max ; public int total , sum ; public Item ( int price ) { min = price ; max = price ; total = 1 ; this . sum = price ; } } static void findPrices ( String [ ] item , int [ ] price , int n ) { Dictionary < String , Item > map = new Dictionary < String , Item > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . ContainsKey ( item [ i ] ) ) { Item currItem = map [ item [ i ] ] ; currItem . min = Math . Min ( currItem . min , price [ i ] ) ; currItem . max = Math . Max ( currItem . max , price [ i ] ) ; currItem . total ++ ; currItem . sum += price [ i ] ; } else { Item currItem = new Item ( price [ i ] ) ; map . Add ( item [ i ] , currItem ) ; } } Console . WriteLine ( " Item ▁ Min ▁ Max ▁ Average " ) ; foreach ( KeyValuePair < String , Item > ob in map ) { String key = ob . Key ; Item currItem = ob . Value ; Console . WriteLine ( key + " ▁ " + currItem . min + " ▁ " + currItem . max + " ▁ " + ( ( float ) currItem . sum / ( float ) currItem . total ) ) ; } } public static void Main ( String [ ] args ) { String [ ] item = { " toy " , " pen " , " notebook " , " pen " } ; int n = item . Length ; int [ ] price = { 2 , 1 , 3 , 2 } ; findPrices ( item , price , n ) ; } }
Queries to find the count of characters preceding the given location | C # implementation of the approach ; returns character at index pos - 1 ; Driver Code
using System ; class GFG { static int Count ( String s , int pos ) { int c = s [ pos - 1 ] ; int counter = 0 ; for ( int i = 0 ; i < pos - 1 ; i ++ ) { if ( s [ i ] == c ) counter = counter + 1 ; } return counter ; } public static void Main ( String [ ] args ) { String s = " abacsddaa " ; int pos ; int n = s . Length ; int [ ] query = { 9 , 3 , 2 } ; int Q = query . Length ; for ( int i = 0 ; i < Q ; i ++ ) { pos = query [ i ] ; Console . WriteLine ( Count ( s , pos ) ) ; } } }
Queries to find the count of characters preceding the given location | C # implementation of the approach ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void Count ( int [ ] temp ) { int [ ] query = { 9 , 3 , 2 } ; int Q = query . Length ; for ( int i = 0 ; i < Q ; i ++ ) { int pos = query [ i ] ; Console . WriteLine ( temp [ pos - 1 ] ) ; } } static int [ ] processing ( String s , int len ) { int [ ] temp = new int [ len ] ; Dictionary < int , int > d = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( ! d . ContainsKey ( s [ i ] ) ) { d . Add ( s [ i ] , i ) ; } else { temp [ i ] = temp [ d [ s [ i ] ] ] + 1 ; d [ s [ i ] ] = i ; } } return temp ; } public static void Main ( String [ ] args ) { String s = " abacsddaa " ; int n = s . Length ; int [ ] temp = processing ( s , n ) ; Count ( temp ) ; } }
Count of matchsticks required to represent the given number | C # implementation of the approach ; stick [ i ] stores the count of sticks required to represent the digit i ; Function to return the count of matchsticks required to represent the given number ; For every digit of the given number ; Add the count of sticks required to represent the current digit ; Driver code
using System ; class GFG { static int [ ] sticks = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; static int countSticks ( String str , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cnt += ( sticks [ str [ i ] - '0' ] ) ; } return cnt ; } public static void Main ( String [ ] args ) { String str = "56" ; int n = str . Length ; Console . WriteLine ( countSticks ( str , n ) ) ; } }
Print characters in decreasing order of frequency | C # implementation of the approach ; Function to print the characters of the given String in decreasing order of their frequencies ; To store the ; Map 's size ; While there are elements in the map ; Finding the maximum value from the map ; Print the character alongwith its frequency ; Delete the maximum value ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void printChar ( char [ ] arr , int len ) { Dictionary < char , int > occ = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < len ; i ++ ) if ( occ . ContainsKey ( arr [ i ] ) ) { occ [ arr [ i ] ] = occ [ arr [ i ] ] + 1 ; } else { occ . Add ( arr [ i ] , 1 ) ; } int size = occ . Count ; while ( size -- > 0 ) { int currentMax = 0 ; char arg_max = ( char ) 0 ; foreach ( KeyValuePair < char , int > it in occ ) { if ( it . Value > currentMax || ( it . Value == currentMax && it . Key > arg_max ) ) { arg_max = it . Key ; currentMax = it . Value ; } } Console . Write ( arg_max + " ▁ - ▁ " + currentMax + " STRNEWLINE " ) ; occ . Remove ( arg_max ) ; } } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . Length ; printChar ( str . ToCharArray ( ) , len ) ; } }
Modulo of a large Binary String | C # implementation of the above approach ; Function to return the value of ( str % k ) ; pwrTwo [ i ] will store ( ( 2 ^ i ) % k ) ; To store the result ; If current bit is 1 ; Add the current power of 2 ; Driver code
using System ; class GFG { static int getMod ( string str , int n , int k ) { int i ; int [ ] pwrTwo = new int [ n ] ; pwrTwo [ 0 ] = 1 % k ; for ( i = 1 ; i < n ; i ++ ) { pwrTwo [ i ] = pwrTwo [ i - 1 ] * ( 2 % k ) ; pwrTwo [ i ] %= k ; } int res = 0 ; i = 0 ; int j = n - 1 ; while ( i < n ) { if ( str [ j ] == '1' ) { res += ( pwrTwo [ i ] ) ; res %= k ; } i ++ ; j -- ; } return res ; } public static void Main ( ) { string str = "1101" ; int n = str . Length ; int k = 45 ; Console . Write ( getMod ( str , n , k ) ) ; } }
Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1 's | C # implementation of the above approach ; Function to return the count of all possible valid strings ; Fill 0 's in the dp array ; Base cases ; dp [ i , j ] = number of possible strings such that '1' just appeared consecutively j times upto the ith index ; Taking previously calculated value ; Taking all possible cases that can appear at the Nth position ; Driver code
using System ; class GFG { static readonly int MOD = 1000000007 ; static long countStrings ( int N ) { int i , j ; int [ , ] dp = new int [ N + 1 , 3 ] ; for ( i = 0 ; i < N + 1 ; i ++ ) { for ( j = 9 ; j < 3 ; j ++ ) { dp [ i , j ] = 0 ; } } dp [ 1 , 0 ] = 1 ; dp [ 1 , 1 ] = 1 ; dp [ 1 , 2 ] = 0 ; for ( i = 2 ; i <= N ; i ++ ) { dp [ i , 0 ] = ( dp [ i - 1 , 0 ] + dp [ i - 1 , 1 ] + dp [ i - 1 , 2 ] ) % MOD ; dp [ i , 1 ] = dp [ i - 1 , 0 ] % MOD ; dp [ i , 2 ] = dp [ i - 1 , 1 ] % MOD ; } int ans = ( dp [ N , 0 ] + dp [ N , 1 ] + dp [ N , 2 ] ) % MOD ; return ans ; } public static void Main ( String [ ] args ) { int N = 3 ; Console . WriteLine ( countStrings ( N ) ) ; } }
Find the maximum possible Binary Number from given string | C # implementation of the approach ; Function to return maximum number that can be formed from the string ; To store the frequency of ' z ' and ' n ' in the given string ; Number of zeroes ; Number of ones ; To store the required number ; Add all the ones ; Add all the zeroes ; Driver code
using System ; class GFG { static string maxNumber ( string str , int n ) { int [ ] freq = new int [ 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' z ' ) { freq [ 0 ] ++ ; } else if ( str [ i ] == ' n ' ) { freq [ 1 ] ++ ; } } string num = " " ; for ( int i = 0 ; i < freq [ 1 ] ; i ++ ) num += '1' ; for ( int i = 0 ; i < freq [ 0 ] ; i ++ ) num += '0' ; return num ; } public static void Main ( ) { string str = " roenenzooe " ; int n = str . Length ; Console . Write ( maxNumber ( str , n ) ) ; } }
Contiguous unique substrings with the given length L | C # implementation for above approach ; Function to print the unique sub - String of length n ; set to store the Strings ; if the size of the String is equal to 1 then insert ; inserting unique sub - String of length L ; Printing the set of Strings ; Driver Code ; Function calling
using System ; using System . Collections . Generic ; class GFG { static void result ( String s , int n ) { HashSet < String > st = new HashSet < String > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { String ans = " " ; for ( int j = i ; j < s . Length ; j ++ ) { ans += s [ j ] ; if ( ans . Length == n ) { st . Add ( ans ) ; break ; } } } foreach ( String it in st ) Console . Write ( it + " ▁ " ) ; } public static void Main ( String [ ] args ) { String s = " abca " ; int n = 3 ; result ( s , n ) ; } }
Find the occurrence of the given binary pattern in the binary representation of the array elements | C # code implementation for above approach ; Function to return the binary representation of n ; Array to store binary representation ; Counter for binary array ; Storing remainder in binary array ; To store the binary representation as a string ; Function to return the frequency of pat in the given string txt ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; If pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Function to find the occurrence of the given pattern in the binary representation of elements of arr [ ] ; For every element of the array ; Find its binary representation ; Print the occurrence of the given pattern in its binary representation ; Driver code
using System ; class GFG { static String decToBinary ( int n ) { int [ ] binaryNum = new int [ 32 ] ; int i = 0 ; while ( n > 0 ) { binaryNum [ i ] = n % 2 ; n = n / 2 ; i ++ ; } String binary = " " ; for ( int j = i - 1 ; j >= 0 ; j -- ) { binary += String . Join ( " " , binaryNum [ j ] ) ; } return binary ; } static int countFreq ( String pat , String txt ) { int M = pat . Length ; int N = txt . Length ; int res = 0 ; for ( int i = 0 ; i <= N - M ; i ++ ) { int j ; for ( j = 0 ; j < M ; j ++ ) { if ( txt [ i + j ] != pat [ j ] ) { break ; } } if ( j == M ) { res ++ ; j = 0 ; } } return res ; } static void findOccurrence ( int [ ] arr , int n , String pattern ) { for ( int i = 0 ; i < n ; i ++ ) { String binary = decToBinary ( arr [ i ] ) ; Console . Write ( countFreq ( pattern , binary ) + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 106 , 7 , 8 } ; String pattern = "10" ; int n = arr . Length ; findOccurrence ( arr , n , pattern ) ; } }
Check if the bracket sequence can be balanced with at most one change in the position of a bracket | C # implementation of the approach ; Function that returns true if the sequence can be balanced by changing the position of at most one bracket ; Odd length string can never be balanced ; Add ' ( ' in the beginning and ' ) ' in the end of the string ; If its an opening bracket then append it to the temp string ; If its a closing bracket ; There was an opening bracket to match it with ; No opening bracket to match it with ; Sequence is balanced ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool canBeBalanced ( string s , int n ) { if ( n % 2 == 1 ) return false ; string k = " ( " ; k += s + " ) " ; List < string > d = new List < string > ( ) ; for ( int i = 0 ; i < k . Length ; i ++ ) { if ( k [ i ] == ' ( ' ) d . Add ( " ( " ) ; else { if ( d . Count != 0 ) d . RemoveAt ( d . Count - 1 ) ; else return false ; } } if ( d . Count == 0 ) return true ; return false ; } public static void Main ( ) { string s = " ) ( ( ) " ; int n = s . Length ; if ( canBeBalanced ( s , n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Find the winner of the game | C # implementation of the approach ; Function to find the winner of the game ; To store the strings for both the players ; If the index is even ; Append the current character to player A 's string ; If the index is odd ; Append the current character to player B 's string ; Sort both the strings to get the lexicographically smallest string possible ; Copmpare both the strings to find the winner of the game ; Driver code
using System ; class GFG { static void find_winner ( String str , int n ) { String str1 = " " , str2 = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { str1 += str [ i ] ; } else { str2 += str [ i ] ; } } char [ ] a = str1 . ToCharArray ( ) ; Array . Sort ( a ) ; char [ ] b = str2 . ToCharArray ( ) ; Array . Sort ( b ) ; str1 = new String ( a ) ; str2 = new String ( b ) ; if ( str1 . CompareTo ( str2 ) < 0 ) Console . Write ( " A " ) ; else if ( str1 . CompareTo ( str2 ) > 0 ) Console . Write ( " B " ) ; else Console . Write ( " Tie " ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int n = str . Length ; find_winner ( str , n ) ; } }
Count of characters in str1 such that after deleting anyone of them str1 becomes str2 | Program to print the given pattern ; Function to return the count of required indices ; Solution doesn 't exist ; Find the length of the longest common prefix of strings ; Find the length of the longest common suffix of strings ; If solution does not exist ; Return the count of indices ; Driver code
using System ; class GFG { static int Find_Index ( String str1 , String str2 ) { int n = str1 . Length ; int m = str2 . Length ; int l = 0 ; int r = 0 ; int i , j ; if ( n != m + 1 ) { return - 1 ; } for ( i = 0 ; i < m ; i ++ ) { if ( str1 [ i ] == str2 [ i ] ) { l += 1 ; } else { break ; } } i = n - 1 ; j = m - 1 ; while ( i >= 0 && j >= 0 && str1 [ i ] == str2 [ j ] ) { r += 1 ; i -= 1 ; j -= 1 ; } if ( l + r < m ) { return - 1 ; } else { i = Math . Max ( n - r , 1 ) ; j = Math . Min ( l + 1 , n ) ; return ( j - i + 1 ) ; } } public static void Main ( String [ ] args ) { String str1 = " aaa " , str2 = " aa " ; Console . WriteLine ( Find_Index ( str1 , str2 ) ) ; } }
Expand the string according to the given conditions | C # implementation of the above approach ; Function to expand and print the given string ; Subtract '0' to convert char to int ; Characters within brackets ; Expanding ; Reset the variables ; Driver code
using System ; class GFG { static void expandString ( string strin ) { string temp = " " ; int j ; for ( int i = 0 ; i < strin . Length ; i ++ ) { if ( strin [ i ] >= 0 ) { int num = strin [ i ] - '0' ; if ( strin [ i + 1 ] == ' ( ' ) { for ( j = i + 1 ; strin [ j ] != ' ) ' ; j ++ ) { if ( ( strin [ j ] >= ' a ' && strin [ j ] <= ' z ' ) || ( strin [ j ] >= ' A ' && strin [ j ] <= ' Z ' ) ) { temp += strin [ j ] ; } } for ( int k = 1 ; k <= num ; k ++ ) { Console . Write ( temp ) ; } num = 0 ; temp = " " ; if ( j < strin . Length ) { i = j ; } } } } } public static void Main ( String [ ] args ) { string strin = "3 ( ab ) 4 ( cd ) " ; expandString ( strin ) ; } }
Largest substring of str2 which is a prefix of str1 | C # implementation of the approach ; Function to return the largest substring in str2 which is a prefix of str1 ; To store the index in str2 which matches the prefix in str1 ; While there are characters left in str1 ; If the prefix is not found in str2 ; Remove the last character ; Prefix found ; No substring found in str2 that matches the prefix of str1 ; Driver code
using System ; class GFG { static String findPrefix ( String str1 , String str2 ) { bool pos = false ; while ( str1 . Length > 0 ) { if ( ! str2 . Contains ( str1 ) ) str1 = str1 . Substring ( 0 , str1 . Length - 1 ) ; else { pos = str2 . Contains ( str1 ) ; break ; } } if ( pos == false ) return " - 1" ; return str1 ; } public static void Main ( String [ ] args ) { String str1 = " geeksfor " ; String str2 = " forgeeks " ; Console . WriteLine ( findPrefix ( str1 , str2 ) ) ; } }