|
52 | 52 | <li><code>a</code> and <code>b</code> consist of lowercase letters.</li>
|
53 | 53 | </ul>
|
54 | 54 |
|
55 |
| - |
56 | 55 | ## Solutions
|
57 | 56 |
|
58 | 57 | <!-- tabs:start -->
|
59 | 58 |
|
60 | 59 | ### **Python3**
|
61 | 60 |
|
62 | 61 | ```python
|
63 |
| - |
| 62 | +class Solution: |
| 63 | + def buddyStrings(self, s: str, goal: str) -> bool: |
| 64 | + m, n = len(s), len(goal) |
| 65 | + if m != n: |
| 66 | + return False |
| 67 | + diff = sum(1 for i in range(n) if s[i] != goal[i]) |
| 68 | + cnt1, cnt2 = Counter(s), Counter(goal) |
| 69 | + if cnt1 != cnt2: |
| 70 | + return False |
| 71 | + return diff == 2 or (diff == 0 and any(e > 1 for e in cnt1.values())) |
64 | 72 | ```
|
65 | 73 |
|
66 | 74 | ### **Java**
|
67 | 75 |
|
68 | 76 | ```java
|
| 77 | +class Solution { |
| 78 | + public boolean buddyStrings(String s, String goal) { |
| 79 | + int m = s.length(), n = goal.length(); |
| 80 | + if (m != n) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + int diff = 0; |
| 84 | + int[] cnt1 = new int[26]; |
| 85 | + int[] cnt2 = new int[26]; |
| 86 | + for (int i = 0; i < n; ++i) { |
| 87 | + int a = s.charAt(i), b = goal.charAt(i); |
| 88 | + ++cnt1[a - 'a']; |
| 89 | + ++cnt2[b - 'a']; |
| 90 | + if (a != b) { |
| 91 | + ++diff; |
| 92 | + } |
| 93 | + } |
| 94 | + boolean f = false; |
| 95 | + for (int i = 0; i < 26; ++i) { |
| 96 | + if (cnt1[i] != cnt2[i]) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + if (cnt1[i] > 1) { |
| 100 | + f = true; |
| 101 | + } |
| 102 | + } |
| 103 | + return diff == 2 || (diff == 0 && f); |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### **C++** |
| 109 | + |
| 110 | +```cpp |
| 111 | +class Solution { |
| 112 | +public: |
| 113 | + bool buddyStrings(string s, string goal) { |
| 114 | + int m = s.size(), n = goal.size(); |
| 115 | + if (m != n) return false; |
| 116 | + int diff = 0; |
| 117 | + vector<int> cnt1(26); |
| 118 | + vector<int> cnt2(26); |
| 119 | + for (int i = 0; i < n; ++i) |
| 120 | + { |
| 121 | + ++cnt1[s[i] - 'a']; |
| 122 | + ++cnt2[goal[i] - 'a']; |
| 123 | + if (s[i] != goal[i]) ++diff; |
| 124 | + } |
| 125 | + bool f = false; |
| 126 | + for (int i = 0; i < 26; ++i) |
| 127 | + { |
| 128 | + if (cnt1[i] != cnt2[i]) return false; |
| 129 | + if (cnt1[i] > 1) f = true; |
| 130 | + } |
| 131 | + return diff == 2 || (diff == 0 && f); |
| 132 | + } |
| 133 | +}; |
| 134 | +``` |
69 | 135 |
|
| 136 | +### **Go** |
| 137 | +
|
| 138 | +```go |
| 139 | +func buddyStrings(s string, goal string) bool { |
| 140 | + m, n := len(s), len(goal) |
| 141 | + if m != n { |
| 142 | + return false |
| 143 | + } |
| 144 | + diff := 0 |
| 145 | + cnt1 := make([]int, 26) |
| 146 | + cnt2 := make([]int, 26) |
| 147 | + for i := 0; i < n; i++ { |
| 148 | + cnt1[s[i]-'a']++ |
| 149 | + cnt2[goal[i]-'a']++ |
| 150 | + if s[i] != goal[i] { |
| 151 | + diff++ |
| 152 | + } |
| 153 | + } |
| 154 | + f := false |
| 155 | + for i := 0; i < 26; i++ { |
| 156 | + if cnt1[i] != cnt2[i] { |
| 157 | + return false |
| 158 | + } |
| 159 | + if cnt1[i] > 1 { |
| 160 | + f = true |
| 161 | + } |
| 162 | + } |
| 163 | + return diff == 2 || (diff == 0 && f) |
| 164 | +} |
70 | 165 | ```
|
71 | 166 |
|
72 | 167 | ### **...**
|
|
0 commit comments