diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /determine_strings_close.cpp | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'determine_strings_close.cpp')
-rw-r--r-- | determine_strings_close.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/determine_strings_close.cpp b/determine_strings_close.cpp new file mode 100644 index 0000000..89e2322 --- /dev/null +++ b/determine_strings_close.cpp @@ -0,0 +1,28 @@ +#include "leetcode.h" + +class Solution { +public: + bool closeStrings(string word1, string word2) { + if (word1.size() != word2.size()) + return false; + vector<int> c1(26), c2(26); + for (auto i = 0; i < word1.size(); ++i) { + ++c1[word1[i] - 'a']; + ++c2[word2[i] - 'a']; + } + if (!equal(begin(c1), end(c1), begin(c2), end(c2), + [](int a, int b) { return (bool)a == bool(b); })) + return false; + sort(begin(c1), end(c1)); + sort(begin(c2), end(c2)); + return c1 == c2; + } +}; + +int main() { + Solution obj; + if (obj.closeStrings("abc", "bca")) + cout << "true"; + else + cout << "false"; +} |