From 4689e4e80b479be25f7557d05818f5caa723aafa Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Wed, 5 Feb 2025 18:36:31 -0800 Subject: Initial commit. --- .../easy/strings/04_valid_anagram.cc | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 top-interview-questions/easy/strings/04_valid_anagram.cc (limited to 'top-interview-questions/easy/strings/04_valid_anagram.cc') diff --git a/top-interview-questions/easy/strings/04_valid_anagram.cc b/top-interview-questions/easy/strings/04_valid_anagram.cc new file mode 100644 index 0000000..3ca6b7c --- /dev/null +++ b/top-interview-questions/easy/strings/04_valid_anagram.cc @@ -0,0 +1,29 @@ +class Solution { +public: + static void count(const string& s, int* counts) { + for (char c : s) { + counts[c]++; + } + } + + bool isAnagram(string s, string t) { + if (s.size() != t.size()) { + return false; + } + // strings are equal length + + int sCount[256] = {}; + int tCount[256] = {}; + + count(s, sCount); + count(t, tCount); + + for (char c : s) { + if (sCount[c] != tCount[c]) { + return false; + } + } + + return true; + } +}; -- cgit v1.2.3