-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy path859.buddy-strings.kt
113 lines (108 loc) · 1.92 KB
/
859.buddy-strings.kt
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* @lc app=leetcode id=859 lang=kotlin
*
* [859] Buddy Strings
*
* https://leetcode.com/problems/buddy-strings/description/
*
* algorithms
* Easy (27.80%)
* Likes: 362
* Dislikes: 217
* Total Accepted: 30.8K
* Total Submissions: 110.5K
* Testcase Example: '"ab"\n"ba"'
*
* 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
*
*
*
*
* Note:
*
*
* 0 <= A.length <= 20000
* 0 <= B.length <= 20000
* A and B consist only of lowercase letters.
*
*
*
*
*
*
*
*/
// @lc code=start
class Solution {
private fun judge(A: String, B: String): Boolean{
if ((A == B && A[0] == A[1]) || (A != B && A[0] == B[1] && A[1] == B[0]))
return true
return false
}
fun buddyStrings(A: String, B: String): Boolean {
if (A.length != B.length || A.length < 2 || B.length < 2) return false
var diff = 0
var st1 = ""
var st2 = ""
for (i in 0 until A.length) {
if (A[i] != B[i]) {
diff++
st1 += A[i]
st2 += B[i]
}
}
if (diff == 1 || diff > 2) return false
else if (diff == 2) return judge(st1, st2)
else {
var nums = IntArray(30)
for (i in 0 until A.length) {
nums[A[i]-'a']++
if (nums[A[i]-'a'] >= 2) return true
}
return false
}
}
}
// @lc code=end