Skip to content

Commit 06f4e75

Browse files
committed
feat: add solutions to lc problem: No.1153
No.1153.String Transforms Into Another String
1 parent e529fe4 commit 06f4e75

File tree

6 files changed

+309
-2
lines changed

6 files changed

+309
-2
lines changed

solution/1100-1199/1153.String Transforms Into Another String/README.md

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,135 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:哈希表**
47+
48+
我们可以先判断 `str1``str2` 是否相等,若相等,直接返回 `true`
49+
50+
然后我们统计 `str2` 中每个字母出现的次数,若出现的次数等于 $26$,说明 `str2` 包含了所有的小写字母,那么无论 `str1` 如何转换,都无法得到 `str2`,直接返回 `false`
51+
52+
否则,我们用数组或哈希表 `d` 记录 `str1` 中每个字母转换后的字母。遍历字符串 `str1``str2`,若 `str1` 中的某个字母已经转换过,那么其转换后的字母必须与 `str2` 中对应位置的字母相同,否则返回 `false`
53+
54+
遍历结束后,返回 `true`
55+
56+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 `str1` 的长度,而 $C$ 为字符集大小,本题中 $C = 26$。
57+
4658
<!-- tabs:start -->
4759

4860
### **Python3**
4961

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

5264
```python
53-
65+
class Solution:
66+
def canConvert(self, str1: str, str2: str) -> bool:
67+
if str1 == str2:
68+
return True
69+
if len(set(str2)) == 26:
70+
return False
71+
d = {}
72+
for a, b in zip(str1, str2):
73+
if a not in d:
74+
d[a] = b
75+
elif d[a] != b:
76+
return False
77+
return True
5478
```
5579

5680
### **Java**
5781

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

6084
```java
85+
class Solution {
86+
public boolean canConvert(String str1, String str2) {
87+
if (str1.equals(str2)) {
88+
return true;
89+
}
90+
int m = 0;
91+
int[] cnt = new int[26];
92+
int n = str1.length();
93+
for (int i = 0; i < n; ++i) {
94+
if (++cnt[str2.charAt(i) - 'a'] == 1) {
95+
++m;
96+
}
97+
}
98+
if (m == 26) {
99+
return false;
100+
}
101+
int[] d = new int[26];
102+
for (int i = 0; i < n; ++i) {
103+
int a = str1.charAt(i) - 'a';
104+
int b = str2.charAt(i) - 'a';
105+
if (d[a] == 0) {
106+
d[a] = b + 1;
107+
} else if (d[a] != b + 1) {
108+
return false;
109+
}
110+
}
111+
return true;
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
bool canConvert(string str1, string str2) {
122+
if (str1 == str2) {
123+
return true;
124+
}
125+
int cnt[26]{};
126+
int m = 0;
127+
for (char& c : str2) {
128+
if (++cnt[c - 'a'] == 1) {
129+
++m;
130+
}
131+
}
132+
if (m == 26) {
133+
return false;
134+
}
135+
int d[26]{};
136+
for (int i = 0; i < str1.size(); ++i) {
137+
int a = str1[i] - 'a';
138+
int b = str2[i] - 'a';
139+
if (d[a] == 0) {
140+
d[a] = b + 1;
141+
} else if (d[a] != b + 1) {
142+
return false;
143+
}
144+
}
145+
return true;
146+
}
147+
};
148+
```
61149
150+
### **Go**
151+
152+
```go
153+
func canConvert(str1 string, str2 string) bool {
154+
if str1 == str2 {
155+
return true
156+
}
157+
s := map[rune]bool{}
158+
for _, c := range str2 {
159+
s[c] = true
160+
if len(s) == 26 {
161+
return false
162+
}
163+
}
164+
d := [26]int{}
165+
for i, c := range str1 {
166+
a, b := int(c-'a'), int(str2[i]-'a')
167+
if d[a] == 0 {
168+
d[a] = b + 1
169+
} else if d[a] != b+1 {
170+
return false
171+
}
172+
}
173+
return true
174+
}
62175
```
63176

64177
### **...**

solution/1100-1199/1153.String Transforms Into Another String/README_EN.md

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,114 @@
4242
### **Python3**
4343

4444
```python
45-
45+
class Solution:
46+
def canConvert(self, str1: str, str2: str) -> bool:
47+
if str1 == str2:
48+
return True
49+
if len(set(str2)) == 26:
50+
return False
51+
d = {}
52+
for a, b in zip(str1, str2):
53+
if a not in d:
54+
d[a] = b
55+
elif d[a] != b:
56+
return False
57+
return True
4658
```
4759

4860
### **Java**
4961

