Skip to content

Commit 6fdd953

Browse files
committed
feat: add solutions to lc problem: No.1616
No.1616.Split Two Strings to Make Palindrome
1 parent 18fafb9 commit 6fdd953

File tree

7 files changed

+365
-2
lines changed

7 files changed

+365
-2
lines changed

solution/1600-1699/1616.Split Two Strings to Make Palindrome/README.md

+131-1
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,152 @@ b<sub>prefix</sub> = "jiz", b<sub>suffix</sub> = "alu"
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:双指针**
62+
63+
- 首先,一个指针从 `a` 的头开始,另一个指针从 `b` 的尾开始,如果字符相等,两个指针就往中间移动,遇到不同的字符或双指针交叉后停止
64+
- 双指针交叉说明 `prefix``suffix` 已经可以得到回文串
65+
- 否则还需要判断 `a[i:j+1]``b[i:j+1]` 是否是回文(通过 `prefix + palindrome + suffix` 得到回文串)
66+
- 如果还得不到回文串,尝试交换 `a`, `b` 重复同样的判断
67+
6168
<!-- tabs:start -->
6269

6370
### **Python3**
6471

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

6774
```python
68-
75+
class Solution:
76+
def checkPalindromeFormation(self, a: str, b: str) -> bool:
77+
def check1(a: str, b: str) -> bool:
78+
i, j = 0, len(b) - 1
79+
while i < j and a[i] == b[j]:
80+
i += 1
81+
j -= 1
82+
return i >= j or check2(a, i, j) or check2(b, i, j)
83+
84+
def check2(a: str, i: int, j: int) -> bool:
85+
while i < j and a[i] == a[j]:
86+
i += 1
87+
j -= 1
88+
return i >= j
89+
90+
return check1(a, b) or check1(b, a)
6991
```
7092

7193
### **Java**
7294

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

7597
```java
98+
class Solution {
99+
public boolean checkPalindromeFormation(String a, String b) {
100+
return check1(a, b) || check1(b, a);
101+
}
102+
103+
private boolean check1(String a, String b) {
104+
int i = 0;
105+
int j = b.length() - 1;
106+
while (i < j && a.charAt(i) == b.charAt(j)) {
107+
i++;
108+
j--;
109+
}
110+
return i >= j || check2(a, i, j) || check2(b, i, j);
111+
}
112+
113+
private boolean check2(String a, int i, int j) {
114+
while (i < j && a.charAt(i) == a.charAt(j)) {
115+
i++;
116+
j--;
117+
}
118+
return i >= j;
119+
}
120+
}
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
class Solution {
127+
public:
128+
bool checkPalindromeFormation(string a, string b) {
129+
return check1(a, b) || check1(b, a);
130+
}
131+
132+
private:
133+
bool check1(string &a, string &b) {
134+
int i = 0, j = b.size() - 1;
135+
while (i < j && a[i] == b[j]) {
136+
++i;
137+
--j;
138+
}
139+
return i >= j || check2(a, i, j) || check2(b, i, j);
140+
}
141+
142+
bool check2(string &a, int i, int j) {
143+
while (i <= j && a[i] == a[j]) {
144+
++i;
145+
--j;
146+
}
147+
return i >= j;
148+
}
149+
};
150+
```
151+
152+
### **Go**
153+
154+
```go
155+
func checkPalindromeFormation(a string, b string) bool {
156+
return check1(a, b) || check1(b, a)
157+
}
158+
159+
func check1(a, b string) bool {
160+
i, j := 0, len(b)-1
161+
for i < j && a[i] == b[j] {
162+
i++
163+
j--
164+
}
165+
return i >= j || check2(a, i, j) || check2(b, i, j)
166+
}
167+
168+
func check2(a string, i, j int) bool {
169+
for i < j && a[i] == a[j] {
170+
i++
171+
j--
172+
}
173+
return i >= j
174+
}
175+
```
76176

177+
### **Rust**
178+
179+
```rust
180+
impl Solution {
181+
pub fn check_palindrome_formation(a: String, b: String) -> bool {
182+
fn check1(a: &[u8], b: &[u8]) -> bool {
183+
let (mut i, mut j) = (0, b.len() - 1);
184+
while i < j && a[i] == b[j] {
185+
i += 1;
186+
j -= 1;
187+
}
188+
if i >= j {
189+
return true;
190+
}
191+
check2(a, i, j) || check2(b, i, j)
192+
}
193+
194+
fn check2(a: &[u8], mut i: usize, mut j: usize) -> bool {
195+
while i < j && a[i] == a[j] {
196+
i += 1;
197+
j -= 1;
198+
}
199+
i >= j
200+
}
201+
202+
let a = a.as_bytes();
203+
let b = b.as_bytes();
204+
check1(a, b) || check1(b, a)
205+
}
206+
}
77207
```
78208

79209
### **...**

solution/1600-1699/1616.Split Two Strings to Make Palindrome/README_EN.md

+124-1
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,136 @@ Then, a<sub>prefix</sub> + b<sub>suffix</sub> = &quot;ula&quot; + &quot;alu&quot
5858
### **Python3**
5959

6060
```python
61-
61+
class Solution:
62+
def checkPalindromeFormation(self, a: str, b: str) -> bool:
63+
def check1(a: str, b: str) -> bool:
64+
i, j = 0, len(b) - 1
65+
while i < j and a[i] == b[j]:
66+
i += 1
67+
j -= 1
68+
return i >= j or check2(a, i, j) or check2(b, i, j)
69+
70+
def check2(a: str, i: int, j: int) -> bool:
71+
while i < j and a[i] == a[j]:
72+
i += 1
73+
j -= 1
74+
return i >= j
75+
76+
return check1(a, b) or check1(b, a)
6277
```
6378

6479
### **Java**
6580

6681
```java
82+
class Solution {
83+
public boolean checkPalindromeFormation(String a, String b) {
84+
return check1(a, b) || check1(b, a);
85+
}
86+
87+
private boolean check1(String a, String b) {
88+
int i = 0;
89+
int j = b.length() - 1;
90+
while (i < j && a.charAt(i) == b.charAt(j)) {
91+
i++;
92+
j--;
93+
}
94+
return i >= j || check2(a, i, j) || check2(b, i, j);
95+
}
96+
97+
private boolean check2(String a, int i, int j) {
98+
while (i < j && a.charAt(i) == a.charAt(j)) {
99+
i++;
100+
j--;
101+
}
102+
return i >= j;
103+
}
104+
}
105+
```
106+
107+
### **C++**
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
bool checkPalindromeFormation(string a, string b) {
113+
return check1(a, b) || check1(b, a);
114+
}
115+
116+
private:
117+
bool check1(string &a, string &b) {
118+
int i = 0, j = b.size() - 1;
119+
while (i < j && a[i] == b[j]) {
120+
++i;
121+
--j;
122+
}
123+
return i >= j || check2(a, i, j) || check2(b, i, j);
124+
}
125+
126+
bool check2(string &a, int i, int j) {
127+
while (i <= j && a[i] == a[j]) {
128+
++i;
129+
--j;
130+
}
131+
return i >= j;
132+
}
133+
};
134+
```
135+
136+
### **Go**
137+
138+
```go
139+
func checkPalindromeFormation(a string, b string) bool {
140+
return check1(a, b) || check1(b, a)
141+
}
142+
143+
func check1(a, b string) bool {
144+
i, j := 0, len(b)-1
145+
for i < j && a[i] == b[j] {
146+
i++
147+
j--
148+
}
149+
return i >= j || check2(a, i, j) || check2(b, i, j)
150+
}
151+
152+
func check2(a string, i, j int) bool {
153+
for i < j && a[i] == a[j] {
154+
i++
155+
j--
156+
}
157+
return i >= j
158+
}
159+
```
67160

161+
### **Rust**
162+
163+
```rust
164+
impl Solution {
165+
pub fn check_palindrome_formation(a: String, b: String) -> bool {
166+
fn check1(a: &[u8], b: &[u8]) -> bool {
167+
let (mut i, mut j) = (0, b.len() - 1);
168+
while i < j && a[i] == b[j] {
169+
i += 1;
170+
j -= 1;
171+
}
172+
if i >= j {
173+
return true;
174+
}
175+
check2(a, i, j) || check2(b, i, j)
176+
}
177+
178+
fn check2(a: &[u8], mut i: usize, mut j: usize) -> bool {
179+
while i < j && a[i] == a[j] {
180+
i += 1;
181+
j -= 1;
182+
}
183+
i >= j
184+
}
185+
186+
let a = a.as_bytes();
187+
let b = b.as_bytes();
188+
check1(a, b) || check1(b, a)
189+
}
190+
}
68191
```
69192

70193
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
bool checkPalindromeFormation(string a, string b) {
4+
return check1(a, b) || check1(b, a);
5+
}
6+
7+
private:
8+
bool check1(string &a, string &b) {
9+
int i = 0, j = b.size() - 1;
10+
while (i < j && a[i] == b[j]) {
11+
++i;
12+
--j;
13+
}
14+
return i >= j || check2(a, i, j) || check2(b, i, j);
15+
}
16+
17+
bool check2(string &a, int i, int j) {
18+
while (i <= j && a[i] == a[j]) {
19+
++i;
20+
--j;
21+
}
22+
return i >= j;
23+
}
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func checkPalindromeFormation(a string, b string) bool {
2+
return check1(a, b) || check1(b, a)
3+
}
4+
5+
func check1(a, b string) bool {
6+
i, j := 0, len(b)-1
7+
for i < j && a[i] == b[j] {
8+
i++
9+
j--
10+
}
11+
return i >= j || check2(a, i, j) || check2(b, i, j)
12+
}
13+
14+
func check2(a string, i, j int) bool {
15+
for i < j && a[i] == a[j] {
16+
i++
17+
j--
18+
}
19+
return i >= j
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public boolean checkPalindromeFormation(String a, String b) {
3+
return check1(a, b) || check1(b, a);
4+
}
5+
6+
private boolean check1(String a, String b) {
7+
int i = 0;
8+
int j = b.length() - 1;
9+
while (i < j && a.charAt(i) == b.charAt(j)) {
10+
i++;
11+
j--;
12+
}
13+
return i >= j || check2(a, i, j) || check2(b, i, j);
14+
}
15+
16+
private boolean check2(String a, int i, int j) {
17+
while (i < j && a.charAt(i) == a.charAt(j)) {
18+
i++;
19+
j--;
20+
}
21+
return i >= j;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def checkPalindromeFormation(self, a: str, b: str) -> bool:
3+
def check1(a: str, b: str) -> bool:
4+
i, j = 0, len(b) - 1
5+
while i < j and a[i] == b[j]:
6+
i += 1
7+
j -= 1
8+
return i >= j or check2(a, i, j) or check2(b, i, j)
9+
10+
def check2(a: str, i: int, j: int) -> bool:
11+
while i < j and a[i] == a[j]:
12+
i += 1
13+
j -= 1
14+
return i >= j
15+
16+
return check1(a, b) or check1(b, a)

0 commit comments

Comments
 (0)