|
| 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 | +} |
0 commit comments