leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 8541556871fc3e0428f02f3e6e784bf020cf2d1d
parent a2abe19f37631d797f036ebb850fe256cc4f8f11
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed,  5 Jun 2024 12:16:22 +0200

Daily Problem

Diffstat:
AProblems/1002.cpp | 24++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/Problems/1002.cpp b/Problems/1002.cpp @@ -0,0 +1,24 @@ +class Solution { + public: + vector<string> commonChars(const vector<string> &words) const { + static unsigned count[26]; + vector<string> res; + + memset(count, 0xFF, sizeof(count)); + for (const auto &word : words) { + unsigned lcount[26] = {0}; + for (const char c : word) + lcount[c - 'a']++; + for (int i = 0; i < 26; i++) + count[i] = min(count[i], lcount[i]); + } + + for (int i = 0; i < 26; i++) { + const string c(1, 'a' + i); + for (int j = 0; j < count[i]; j++) + res.push_back(c); + } + + return res; + } +}; diff --git a/README.md b/README.md @@ -606,6 +606,7 @@ for solving problems. | 0994 | Medium | [Rotting Oranges](Problems/0994.cpp) | | 0997 | Easy | [Find the Town Judge](Problems/0997.cpp) | | 0998 | Medium | [Maximum Binary Tree II](Problems/0998.cpp) | +| 1002 | Easy | [Find Common Characters](Problems/1002.cpp) | | 1003 | Medium | [Check If Word Is Valid After Substitutions](Problems/1003.cpp) | | 1004 | Medium | [Max Consecutive Ones III](Problems/1004.cpp) | | 1006 | Medium | [Clumsy Factorial](Problems/1006.cpp) |