Skip to content

Added Anagram Checker in Java #6281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

farheen-shaikh530
Copy link


✅ Features

  • Ignores case ("Listen" and "Silent" are anagrams)
  • Ignores whitespace ("Clint Eastwood" and "Old West Action")
  • Uses HashMap for character frequency count
  • Efficient: Time Complexity: O(n) | Space Complexity: O(n)
  • Demonstrates mid-level Java skills:
    • String manipulation
    • HashMap usage
    • Input sanitization
    • Edge case handling

💡 How It Works

  1. Converts both strings to lowercase and removes all spaces.
  2. Uses a HashMap<Character, Integer> to count the frequency of each character in the first string.
  3. Iterates over the second string and decrements the character counts.
  4. If all frequencies return to zero and the map is empty, the strings are anagrams.

🧪 Example

public static void main(String[] args) {
    String s1 = "Listen";
    String s2 = "Silent";

    if (areAnagrams(s1, s2)) {
        System.out.println("\"" + s1 + "\" and \"" + s2 + "\" are anagrams.");
    } else {
        System.out.println("\"" + s1 + "\" and \"" + s2 + "\" are NOT anagrams.");
    }
}

@codecov-commenter
Copy link

codecov-commenter commented Jun 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.07%. Comparing base (ed4a724) to head (f58d61a).

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #6281   +/-   ##
=========================================
  Coverage     74.07%   74.07%           
  Complexity     5351     5351           
=========================================
  Files           677      677           
  Lines         18648    18648           
  Branches       3621     3621           
=========================================
  Hits          13813    13813           
  Misses         4276     4276           
  Partials        559      559           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vil02
Copy link
Member

vil02 commented Jun 10, 2025

All of these are already implemented. Additionally please place your code in src/main/java/com/thealgorithms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants