Skip to content

Commit 775e755

Browse files
committed
feat: add python and java solutions to lcci question
添加《程序员面试金典》题解:面试题 01.05. 一次编辑
1 parent 9e16108 commit 775e755

File tree

4 files changed

+171
-49
lines changed

4 files changed

+171
-49
lines changed

lcci/01.05.One Away/README.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,53 @@ second = "pal"
2626

2727
## 解法
2828
<!-- 这里可写通用的实现逻辑 -->
29-
29+
遍历两个字符串,逐个字符比较判断。
3030

3131
### Python3
3232
<!-- 这里可写当前语言的特殊实现逻辑 -->
3333

3434
```python
35-
35+
class Solution:
36+
def oneEditAway(self, first: str, second: str) -> bool:
37+
n1, n2 = len(first), len(second)
38+
if abs(n1 - n2) > 1:
39+
return False
40+
if n1 + n2 <= 2:
41+
return True
42+
if first[0] != second[0]:
43+
if n1 == n2:
44+
return first[1:] == second[1:]
45+
else:
46+
return first[1:] == second or second[1:] == first
47+
else:
48+
return self.oneEditAway(first[1:], second[1:])
3649
```
3750

3851
### Java
3952
<!-- 这里可写当前语言的特殊实现逻辑 -->
4053

4154
```java
42-
55+
class Solution {
56+
public boolean oneEditAway(String first, String second) {
57+
int n1 = first.length(), n2 = second.length();
58+
int differ = Math.abs(n1 - n2);
59+
if (differ > 1) {
60+
return false;
61+
}
62+
if (n1 + n2 <= 2) {
63+
return true;
64+
}
65+
if (first.charAt(0) != second.charAt(0)) {
66+
if (n1 == n2) {
67+
return first.substring(1).equals(second.substring(1));
68+
} else {
69+
return first.substring(1).equals(second) || second.substring(1).equals(first);
70+
}
71+
} else {
72+
return oneEditAway(first.substring(1), second.substring(1));
73+
}
74+
}
75+
}
4376
```
4477

4578
### ...

lcci/01.05.One Away/README_EN.md

Lines changed: 100 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,100 @@
1-
# [01.05. One Away](https://leetcode-cn.com/problems/one-away-lcci)
2-
3-
## Description
4-
<p>There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away.</p>
5-
6-
<p>&nbsp;</p>
7-
8-
<p><strong>Example&nbsp;1:</strong></p>
9-
10-
<pre>
11-
<strong>Input:</strong>
12-
first = &quot;pale&quot;
13-
second = &quot;ple&quot;
14-
<strong>Output:</strong> True
15-
</pre>
16-
17-
<p><strong>Example&nbsp;2:</strong></p>
18-
19-
<pre>
20-
<strong>Input:</strong>
21-
first = &quot;pales&quot;
22-
second = &quot;pal&quot;
23-
<strong>Output:</strong> False
24-
</pre>
25-
26-
27-
28-
## Solutions
29-
30-
31-
### Python3
32-
33-
```python
34-
35-
```
36-
37-
### Java
38-
39-
```java
40-
41-
```
42-
43-
### ...
44-
```
45-
46-
```
1+
# [01.05. One Away](https://leetcode-cn.com/problems/one-away-lcci)
2+
3+
## Description
4+
<p>There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away.</p>
5+
6+
7+
8+
<p>&nbsp;</p>
9+
10+
11+
12+
<p><strong>Example&nbsp;1:</strong></p>
13+
14+
15+
16+
<pre>
17+
18+
<strong>Input:</strong>
19+
20+
first = &quot;pale&quot;
21+
22+
second = &quot;ple&quot;
23+
24+
<strong>Output:</strong> True
25+
26+
</pre>
27+
28+
29+
30+
<p><strong>Example&nbsp;2:</strong></p>
31+
32+
33+
34+
<pre>
35+
36+
<strong>Input:</strong>
37+
38+
first = &quot;pales&quot;
39+
40+
second = &quot;pal&quot;
41+
42+
<strong>Output:</strong> False
43+
44+
</pre>
45+
46+
47+
48+
49+
## Solutions
50+
51+
52+
### Python3
53+
54+
```python
55+
class Solution:
56+
def oneEditAway(self, first: str, second: str) -> bool:
57+
n1, n2 = len(first), len(second)
58+
if abs(n1 - n2) > 1:
59+
return False
60+
if n1 + n2 <= 2:
61+
return True
62+
if first[0] != second[0]:
63+
if n1 == n2:
64+
return first[1:] == second[1:]
65+
else:
66+
return first[1:] == second or second[1:] == first
67+
else:
68+
return self.oneEditAway(first[1:], second[1:])
69+
```
70+
71+
### Java
72+
73+
```java
74+
class Solution {
75+
public boolean oneEditAway(String first, String second) {
76+
int n1 = first.length(), n2 = second.length();
77+
int differ = Math.abs(n1 - n2);
78+
if (differ > 1) {
79+
return false;
80+
}
81+
if (n1 + n2 <= 2) {
82+
return true;
83+
}
84+
if (first.charAt(0) != second.charAt(0)) {
85+
if (n1 == n2) {
86+
return first.substring(1).equals(second.substring(1));
87+
} else {
88+
return first.substring(1).equals(second) || second.substring(1).equals(first);
89+
}
90+
} else {
91+
return oneEditAway(first.substring(1), second.substring(1));
92+
}
93+
}
94+
}
95+
```
96+
97+
### ...
98+
```
99+
100+
```

lcci/01.05.One Away/Solution.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public boolean oneEditAway(String first, String second) {
3+
int n1 = first.length(), n2 = second.length();
4+
int differ = Math.abs(n1 - n2);
5+
if (differ > 1) {
6+
return false;
7+
}
8+
if (n1 + n2 <= 2) {
9+
return true;
10+
}
11+
if (first.charAt(0) != second.charAt(0)) {
12+
if (n1 == n2) {
13+
return first.substring(1).equals(second.substring(1));
14+
} else {
15+
return first.substring(1).equals(second) || second.substring(1).equals(first);
16+
}
17+
} else {
18+
return oneEditAway(first.substring(1), second.substring(1));
19+
}
20+
}
21+
}

lcci/01.05.One Away/Solution.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def oneEditAway(self, first: str, second: str) -> bool:
3+
n1, n2 = len(first), len(second)
4+
if abs(n1 - n2) > 1:
5+
return False
6+
if n1 + n2 <= 2:
7+
return True
8+
if first[0] != second[0]:
9+
if n1 == n2:
10+
return first[1:] == second[1:]
11+
else:
12+
return first[1:] == second or second[1:] == first
13+
else:
14+
return self.oneEditAway(first[1:], second[1:])

0 commit comments

Comments
 (0)