Skip to content

Commit 8d1b5e9

Browse files
committed
feat: add solutions to lc problem: No.1585
No.1585.Check If String Is Transformable With Substring Sort Operations
1 parent 11bb188 commit 8d1b5e9

File tree

6 files changed

+252
-2
lines changed

6 files changed

+252
-2
lines changed

solution/1500-1599/1585.Check If String Is Transformable With Substring Sort Operations/README.md

+95-1
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,116 @@
6868

6969
<!-- 这里可写通用的实现逻辑 -->
7070

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+
7183
<!-- tabs:start -->
7284

7385
### **Python3**
7486

7587
<!-- 这里可写当前语言的特殊实现逻辑 -->
7688

7789
```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
79101
```
80102

81103
### **Java**
82104

83105
<!-- 这里可写当前语言的特殊实现逻辑 -->
84106

85107
```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+
```
86158
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+
}
87181
```
88182

89183
### **...**

solution/1500-1599/1585.Check If String Is Transformable With Substring Sort Operations/README_EN.md

+83-1
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,95 @@
6464
### **Python3**
6565

6666
```python
67-
67+
class Solution:
68+
def isTransformable(self, s: str, t: str) -> bool:
69+
pos = defaultdict(deque)
70+
for i, c in enumerate(s):
71+
pos[int(c)].append(i)
72+
for c in t:
73+
x = int(c)
74+
if not pos[x] or any(pos[i] and pos[i][0] < pos[x][0] for i in range(x)):
75+
return False
76+
pos[x].popleft()
77+
return True
6878
```
6979

7080
### **Java**
7181

7282
```java
83+
class Solution {
84+
public boolean isTransformable(String s, String t) {
85+
Deque<Integer>[] pos = new Deque[10];
86+
Arrays.setAll(pos, k -> new ArrayDeque<>());
87+
for (int i = 0; i < s.length(); ++i) {
88+
pos[s.charAt(i) - '0'].offer(i);
89+
}
90+
for (int i = 0; i < t.length(); ++i) {
91+
int x = t.charAt(i) - '0';
92+
if (pos[x].isEmpty()) {
93+
return false;
94+
}
95+
for (int j = 0; j < x; ++j) {
96+
if (!pos[j].isEmpty() && pos[j].peek() < pos[x].peek()) {
97+
return false;
98+
}
99+
}
100+
pos[x].poll();
101+
}
102+
return true;
103+
}
104+
}
105+
```
106+
107+
### **C++**
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
bool isTransformable(string s, string t) {
113+
queue<int> pos[10];
114+
for (int i = 0; i < s.size(); ++i) {
115+
pos[s[i] - '0'].push(i);
116+
}
117+
for (char& c : t) {
118+
int x = c - '0';
119+
if (pos[x].empty()) {
120+
return false;
121+
}
122+
for (int j = 0; j < x; ++j) {
123+
if (!pos[j].empty() && pos[j].front() < pos[x].front()) {
124+
return false;
125+
}
126+
}
127+
pos[x].pop();
128+
}
129+
return true;
130+
}
131+
};
132+
```
73133
134+
### **Go**
135+
136+
```go
137+
func isTransformable(s string, t string) bool {
138+
pos := [10][]int{}
139+
for i, c := range s {
140+
pos[c-'0'] = append(pos[c-'0'], i)
141+
}
142+
for _, c := range t {
143+
x := int(c - '0')
144+
if len(pos[x]) == 0 {
145+
return false
146+
}
147+
for j := 0; j < x; j++ {
148+
if len(pos[j]) > 0 && pos[j][0] < pos[x][0] {
149+
return false
150+
}
151+
}
152+
pos[x] = pos[x][1:]
153+
}
154+
return true
155+
}
74156
```
75157

76158
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
bool isTransformable(string s, string t) {
4+
queue<int> pos[10];
5+
for (int i = 0; i < s.size(); ++i) {
6+
pos[s[i] - '0'].push(i);
7+
}
8+
for (char& c : t) {
9+
int x = c - '0';
10+
if (pos[x].empty()) {
11+
return false;
12+
}
13+
for (int j = 0; j < x; ++j) {
14+
if (!pos[j].empty() && pos[j].front() < pos[x].front()) {
15+
return false;
16+
}
17+
}
18+
pos[x].pop();
19+
}
20+
return true;
21+
}
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func isTransformable(s string, t string) bool {
2+
pos := [10][]int{}
3+
for i, c := range s {
4+
pos[c-'0'] = append(pos[c-'0'], i)
5+
}
6+
for _, c := range t {
7+
x := int(c - '0')
8+
if len(pos[x]) == 0 {
9+
return false
10+
}
11+
for j := 0; j < x; j++ {
12+
if len(pos[j]) > 0 && pos[j][0] < pos[x][0] {
13+
return false
14+
}
15+
}
16+
pos[x] = pos[x][1:]
17+
}
18+
return true
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public boolean isTransformable(String s, String t) {
3+
Deque<Integer>[] pos = new Deque[10];
4+
Arrays.setAll(pos, k -> new ArrayDeque<>());
5+
for (int i = 0; i < s.length(); ++i) {
6+
pos[s.charAt(i) - '0'].offer(i);
7+
}
8+
for (int i = 0; i < t.length(); ++i) {
9+
int x = t.charAt(i) - '0';
10+
if (pos[x].isEmpty()) {
11+
return false;
12+
}
13+
for (int j = 0; j < x; ++j) {
14+
if (!pos[j].isEmpty() && pos[j].peek() < pos[x].peek()) {
15+
return false;
16+
}
17+
}
18+
pos[x].poll();
19+
}
20+
return true;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def isTransformable(self, s: str, t: str) -> bool:
3+
pos = defaultdict(deque)
4+
for i, c in enumerate(s):
5+
pos[int(c)].append(i)
6+
for c in t:
7+
x = int(c)
8+
if not pos[x] or any(pos[i] and pos[i][0] < pos[x][0] for i in range(x)):
9+
return False
10+
pos[x].popleft()
11+
return True

0 commit comments

Comments
 (0)