Skip to content

Conversation

@LS1212-dec
Copy link

@LS1212-dec LS1212-dec commented Oct 22, 2025

PR Title Format: 242.Valid Anagram.cpp

Intuition

The problem asks to check if two strings s and t are anagrams, i.e., they contain the same characters in the same frequency.
My initial thought is to count the frequency of each character in both strings and compare them.

Approach

Check lengths: If s and t have different lengths, return false.

Frequency array:

Create an array of size 26 (for lowercase letters).

Increment for characters in s, decrement for characters in t.

Validation: If all elements in the frequency array are zero, the strings are anagrams; otherwise, they are not.

Alternative approach: Sort both strings and compare them. This works too but is slightly slower (O(n log n)).

Code Solution (C++)

class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.length() != t.length()) return false;
        
        int count[26] = {0};
        
        for(int i = 0; i < s.length(); i++) {
            count[s[i]-'a']++;
            count[t[i]-'a']--;
        }
        
        for(int i = 0; i < 26; i++) {
            if(count[i] != 0) return false;
        }
        
        return true;
    }
};

Related Issues

By submitting this PR, I confirm that:

  • [✅] This is my original work not totally AI generated
  • [✅] I have tested the solution thoroughly on leetcode
  • [✅] I have maintained proper PR description format
  • [✅] This is a meaningful contribution, not spam

Summary by Sourcery

Add C++ solution for LeetCode problem 242: Valid Anagram using a fixed-size frequency array to compare character counts

New Features:

  • Implement isAnagram method in Solution class with length check and single-pass frequency increment/decrement
  • Validate anagram by ensuring all count entries return to zero

@sourcery-ai
Copy link

sourcery-ai bot commented Oct 22, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR introduces a new C++ solution for the 'Valid Anagram' problem by leveraging a fixed-size frequency array to achieve an O(n) time complexity. The solution first checks string lengths, then updates character counts in a single pass, and finally verifies that all counts return to zero.

Class diagram for the new Solution class (Valid Anagram)

classDiagram
class Solution {
  +bool isAnagram(string s, string t)
}
Loading

File-Level Changes

Change Details Files
Implemented the isAnagram method using a frequency array approach
  • Added early length mismatch check
  • Declared a 26-element count array initialized to zero
  • Incremented count for characters in s and decremented for characters in t within a single loop
  • Added a verification loop to ensure all frequency counts are zero
242. Valid Anagram.cpp

Possibly linked issues

  • 52. N-Queens II #242: The PR adds the C++ solution for LeetCode 242 Valid Anagram, which is the problem detailed in the issue description.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for raising the PR, the owner will be review it soon' keep patience, keep contributing>>>!!! make sure you have star ⭐ the repo

@SjxSubham
Copy link
Owner

Hey, @LS1212-dec

Raise an ISSUE as well...
Rename the PR name/Title as problem No. Problem name. cpp

@LS1212-dec LS1212-dec changed the title Added Leetcode 242 242. ValidAnagram.cpp Oct 22, 2025
@LS1212-dec LS1212-dec changed the title 242. ValidAnagram.cpp 242. Valid Anagram.cpp Oct 22, 2025
@LS1212-dec
Copy link
Author

Hi, I’ve renamed the PR as per the guidelines.
Please review and merge when possible.
Thank you!

@SjxSubham SjxSubham linked an issue Oct 22, 2025 that may be closed by this pull request
4 tasks
@SjxSubham SjxSubham added the hacktoberest-accepted hacktoberfest-accepted label Oct 22, 2025
@SjxSubham SjxSubham merged commit 0d21b0c into SjxSubham:main Oct 22, 2025
2 checks passed
@github-actions
Copy link

🎉 Congrats on getting your PR merged in, @LS1212-dec! 🙌🏼

Thanks for your contribution every effort helps improve the project.

Looking forward to seeing more from you! 🥳✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberest-accepted hacktoberfest-accepted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

242 : Valid Anagram

2 participants