VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-check-if-two-strings-are-same-or-not/

⇱ Check if two strings are same - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if two strings are same

Last Updated : 28 Mar, 2026

Given two strings, check if these two strings are identical(same) or not. Consider case sensitivity.

Examples:

Input: s1 = "abc", s2 = "abc" 
Output: Yes 

Input: s1 = "", s2 = "" 
Output: Yes 

Input: s1 = "GeeksforGeeks", s2 = "Geeks" 
Output: No 

Using (==) in C++/Python/C#, equals in Java and === in JavaScript - O(n) Time and O(1) Space

The idea is to use equality operator (==) to compare both strings character by character. If the strings are identical, it prints "Yes", otherwise, it prints "No".


Output
No

Using String Comparison Functions - O(n) Time and O(1) Space

The idea is to the direct string comparison function, which compares the strings character by character. If the strings are exactly the same, it returns 0.. If the strings are different,, returns a non-zero value. Based on this result, the program prints "Yes" if the strings match and "No" if they don't. The comparison stops as soon as a mismatch is found or the strings end.


Output
Yes

Writing your Own Method - O(n) Time and O(1) Space

  • If their lengths are the same. If the lengths differ, the strings cannot be identical, so it returns false.
  • Then compares each character of the two strings one by one. If any mismatch is found, it returns false.
  • If no differences are found throughout the comparison, it returns true.

Output
Yes
Comment
Article Tags: