Input: s1 = “geeksforgeeks”, s2 = “geeks”
Output: Uequal Strings
Input: s1 = “geeksforgeeks”, s2 = “geeksforgeeks”
Output: Equal Strings
Approach: There are three possible cases occur when we compare two strings:
- Both the strings are the same means difference of ASCII value between both the strings is 0.
- Both the strings are different means ASCII value of first not matching character in the first string is less than the second string then the difference between both the strings is (<0).
- Both the strings are different means ASCII value of first not matching character in the first string is greater than the second string then the difference between both the strings is (>0).
Based on the above three conditions, the idea is to compare each character of the given strings one by one whenever condition 2 or 3 occurs then print “Unequal strings” else print “Equal strings”.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- C++ program to compare two Strings using Operator Overloading
- Write your own strcmp that ignores cases
- Compare two strings lexicographically in Java
- Compare two strings considering only alphanumeric characters
- C++ Program to compare two string using pointers
- Program to compare two fractions
- C Program to concatenate two strings without using strcat
- How to Initialize and Compare Strings in Java?
- How to compare two arrays in Java?
- Compare two Version numbers
- Program to compare m^n and n^m
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- C/C++ Program to Count number of binary strings without consecutive 1's
- Compare Version Numbers with large inputs allowed
- Compare sum of first N-1 elements to Nth element of an array
- Compare numbers represented by Linked Lists
- Pairs of complete strings in two sets of strings
- Meta Strings (Check if two strings can become same after a swap in one string)
- Number of common base strings for two strings
- Count of strings that become equal to one of the two strings after one removal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.