Skip to content

Commit 8d588da

Browse files
committed
feat: add solutions to lc problem: No.1540
No.1540.Can Convert String in K Moves
1 parent f1d743e commit 8d588da

File tree

6 files changed

+225
-2
lines changed

6 files changed

+225
-2
lines changed

solution/1500-1599/1540.Can Convert String in K Moves/README.md

+86-1
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,107 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
**方法一:计数**
65+
66+
我们首先判断字符串 $s$ 和字符串 $t$ 的长度是否相等,如果不相等,直接返回 `false`
67+
68+
如果相等,我们可以统计每个位置的字符需要操作的最小次数,即 $cnt[x]$ 表示最小操作次数为 $x$ 的字符的个数。
69+
70+
如果有 $cnt[x]$ 个字符需要操作 $x$ 次,那么我们需要 $x + 26 \times (cnt[x] - 1)$ 次操作才能将这些字符转换为 $t$ 中对应的字符。因此,我们在 $[1,..25] 范围内枚举 $x$,如果 $x + 26 \times (cnt[x] - 1) \gt k$,说明我们无法将所有字符转换为 $t$ 中对应的字符,返回 `false`
71+
72+
否则,枚举结束后,说明我们可以将所有字符转换为 $t$ 中对应的字符,返回 `true`
73+
74+
时间复杂度 $O(n + C)$,空间复杂度 $O(C)$,其中 $n$ 为字符串 $s$ 和 $t$ 的长度;而 $C$ 为字符集大小,本题中 $C = 26$。
75+
6476
<!-- tabs:start -->
6577

6678
### **Python3**
6779

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

7082
```python
71-
83+
class Solution:
84+
def canConvertString(self, s: str, t: str, k: int) -> bool:
85+
if len(s) != len(t):
86+
return False
87+
cnt = [0] * 26
88+
for a, b in zip(s, t):
89+
x = (ord(b) - ord(a) + 26) % 26
90+
cnt[x] += 1
91+
for i in range(1, 26):
92+
if i + 26 * (cnt[i] - 1) > k:
93+
return False
94+
return True
7295
```
7396

7497
### **Java**
7598

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

78101
```java
102+
class Solution {
103+
public boolean canConvertString(String s, String t, int k) {
104+
if (s.length() != t.length()) {
105+
return false;
106+
}
107+
int[] cnt = new int[26];
108+
for (int i = 0; i < s.length(); ++i) {
109+
int x = (t.charAt(i) - s.charAt(i) + 26) % 26;
110+
++cnt[x];
111+
}
112+
for (int i = 1; i < 26; ++i) {
113+
if (i + 26 * (cnt[i] - 1) > k) {
114+
return false;
115+
}
116+
}
117+
return true;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
bool canConvertString(string s, string t, int k) {
128+
if (s.size() != t.size()) {
129+
return false;
130+
}
131+
int cnt[26]{};
132+
for (int i = 0; i < s.size(); ++i) {
133+
int x = (t[i] - s[i] + 26) % 26;
134+
++cnt[x];
135+
}
136+
for (int i = 1; i < 26; ++i) {
137+
if (i + 26 * (cnt[i] - 1) > k) {
138+
return false;
139+
}
140+
}
141+
return true;
142+
}
143+
};
144+
```
79145
146+
### **Go**
147+
148+
```go
149+
func canConvertString(s string, t string, k int) bool {
150+
if len(s) != len(t) {
151+
return false
152+
}
153+
cnt := [26]int{}
154+
for i := range s {
155+
x := (t[i] - s[i] + 26) % 26
156+
cnt[x]++
157+
}
158+
for i := 1; i < 26; i++ {
159+
if i+26*(cnt[i]-1) > k {
160+
return false
161+
}
162+
}
163+
return true
164+
}
80165
```
81166

82167
### **...**

solution/1500-1599/1540.Can Convert String in K Moves/README_EN.md

+74-1
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,86 @@
6060
### **Python3**
6161

