From 87a243ad3b121f663c33e27fa02b6793445392bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:25:20 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A2=20Port=20problem=20242=20(Valid=20?= =?UTF-8?q?Anagram)=20from=20Swift=20to=20C++23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces swift/242.swift with solutions/0201-0300/0242-valid-anagram/, keeping the general hash-map approach and adding a fixed 26-slot count-array optimized version (valid given the problem's lowercase English letters constraint). --- .../0201-0300/0242-valid-anagram/README.md | 11 +++++ .../0201-0300/0242-valid-anagram/solution.cpp | 44 +++++++++++++++++++ .../0201-0300/0242-valid-anagram/solution.hpp | 15 +++++++ .../0201-0300/0242-valid-anagram/test.cpp | 13 ++++++ swift/242.swift | 20 --------- 5 files changed, 83 insertions(+), 20 deletions(-) create mode 100644 solutions/0201-0300/0242-valid-anagram/README.md create mode 100644 solutions/0201-0300/0242-valid-anagram/solution.cpp create mode 100644 solutions/0201-0300/0242-valid-anagram/solution.hpp create mode 100644 solutions/0201-0300/0242-valid-anagram/test.cpp delete mode 100644 swift/242.swift 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 - } -}