Skip to content

Commit 352adb0

Browse files
committed
leetcode
1 parent 474d712 commit 352adb0

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

BuddyStrings/buddy_strings.dart

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
3+
4+
-* 859. Buddy Strings *-
5+
6+
7+
8+
Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false.
9+
10+
Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j].
11+
12+
For example, swapping at indices 0 and 2 in "abcd" results in "cbad".
13+
14+
15+
Example 1:
16+
17+
Input: s = "ab", goal = "ba"
18+
Output: true
19+
Explanation: You can swap s[0] = 'a' and s[1] = 'b' to get "ba", which is equal to goal.
20+
Example 2:
21+
22+
Input: s = "ab", goal = "ab"
23+
Output: false
24+
Explanation: The only letters you can swap are s[0] = 'a' and s[1] = 'b', which results in "ba" != goal.
25+
Example 3:
26+
27+
Input: s = "aa", goal = "aa"
28+
Output: true
29+
Explanation: You can swap s[0] = 'a' and s[1] = 'a' to get "aa", which is equal to goal.
30+
31+
32+
Constraints:
33+
34+
1 <= s.length, goal.length <= 2 * 104
35+
s and goal consist of lowercase letters.
36+
37+
38+
39+
*/
40+
41+
import 'dart:collection';
42+
43+
class A {
44+
bool buddyStrings(String s, String goal) {
45+
if (s.length != goal.length) return false;
46+
47+
if (s == goal) {
48+
List<int> count = List.filled(26, 0);
49+
50+
for (int i = 0; i < s.length; i++) {
51+
int c = s.codeUnitAt(i) - 'a'.codeUnitAt(0);
52+
count[c]++;
53+
if (count[c] == 2) return true;
54+
}
55+
56+
return false;
57+
}
58+
59+
int index1 = -1;
60+
int index2 = -1;
61+
62+
for (int i = 0; i < s.length; i++) {
63+
if (s[i] != goal[i]) {
64+
if (index1 == -1)
65+
index1 = i;
66+
else if (index2 == -1)
67+
index2 = i;
68+
else
69+
return false;
70+
}
71+
}
72+
73+
if (index2 == -1) return false;
74+
75+
return (s[index1] == goal[index2] && s[index2] == goal[index1]);
76+
}
77+
}
78+
79+
class B {
80+
bool buddyStrings(String s, String goal) {
81+
final int n = s.length;
82+
83+
if (s == goal) {
84+
final HashSet<String> temp = HashSet<String>();
85+
for (int i = 0; i < s.length; i++) {
86+
temp.add(s[i]);
87+
}
88+
return temp.length < goal.length; // Swapping same characters
89+
}
90+
91+
int i = 0;
92+
int j = n - 1;
93+
94+
while (i < j && s[i] == goal[i]) {
95+
i++;
96+
}
97+
98+
while (j >= 0 && s[j] == goal[j]) {
99+
j--;
100+
}
101+
102+
if (i < j) {
103+
final List<String> sArr = s.split('');
104+
String temp = sArr[i];
105+
sArr[i] = sArr[j];
106+
sArr[j] = temp;
107+
s = sArr.join('');
108+
}
109+
110+
return s == goal;
111+
}
112+
}

BuddyStrings/buddy_strings.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
func buddyStrings(s string, goal string) bool {
4+
n := len(s)
5+
6+
if s == goal {
7+
temp := make(map[rune]bool)
8+
for _, c := range s {
9+
temp[c] = true
10+
}
11+
return len(temp) < len(goal) // Swapping same characters
12+
}
13+
14+
i := 0
15+
j := n - 1
16+
17+
for i < j && s[i] == goal[i] {
18+
i++
19+
}
20+
21+
for j >= 0 && s[j] == goal[j] {
22+
j--
23+
}
24+
25+
if i < j {
26+
sArr := []rune(s)
27+
temp := sArr[i]
28+
sArr[i] = sArr[j]
29+
sArr[j] = temp
30+
s = string(sArr)
31+
}
32+
33+
return s == goal
34+
}

BuddyStrings/buddy_strings.md

Whitespace-only changes.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
240240
- [**956.** Tallest Billboard](TallestBillboard/tallest_billboard.dart)
241241
- [**864.** Shortest Path to Get All Keys](ShortestPathToGetAllKeys/shortest_path_to_get_all_keys.dart)
242242
- [**1970.** Last Day Where You Can Still Cross](LastDayWhereYouCanStillCross/last_day_where_you_can_still_cross.dart)
243+
- [**859.** Buddy Strings](BuddyStrings/buddy_strings.dart)
243244

244245
## Reach me via
245246

0 commit comments

Comments
 (0)