5062
```java
63+
class Solution {
64+
public boolean canConvert(String str1, String str2) {
65+
if (str1.equals(str2)) {
66+
return true;
67+
}
68+
int m = 0;
69+
int[] cnt = new int[26];
70+
int n = str1.length();
71+
for (int i = 0; i < n; ++i) {
72+
if (++cnt[str2.charAt(i) - 'a'] == 1) {
73+
++m;
74+
}
75+
}
76+
if (m == 26) {
77+
return false;
78+
}
79+
int[] d = new int[26];
80+
for (int i = 0; i < n; ++i) {
81+
int a = str1.charAt(i) - 'a';
82+
int b = str2.charAt(i) - 'a';
83+
if (d[a] == 0) {
84+
d[a] = b + 1;
85+
} else if (d[a] != b + 1) {
86+
return false;
87+
}
88+
}
89+
return true;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
bool canConvert(string str1, string str2) {
100+
if (str1 == str2) {
101+
return true;
102+
}
103+
int cnt[26]{};
104+
int m = 0;
105+
for (char& c : str2) {
106+
if (++cnt[c - 'a'] == 1) {
107+
++m;
108+
}
109+
}
110+
if (m == 26) {
111+
return false;
112+
}
113+
int d[26]{};
114+
for (int i = 0; i < str1.size(); ++i) {
115+
int a = str1[i] - 'a';
116+
int b = str2[i] - 'a';
117+
if (d[a] == 0) {
118+
d[a] = b + 1;
119+
} else if (d[a] != b + 1) {
120+
return false;
121+
}
122+
}
123+
return true;
124+
}
125+
};
126+
```
51127
128+
### **Go**
129+
130+
```go
131+
func canConvert(str1 string, str2 string) bool {
132+
if str1 == str2 {
133+
return true
134+
}
135+
s := map[rune]bool{}
136+
for _, c := range str2 {
137+
s[c] = true
138+
if len(s) == 26 {
139+
return false
140+
}
141+
}
142+
d := [26]int{}
143+
for i, c := range str1 {
144+
a, b := int(c-'a'), int(str2[i]-'a')
145+
if d[a] == 0 {
146+
d[a] = b + 1
147+
} else if d[a] != b+1 {
148+
return false
149+
}
150+
}
151+
return true
152+
}
52153
```
53154

54155
### **...**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
bool canConvert(string str1, string str2) {
4+
if (str1 == str2) {
5+
return true;
6+
}
7+
int cnt[26]{};
8+
int m = 0;
9+
for (char& c : str2) {
10+
if (++cnt[c - 'a'] == 1) {
11+
++m;
12+
}
13+
}
14+
if (m == 26) {
15+
return false;
16+
}
17+
int d[26]{};
18+
for (int i = 0; i < str1.size(); ++i) {
19+
int a = str1[i] - 'a';
20+
int b = str2[i] - 'a';
21+
if (d[a] == 0) {
22+
d[a] = b + 1;
23+
} else if (d[a] != b + 1) {
24+
return false;
25+
}
26+
}
27+
return true;
28+
}
29+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func canConvert(str1 string, str2 string) bool {
2+
if str1 == str2 {
3+
return true
4+
}
5+
s := map[rune]bool{}
6+
for _, c := range str2 {
7+
s[c] = true
8+
if len(s) == 26 {
9+
return false
10+
}
11+
}
12+
d := [26]int{}
13+
for i, c := range str1 {
14+
a, b := int(c-'a'), int(str2[i]-'a')
15+
if d[a] == 0 {
16+
d[a] = b + 1
17+
} else if d[a] != b+1 {
18+
return false
19+
}
20+
}
21+
return true
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public boolean canConvert(String str1, String str2) {
3+
if (str1.equals(str2)) {
4+
return true;
5+
}
6+
int m = 0;
7+
int[] cnt = new int[26];
8+
int n = str1.length();
9+
for (int i = 0; i < n; ++i) {
10+
if (++cnt[str2.charAt(i) - 'a'] == 1) {
11+
++m;
12+
}
13+
}
14+
if (m == 26) {
15+
return false;
16+
}
17+
int[] d = new int[26];
18+
for (int i = 0; i < n; ++i) {
19+
int a = str1.charAt(i) - 'a';
20+
int b = str2.charAt(i) - 'a';
21+
if (d[a] == 0) {
22+
d[a] = b + 1;
23+
} else if (d[a] != b + 1) {
24+
return false;
25+
}
26+
}
27+
return true;
28+
}
29+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def canConvert(self, str1: str, str2: str) -> bool:
3+
if str1 == str2:
4+
return True
5+
if len(set(str2)) == 26:
6+
return False
7+
d = {}
8+
for a, b in zip(str1, str2):
9+
if a not in d:
10+
d[a] = b
11+
elif d[a] != b:
12+
return False
13+
return True

0 commit comments

Comments
 (0)