-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuddy-strings.h
36 lines (30 loc) · 936 Bytes
/
buddy-strings.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef BUDDY_STRINGS_H_
#define BUDDY_STRINGS_H_
#include <algorithm>
#include <string>
namespace solution {
bool buddyStrings(std::string s, std::string goal) {
// direct
// Runtime: 0 ms, faster than 100.00% of C++ online submissions for Buddy Strings.
// Memory Usage: 7.2 MB, less than 8.46% of C++ online submissions for Buddy Strings.
//
if (s.size() != goal.size() || s.size() == 1) return false;
if (s == goal)
return std::any_of(s.cbegin(), s.cend(), [s](const auto& c) {
return std::count(s.cbegin(), s.cend(), c) >= 2;
});
auto count{0};
auto good{false};
char c1{}, c2{};
for (size_t i = 0; i < s.size(); ++i) {
if (s[i] != goal[i]) {
if (count == 2) return false;
if (count == 1 && c1 == goal[i] && c2 == s[i]) good = true;
if (count == 0) c1 = s[i], c2 = goal[i];
++count;
}
}
return good;
}
} // namespace solution
#endif // BUDDY_STRINGS_H_