6262
```python
63-
63+
class Solution:
64+
def canConvertString(self, s: str, t: str, k: int) -> bool:
65+
if len(s) != len(t):
66+
return False
67+
cnt = [0] * 26
68+
for a, b in zip(s, t):
69+
x = (ord(b) - ord(a) + 26) % 26
70+
cnt[x] += 1
71+
for i in range(1, 26):
72+
if i + 26 * (cnt[i] - 1) > k:
73+
return False
74+
return True
6475
```
6576

6677
### **Java**
6778

6879
```java
80+
class Solution {
81+
public boolean canConvertString(String s, String t, int k) {
82+
if (s.length() != t.length()) {
83+
return false;
84+
}
85+
int[] cnt = new int[26];
86+
for (int i = 0; i < s.length(); ++i) {
87+
int x = (t.charAt(i) - s.charAt(i) + 26) % 26;
88+
++cnt[x];
89+
}
90+
for (int i = 1; i < 26; ++i) {
91+
if (i + 26 * (cnt[i] - 1) > k) {
92+
return false;
93+
}
94+
}
95+
return true;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
bool canConvertString(string s, string t, int k) {
106+
if (s.size() != t.size()) {
107+
return false;
108+
}
109+
int cnt[26]{};
110+
for (int i = 0; i < s.size(); ++i) {
111+
int x = (t[i] - s[i] + 26) % 26;
112+
++cnt[x];
113+
}
114+
for (int i = 1; i < 26; ++i) {
115+
if (i + 26 * (cnt[i] - 1) > k) {
116+
return false;
117+
}
118+
}
119+
return true;
120+
}
121+
};
122+
```
69123
124+
### **Go**
125+
126+
```go
127+
func canConvertString(s string, t string, k int) bool {
128+
if len(s) != len(t) {
129+
return false
130+
}
131+
cnt := [26]int{}
132+
for i := range s {
133+
x := (t[i] - s[i] + 26) % 26
134+
cnt[x]++
135+
}
136+
for i := 1; i < 26; i++ {
137+
if i+26*(cnt[i]-1) > k {
138+
return false
139+
}
140+
}
141+
return true
142+
}
70143
```
71144

72145
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
bool canConvertString(string s, string t, int k) {
4+
if (s.size() != t.size()) {
5+
return false;
6+
}
7+
int cnt[26]{};
8+
for (int i = 0; i < s.size(); ++i) {
9+
int x = (t[i] - s[i] + 26) % 26;
10+
++cnt[x];
11+
}
12+
for (int i = 1; i < 26; ++i) {
13+
if (i + 26 * (cnt[i] - 1) > k) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func canConvertString(s string, t string, k int) bool {
2+
if len(s) != len(t) {
3+
return false
4+
}
5+
cnt := [26]int{}
6+
for i := range s {
7+
x := (t[i] - s[i] + 26) % 26
8+
cnt[x]++
9+
}
10+
for i := 1; i < 26; i++ {
11+
if i+26*(cnt[i]-1) > k {
12+
return false
13+
}
14+
}
15+
return true
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public boolean canConvertString(String s, String t, int k) {
3+
if (s.length() != t.length()) {
4+
return false;
5+
}
6+
int[] cnt = new int[26];
7+
for (int i = 0; i < s.length(); ++i) {
8+
int x = (t.charAt(i) - s.charAt(i) + 26) % 26;
9+
++cnt[x];
10+
}
11+
for (int i = 1; i < 26; ++i) {
12+
if (i + 26 * (cnt[i] - 1) > k) {
13+
return false;
14+
}
15+
}
16+
return true;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def canConvertString(self, s: str, t: str, k: int) -> bool:
3+
if len(s) != len(t):
4+
return False
5+
cnt = [0] * 26
6+
for a, b in zip(s, t):
7+
x = (ord(b) - ord(a) + 26) % 26
8+
cnt[x] += 1
9+
for i in range(1, 26):
10+
if i + 26 * (cnt[i] - 1) > k:
11+
return False
12+
return True

0 commit comments

Comments
 (0)