Code Logo

Compare with equalsIgnoreCase()

Published at25 Jul 2026
Java OOP Easy 0 views
Like0

Write a Java function that takes two strings and returns true if they are equal ignoring case using equalsIgnoreCase(). This method compares strings lexicographically without considering case differences.

String.equalsIgnoreCase() is a method that compares two strings for equality, ignoring case considerations. It returns true if the strings have the same length and corresponding characters are equal ignoring case. The method is case-insensitive for both uppercase and lowercase letters in the Latin alphabet.

Unlike equals() which compares strings with case sensitivity (e.g., "Hello" != "hello"), equalsIgnoreCase() treats uppercase and lowercase as equivalent. This is useful for user input validation, command parsing, and any scenario where case-insensitive comparison is needed.

Time complexity is O(n) where n is the string length. Space complexity is O(1). The method does not create new strings; it compares characters directly using Character.toLowerCase() and Character.toUpperCase() for each character pair.

Edge cases include empty strings (equal), null strings (throws NullPointerException if called on null, returns false if passed as argument), strings with different lengths (returns false without character comparison), and strings with Unicode characters beyond ASCII.

Example Input & Output

Example 1
Input
"Java","java"
Output
true
Explanation

Case-insensitive equal

Example 2
Input
"",""
Output
true
Explanation

Both empty

Example 3
Input
"Hello","hello"
Output
true
Explanation

Different case but equal

Example 4
Input
"test","Test"
Output
true
Explanation

Single char different case

Example 5
Input
"abc","xyz"
Output
false
Explanation

Different strings

Algorithm Flow

Recommendation Algorithm Flow for Compare with equalsIgnoreCase()
Recommendation Algorithm Flow for Compare with equalsIgnoreCase()

Solution Approach

class Solution {
    public boolean solution(String a, String b) {
        return a.equalsIgnoreCase(b);
    }
}

Best Answers

java - Approach 1
class Solution {
    public boolean solution(String a, String b) {
        return a.equalsIgnoreCase(b);
    }
}