Skip to content

Commit d2cd015

Browse files
committed
leetcode 859
1 parent 118fff7 commit d2cd015

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

LeetCode/859.Buddy Strings.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
typedef long long ll ;
6+
const ll INF=1e18;
7+
const ll mod1=1e9+7;
8+
const ll mod2=998244353;
9+
//Add main code here
10+
11+
class Solution
12+
{
13+
public:
14+
bool buddyStrings(string A, string B)
15+
{
16+
if (A.size() != B.size())
17+
return false;
18+
19+
if (A == B)
20+
{
21+
unordered_set<char> uniqueChars(A.begin(), A.end());
22+
return uniqueChars.size() < A.size();
23+
}
24+
25+
vector<int> diffIndices;
26+
27+
for (int i = 0; i < A.size(); i++)
28+
{
29+
if (A[i] != B[i])
30+
diffIndices.push_back(i);
31+
}
32+
33+
if (diffIndices.size() != 2)
34+
return false;
35+
36+
return A[diffIndices[0]] == B[diffIndices[1]] && A[diffIndices[1]] == B[diffIndices[0]];
37+
}
38+
};

0 commit comments

Comments
 (0)