Skip to content

Commit bea0734

Browse files
committedJul 23, 2022
feat: add solutions to lc problem: No.2330
No.2330.Valid Palindrome IV
1 parent 2801bae commit bea0734

File tree

6 files changed

+131
-2
lines changed

6 files changed

+131
-2
lines changed
 

‎solution/2300-2399/2330.Valid Palindrome IV/README.md

+48-1
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,69 @@ Two operations could be performed to make s a palindrome so return true.
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
**方法一:双指针**
56+
5557
<!-- tabs:start -->
5658

5759
### **Python3**
5860

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

6163
```python
62-
64+
class Solution:
65+
def makePalindrome(self, s: str) -> bool:
66+
i, j = 0, len(s) - 1
67+
t = 0
68+
while i < j:
69+
if s[i] != s[j]:
70+
t += 1
71+
i, j = i + 1, j - 1
72+
return t <= 2
6373
```
6474

6575
### **Java**
6676

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

6979
```java
80+
class Solution {
81+
public boolean makePalindrome(String s) {
82+
int t = 0;
83+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
84+
if (s.charAt(i) != s.charAt(j)) {
85+
++t;
86+
}
87+
}
88+
return t <= 2;
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
bool makePalindrome(string s) {
99+
int t = 0;
100+
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) t += s[i] != s[j];
101+
return t <= 2;
102+
}
103+
};
104+
```
70105
106+
### **Go**
107+
108+
```go
109+
func makePalindrome(s string) bool {
110+
t := 0
111+
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
112+
if s[i] != s[j] {
113+
t++
114+
}
115+
}
116+
return t <= 2
117+
}
71118
```
72119

73120
### **TypeScript**

‎solution/2300-2399/2330.Valid Palindrome IV/README_EN.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,58 @@ Two operations could be performed to make s a palindrome so return true.
5353
### **Python3**
5454

5555
```python
56-
56+
class Solution:
57+
def makePalindrome(self, s: str) -> bool:
58+
i, j = 0, len(s) - 1
59+
t = 0
60+
while i < j:
61+
if s[i] != s[j]:
62+
t += 1
63+
i, j = i + 1, j - 1
64+
return t <= 2
5765
```
5866

5967
### **Java**
6068

6169
```java
70+
class Solution {
71+
public boolean makePalindrome(String s) {
72+
int t = 0;
73+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
74+
if (s.charAt(i) != s.charAt(j)) {
75+
++t;
76+
}
77+
}
78+
return t <= 2;
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
bool makePalindrome(string s) {
89+
int t = 0;
90+
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) t += s[i] != s[j];
91+
return t <= 2;
92+
}
93+
};
94+
```
6295
96+
### **Go**
97+
98+
```go
99+
func makePalindrome(s string) bool {
100+
t := 0
101+
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
102+
if s[i] != s[j] {
103+
t++
104+
}
105+
}
106+
return t <= 2
107+
}
63108
```
64109

65110
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
bool makePalindrome(string s) {
4+
int t = 0;
5+
for (int i = 0, j = s.size() - 1; i < j; ++i, --j) t += s[i] != s[j];
6+
return t <= 2;
7+
}
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func makePalindrome(s string) bool {
2+
t := 0
3+
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
4+
if s[i] != s[j] {
5+
t++
6+
}
7+
}
8+
return t <= 2
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public boolean makePalindrome(String s) {
3+
int t = 0;
4+
for (int i = 0, j = s.length() - 1; i < j; ++i, --j) {
5+
if (s.charAt(i) != s.charAt(j)) {
6+
++t;
7+
}
8+
}
9+
return t <= 2;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def makePalindrome(self, s: str) -> bool:
3+
i, j = 0, len(s) - 1
4+
t = 0
5+
while i < j:
6+
if s[i] != s[j]:
7+
t += 1
8+
i, j = i + 1, j - 1
9+
return t <= 2

0 commit comments

Comments
 (0)