-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuddy_string.cpp
More file actions
78 lines (64 loc) · 1.59 KB
/
buddy_string.cpp
File metadata and controls
78 lines (64 loc) · 1.59 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// Created by Mayank Parasar on 2019-12-26.
//
/*
* Given two strings A and B of lowercase letters, return true
* if and only if we can swap two letters in A so that the result equals B.
Example 1:
Input: A = "ab", B = "ba"
Output: true
Example 2:
Input: A = "ab", B = "ab"
Output: false
Example 3:
Input: A = "aa", B = "aa"
Output: true
Example 4:
Input: A = "aaaaaaabc", B = "aaaaaaacb"
Output: true
Example 5:
Input: A = "", B = "aa"
Output: false
*/
#include <iostream>
#include <vector>
#include <string>
#include <assert.h>
using namespace std;
string& swap_char(string& str, int indx1, int indx2) {
char tmp_char;
tmp_char = str[indx1];
str[indx1] = str[indx2];
str[indx2] = tmp_char;
return (str);
}
bool buddy_string(string& str1, string& str2) {
if(str1.length() != str2.length())
return false;
// string tmp;
for(int ii=0; ii < str1.size(); ++ii) {
for (int kk =ii+1; kk < str1.size(); ++kk) {
// swap character str1[ii] with str1[jj] and
// compare with string str2,
if (swap_char(str1, ii, kk) == str2)
return true;
else {
// swap back
swap_char(str1, kk, ii);
}
}
}
// bool result = false;
return (false);
}
int main() {
// string str1 = "aaaaaaabc";
// string str2 = "aaaaaaacb";
// string str1 = "ab";
// string str2 = "ab";
string str1 = "aa";
string str2 = "aa";
// cout << "length of str1: " << str1.length() << endl;
cout << (buddy_string(str1, str2) ? "True" : "False");
return 0;
}