-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat: valid_anagram.cpp #3031
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
Open
sgindeed
wants to merge
1
commit into
TheAlgorithms:master
Choose a base branch
from
sgindeed:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+123
−0
Open
feat: valid_anagram.cpp #3031
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/** | ||
* @file | ||
* @brief Implementation of [Anagram | ||
* Checker](https://en.wikipedia.org/wiki/Anagram) using a hash table. | ||
* @details | ||
* An anagram is a word or phrase formed by rearranging the letters of a | ||
* different word or phrase, typically using all the original letters exactly | ||
* once. This implementation is case-sensitive and considers spaces and | ||
* punctuation as characters. | ||
* | ||
* ### Algorithm | ||
* To check if two strings are anagrams, we first perform a quick check on their | ||
* lengths. If the lengths are different, they cannot be anagrams. | ||
* | ||
* We then use a hash table (std::unordered_map) to store the character counts | ||
* of the first string. We iterate through the first string, mapping each | ||
* character to its frequency. | ||
* | ||
* Next, we iterate through the second string. For each character, we decrement | ||
* its count in the hash table. If a character's count drops below zero, or if | ||
* the character is not found in the map, the strings are not anagrams. | ||
* | ||
* If we successfully iterate through the second string without any issues, the | ||
* two strings are valid anagrams. | ||
*/ | ||
|
||
#include <cassert> /// for assert | ||
#include <iostream> /// for I/O operations | ||
#include <string> /// for std::string | ||
#include <unordered_map> /// for std::unordered_map (hash table) | ||
|
||
/** | ||
* @namespace string_algorithms | ||
* @brief String manipulation algorithms | ||
*/ | ||
namespace string_algorithms { | ||
/** | ||
* @namespace anagram_checker | ||
* @brief Functions for checking anagrams | ||
*/ | ||
namespace anagram_checker { | ||
|
||
/** | ||
* @brief Checks if two strings are valid anagrams of each other. | ||
* @param[in] s1 first string to compare. | ||
* @param[in] s2 second string to compare. | ||
* @return `true` if the strings are anagrams. | ||
* @return `false` if the strings are not anagrams. | ||
*/ | ||
bool are_anagrams(const std::string& s1, const std::string& s2) { | ||
// Anagrams must have the same length. | ||
if (s1.length() != s2.length()) { | ||
return false; | ||
} | ||
|
||
// Use a hash map to store character frequencies. | ||
std::unordered_map<char, int> counts; | ||
|
||
// Count characters in the first string. | ||
for (char c : s1) { | ||
counts[c]++; | ||
} | ||
|
||
// Decrement counts using characters from the second string. | ||
for (char c : s2) { | ||
// If a char is not found or its count is already zero, they're not | ||
// anagrams. | ||
if (counts.find(c) == counts.end() || counts[c] == 0) { | ||
return false; | ||
} | ||
counts[c]--; | ||
} | ||
|
||
// If we get here, the strings are anagrams. | ||
return true; | ||
} | ||
|
||
} // namespace anagram_checker | ||
} // namespace string_algorithms | ||
|
||
/** | ||
* @brief Self-test implementations for the anagram checker. | ||
* @returns void | ||
*/ | ||
static void test() { | ||
// Test case 1: Basic valid anagrams | ||
assert(string_algorithms::anagram_checker::are_anagrams("listen", "silent")); | ||
assert(string_algorithms::anagram_checker::are_anagrams("triangle", | ||
"integral")); | ||
|
||
// Test case 2: Valid anagrams with spaces | ||
assert(string_algorithms::anagram_checker::are_anagrams("a gentleman", | ||
"elegant man")); | ||
|
||
// Test case 3: Invalid anagrams (different characters) | ||
assert(!string_algorithms::anagram_checker::are_anagrams("hello", "world")); | ||
|
||
// Test case 4: Invalid anagrams (different lengths) | ||
assert(!string_algorithms::anagram_checker::are_anagrams("apple", "apples")); | ||
|
||
// Test case 5: Invalid anagrams (different character counts) | ||
assert(!string_algorithms::anagram_checker::are_anagrams("aabbc", "aaabc")); | ||
|
||
// Test case 6: Case-sensitive check (should be false) | ||
assert(!string_algorithms::anagram_checker::are_anagrams("Listen", "silent")); | ||
|
||
// Test case 7: Empty strings (should be true) | ||
assert(string_algorithms::anagram_checker::are_anagrams("", "")); | ||
|
||
// Test case 8: Strings with numbers and symbols | ||
assert(string_algorithms::anagram_checker::are_anagrams("123!!", "!1!23")); | ||
|
||
std::cout << "All tests have successfully passed!" << std::endl; | ||
} | ||
|
||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
int main() { | ||
test(); // run self-test implementations | ||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't think character frequency could be negative, use an unsigned integer here, fixed width.