diff --git a/solutions/0201-0300/0242-valid-anagram/README.md b/solutions/0201-0300/0242-valid-anagram/README.md new file mode 100644 index 0000000..af07328 --- /dev/null +++ b/solutions/0201-0300/0242-valid-anagram/README.md @@ -0,0 +1,11 @@ +# 242. Valid Anagram + +[LeetCode problem 242](https://leetcode.com/problems/valid-anagram/) + +- **Difficulty**: Easy +- **Tags**: hash-table, string, sorting + +## Description + +Given two strings `s` and `t`, return `true` if `t` is an anagram of `s` +(uses the exact same letters, same counts), `false` otherwise. diff --git a/solutions/0201-0300/0242-valid-anagram/solution.cpp b/solutions/0201-0300/0242-valid-anagram/solution.cpp new file mode 100644 index 0000000..0ca4c8b --- /dev/null +++ b/solutions/0201-0300/0242-valid-anagram/solution.cpp @@ -0,0 +1,44 @@ +#include "solution.hpp" + +#include +#include +#include + +namespace leetcode::p0242 { + +bool solve(const std::string& s, const std::string& t) { + if (s.size() != t.size()) { + return false; + } + + std::array counts{}; + + for (char c : s) { + ++counts.at(static_cast(c - 'a')); + } + for (char c : t) { + --counts.at(static_cast(c - 'a')); + } + + return std::ranges::all_of(counts, [](int count) { return count == 0; }); +} + +bool solveHashMap(const std::string& s, const std::string& t) { + if (s.size() != t.size()) { + return false; + } + + std::unordered_map countS; + std::unordered_map countT; + + for (char c : s) { + ++countS[c]; + } + for (char c : t) { + ++countT[c]; + } + + return countS == countT; +} + +} // namespace leetcode::p0242 diff --git a/solutions/0201-0300/0242-valid-anagram/solution.hpp b/solutions/0201-0300/0242-valid-anagram/solution.hpp new file mode 100644 index 0000000..13a1894 --- /dev/null +++ b/solutions/0201-0300/0242-valid-anagram/solution.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +namespace leetcode::p0242 { + +// optimized: O(n) time, O(1) space — fixed 26-slot count array (assumes +// lowercase English letters, per the problem's constraints). +bool solve(const std::string& s, const std::string& t); + +// hash-map: O(n) time, O(k) space (k = distinct characters) — works for any +// character set, not just lowercase English letters. +bool solveHashMap(const std::string& s, const std::string& t); + +} // namespace leetcode::p0242 diff --git a/solutions/0201-0300/0242-valid-anagram/test.cpp b/solutions/0201-0300/0242-valid-anagram/test.cpp new file mode 100644 index 0000000..fbaf4ea --- /dev/null +++ b/solutions/0201-0300/0242-valid-anagram/test.cpp @@ -0,0 +1,13 @@ +#include + +#include "solution.hpp" + +TEST_CASE("Problem 242 - optimized", "[p0242]") { + CHECK(leetcode::p0242::solve("anagram", "nagaram")); + CHECK_FALSE(leetcode::p0242::solve("rat", "car")); +} + +TEST_CASE("Problem 242 - hash map", "[p0242]") { + CHECK(leetcode::p0242::solveHashMap("anagram", "nagaram")); + CHECK_FALSE(leetcode::p0242::solveHashMap("rat", "car")); +} diff --git a/swift/242.swift b/swift/242.swift deleted file mode 100644 index c074259..0000000 --- a/swift/242.swift +++ /dev/null @@ -1,20 +0,0 @@ -class Solution { - func isAnagram(_ s: String, _ t: String) -> Bool { - if s.count != t.count { - return false - } - - var countS: [Character: Int] = [:] - var countT: [Character: Int] = [:] - - for char in s { - countS[char, default: 0] += 1 - } - - for char in t { - countT[char, default: 0] += 1 - } - - return countS == countT - } -}