We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 118fff7 commit d2cd015Copy full SHA for d2cd015
LeetCode/859.Buddy Strings.cpp
@@ -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
35
36
+ return A[diffIndices[0]] == B[diffIndices[1]] && A[diffIndices[1]] == B[diffIndices[0]];
37
38
+};
0 commit comments