leetcode

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

commit 9c57818fcb6211316f3e8e5a39096ff703fd6a6e
parent e41c31d07c5a3d23d84cf829b66a22d46d073f13
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Mon,  3 Jul 2023 14:41:06 +0200

Daily Problem

Diffstat:
AProblems/0859.cpp | 22++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/Problems/0859.cpp b/Problems/0859.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + bool buddyStrings(const string &s, const string &goal) { + int a = -1, b = -1, dup = 0, count[26] = {0}; + if (s.size() != goal.size()) return false; + for (int i = 0; i < s.size(); i++) { + if (count[s[i] & 0xF]) dup = 1; + count[s[i] & 0xF] = 1; + if (s[i] != goal[i]) { + if (a == -1) + a = i; + else if (b == -1) + b = i; + else + return false; + } + } + if (a == -1) return dup; + if (b == -1) return false; + return s[a] == goal[b] && s[b] == goal[a]; + } +}; diff --git a/README.md b/README.md @@ -360,6 +360,7 @@ for solving problems. | 0844 | Easy | [Backspace String Compare](Problems/0844.cpp) | | 0851 | Medium | [Loud and Rich](Problems/0851.cpp) | | 0853 | Medium | [Car Fleet](Problems/0853.cpp) | +| 0859 | Easy | [Buddy Strings](Problems/0859.cpp) | | 0864 | Hard | [Shortest Path to Get All Keys](Problems/0864.cpp) | | 0872 | Easy | [Leaf-Similar Trees](Problems/0872.cpp) | | 0875 | Medium | [Koko Eating Bananas](Problems/0875.cpp) |