-
-
Notifications
You must be signed in to change notification settings - Fork 100
242. Valid Anagram.cpp #204
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
Reviewer's guide (collapsed on small PRs)Reviewer's GuideThis 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)
}
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this 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
|
Hey, @LS1212-dec Raise an ISSUE as well... |
|
Hi, I’ve renamed the PR as per the guidelines. |
🎉 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! 🥳✨ |
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++)
Related Issues
By submitting this PR, I confirm that:
Summary by Sourcery
Add C++ solution for LeetCode problem 242: Valid Anagram using a fixed-size frequency array to compare character counts
New Features: