889162. Check if two strings are same or not

Easy

Here is the detailed Markdown blog post for the problem "Check if two strings are same or not":

Check if two strings are same or not

Summary

This problem involves checking whether two given strings are identical or not. The key concept involved here is string comparison, which requires comparing each character of both strings and determining if they match.

Detailed Explanation

To solve this problem, we can use a simple algorithm that compares each character of the two strings. Here's a step-by-step breakdown:

  1. Initialize a variable same to true.
  2. Loop through each character of both strings.
  3. Compare each pair of characters. If they don't match, set same to false and break out of the loop.
  4. After looping through all characters, if same is still true, return that the two strings are identical.

Here's a simple ASCII art diagram to illustrate this:

String 1: "hello"
String 2: "hello"

 Loop through each character:
 h -> h (match)
 e -> e (match)
 l -> l (match)
 l -> l (match)
 o -> o (match)

 same = true

Time complexity for this algorithm is O(n), where n is the length of the strings, as we need to compare each character once. Space complexity is O(1), as we only require a single variable to store the result.

Optimized Solutions

Java

View on GeeksforGeeks
public boolean areStringsSame(String s1, String s2) {
    if (s1.length() != s2.length()) {
        return false;
    }
    for (int i = 0; i < s1.length(); i++) {
        if (s1.charAt(i) != s2.charAt(i)) {
            return false;
        }
    }
    return true;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.