Check whether two strings contain same characters in same order
Given two strings s1 and s2, the task is to find whether the two string contain the same characters that occur in the same order. For example: string “Geeks” and string “Geks” contain the same characters in same order.
Examples:
Input: s1 = “Geeks”, s2 = “Geks”
Output: YesInput: s1 = “Arnab”, s2 = “Andrew”
Output: No
Approach: We have two strings now we have to check whether the strings contain the same characters in the same order. So we will replace the contiguous similar element with a single element i.e. if we have “eee”, we will replace it with a single “e”. Now we will check that both the strings are equal or not. If equal then print Yes else No.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <iostream> using namespace std; string getString(char x) { // 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. string s(1, x); return s; } // Function that returns true if // the given strings contain same // characters in same order bool solve(string s1, string s2) { // Get the first character of both strings string a = getString(s1[0]), b = getString(s2[0]); // Now if there are adjacent similar character // remove that character from s1 for (int i = 1; i < s1.length(); i++) if (s1[i] != s1[i - 1]) { a += getString(s1[i]); } // Now if there are adjacent similar character // remove that character from s2 for (int i = 1; i < s2.length(); i++) if (s2[i] != s2[i - 1]) { b += getString(s2[i]); } // If both the strings are equal // then return true if (a == b) return true; return false; } // Driver code int main() { string s1 = "Geeks", s2 = "Geks"; if (solve(s1, s2)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java implementation of the approach class temp { static String getString(char x) { // 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. String s = String.valueOf(x); return s; } // Function that returns true if // the given Strings contain same // characters in same order static boolean solve(String s1, String s2) { // Get the first character of both Strings String a = getString(s1.charAt(0)), b = getString(s2.charAt(0)); // Now if there are adjacent similar character // remove that character from s1 for (int i = 1; i < s1.length(); i++) if (s1.charAt(i) != s1.charAt(i - 1)) { a += getString(s1.charAt(i)); } // Now if there are adjacent similar character // remove that character from s2 for (int i = 1; i < s2.length(); i++) if (s2.charAt(i) != s2.charAt(i - 1)) { b += getString(s2.charAt(i)); } // If both the Strings are equal // then return true if (a.equals(b)) return true; return false; } // Driver code public static void main(String[] args) { String s1 = "Geeks", s2 = "Geks"; if (solve(s1, s2)) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by ankush_953 |
Python3
# Python3 implementation of the approach def getString(x): # string class has a constructor # that allows us to specify the size of # string as first parameter and character # to be filled in given size as the second # parameter. return x # Function that returns true if # the given strings contain same # characters in same order def solve(s1, s2): # Get the first character of both strings a = getString(s1[0]) b = getString(s2[0]) # Now if there are adjacent similar character # remove that character from s1 for i in range(1, len(s1)): if s1[i] != s1[i - 1]: a += getString(s1[i]) # Now if there are adjacent similar character # remove that character from s2 for i in range(1, len(s2)): if s2[i] != s2[i - 1]: b += getString(s2[i]) # If both the strings are equal # then return true if a == b: return True return False # Driver code s1 = "Geeks"s2 = "Geks"if solve(s1, s2): print("Yes") else: print("No") # This code is contributed by ankush_953 |
C#
// C# implementation of the approach using System; public class temp { static String getString(char x) { // 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. String s = String.Join("",x); return s; } // Function that returns true if // the given Strings contain same // characters in same order static Boolean solve(String s1, String s2) { // Get the first character of both Strings String a = getString(s1[0]), b = getString(s2[0]); // Now if there are adjacent similar character // remove that character from s1 for (int i = 1; i < s1.Length; i++) if (s1[i] != s1[i - 1]) { a += getString(s1[i]); } // Now if there are adjacent similar character // remove that character from s2 for (int i = 1; i < s2.Length; i++) if (s2[i] != s2[i - 1]) { b += getString(s2[i]); } // If both the strings are equal // then return true if (a == b) return true; return false; } // Driver code public static void Main(String[] args) { String s1 = "Geeks", s2 = "Geks"; if (solve(s1, s2)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by Princi Singh |
Yes
Recommended Posts:
- Print common characters of two Strings in alphabetical order
- Python code to print common characters of two Strings in alphabetical order
- Check if lowercase and uppercase characters are in same order
- Check if the characters of a given string are in alphabetical order
- Check if string follows order of characters defined by a pattern or not | Set 2
- Check if string follows order of characters defined by a pattern or not | Set 3
- Check if string follows order of characters defined by a pattern or not | Set 1
- Test Case Generation | Set 2 ( Random Characters, Strings and Arrays of Random Strings)
- Print characters having even frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence
- Print characters having odd frequencies in order of occurrence
- Meta Strings (Check if two strings can become same after a swap in one string)
- Print all distinct characters of a string in order (3 Methods)
- Print characters and their frequencies in order of occurrence using a LinkedHashMap in Java
- Given a sorted dictionary of an alien language, find order of characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



