|
68 | 68 |
|
69 | 69 | <!-- 这里可写通用的实现逻辑 -->
|
70 | 70 |
|
| 71 | +**方法一:冒泡排序** |
| 72 | + |
| 73 | +题目实际上等价于判断:将字符串 $s$ 中任意长度为 $2$ 的子字符串采用冒泡排序交换,是否能得到 $t$。 |
| 74 | + |
| 75 | +因此我们用一个长度为 $10$ 的数组 $pos$ 记录字符串 $s$ 中每个字符数字的下标,其中 $pos[i]$ 表示数字 $i$ 出现的下标列表,按从小到大排序。 |
| 76 | + |
| 77 | +接下来,我们遍历字符串 $t$,对于 $t$ 中的每个字符 $t[i]$,我们转为数字 $x$,我们判断 $pos[x]$ 是否为空,若是,说明字符串 $s$ 中不存在 $t$ 中的数字,直接返回 `false`。否则,若要将 $pos[x]$ 的第一个位置下标的字符交换到下标 $i$ 的位置,需要满足小于 $x$ 的所有数字的下标均不小于 $pos[x]$ 的第一个位置下标,若不满足,返回 `false`。否则,我们将 $pos[x]$ 的第一个位置下标弹出,然后继续遍历字符串 $t$。 |
| 78 | + |
| 79 | +遍历结束,返回 `true`。 |
| 80 | + |
| 81 | +时间复杂度 $O(n \times C)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度,而 $C$ 是数字集的大小,本题中 $C=10$。 |
| 82 | + |
71 | 83 | <!-- tabs:start -->
|
72 | 84 |
|
73 | 85 | ### **Python3**
|
74 | 86 |
|
75 | 87 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
76 | 88 |
|
77 | 89 | ```python
|
78 |
| - |
| 90 | +class Solution: |
| 91 | + def isTransformable(self, s: str, t: str) -> bool: |
| 92 | + pos = defaultdict(deque) |
| 93 | + for i, c in enumerate(s): |
| 94 | + pos[int(c)].append(i) |
| 95 | + for c in t: |
| 96 | + x = int(c) |
| 97 | + if not pos[x] or any(pos[i] and pos[i][0] < pos[x][0] for i in range(x)): |
| 98 | + return False |
| 99 | + pos[x].popleft() |
| 100 | + return True |
79 | 101 | ```
|
80 | 102 |
|
81 | 103 | ### **Java**
|
82 | 104 |
|
83 | 105 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
84 | 106 |
|
85 | 107 | ```java
|
| 108 | +class Solution { |
| 109 | + public boolean isTransformable(String s, String t) { |
| 110 | + Deque<Integer>[] pos = new Deque[10]; |
| 111 | + Arrays.setAll(pos, k -> new ArrayDeque<>()); |
| 112 | + for (int i = 0; i < s.length(); ++i) { |
| 113 | + pos[s.charAt(i) - '0'].offer(i); |
| 114 | + } |
| 115 | + for (int i = 0; i < t.length(); ++i) { |
| 116 | + int x = t.charAt(i) - '0'; |
| 117 | + if (pos[x].isEmpty()) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + for (int j = 0; j < x; ++j) { |
| 121 | + if (!pos[j].isEmpty() && pos[j].peek() < pos[x].peek()) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + } |
| 125 | + pos[x].poll(); |
| 126 | + } |
| 127 | + return true; |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +### **C++** |
| 133 | + |
| 134 | +```cpp |
| 135 | +class Solution { |
| 136 | +public: |
| 137 | + bool isTransformable(string s, string t) { |
| 138 | + queue<int> pos[10]; |
| 139 | + for (int i = 0; i < s.size(); ++i) { |
| 140 | + pos[s[i] - '0'].push(i); |
| 141 | + } |
| 142 | + for (char& c : t) { |
| 143 | + int x = c - '0'; |
| 144 | + if (pos[x].empty()) { |
| 145 | + return false; |
| 146 | + } |
| 147 | + for (int j = 0; j < x; ++j) { |
| 148 | + if (!pos[j].empty() && pos[j].front() < pos[x].front()) { |
| 149 | + return false; |
| 150 | + } |
| 151 | + } |
| 152 | + pos[x].pop(); |
| 153 | + } |
| 154 | + return true; |
| 155 | + } |
| 156 | +}; |
| 157 | +``` |
86 | 158 |
|
| 159 | +### **Go** |
| 160 | +
|
| 161 | +```go |
| 162 | +func isTransformable(s string, t string) bool { |
| 163 | + pos := [10][]int{} |
| 164 | + for i, c := range s { |
| 165 | + pos[c-'0'] = append(pos[c-'0'], i) |
| 166 | + } |
| 167 | + for _, c := range t { |
| 168 | + x := int(c - '0') |
| 169 | + if len(pos[x]) == 0 { |
| 170 | + return false |
| 171 | + } |
| 172 | + for j := 0; j < x; j++ { |
| 173 | + if len(pos[j]) > 0 && pos[j][0] < pos[x][0] { |
| 174 | + return false |
| 175 | + } |
| 176 | + } |
| 177 | + pos[x] = pos[x][1:] |
| 178 | + } |
| 179 | + return true |
| 180 | +} |
87 | 181 | ```
|
88 | 182 |
|
89 | 183 | ### **...**
|
|
0 commit comments