Skip to content

Commit f8f045f

Browse files
add 680
1 parent 5a1441f commit f8f045f

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|680|[Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_680.java) | O(n) | O(1) | Easy | String
2526
|676|[Implement Magic Dictionary](https://leetcode.com/problems/implement-magic-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_676.java) | O(n^2) | O(n) | Medium |
2627
|674|[Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_674.java) | O(n^2) | O(1) | Easy |
2728
|672|[Bulb Switcher II](https://leetcode.com/problems/bulb-switcher-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_672.java) | O(1) | O(1) | Medium | Math
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 680. Valid Palindrome II
5+
*
6+
* Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.
7+
8+
Example 1:
9+
Input: "aba"
10+
Output: True
11+
12+
Example 2:
13+
Input: "abca"
14+
Output: True
15+
Explanation: You could delete the character 'c'.
16+
17+
Note:
18+
The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
19+
20+
*/
21+
public class _680 {
22+
public static class Solution1 {
23+
public boolean validPalindrome(String s) {
24+
int left = 0;
25+
int right = s.length() - 1;
26+
int diff = 0;
27+
while (left < right) {
28+
if (s.charAt(left) != s.charAt(right)) {
29+
left++;
30+
diff++;
31+
if (diff > 1) {
32+
break;
33+
}
34+
} else {
35+
left++;
36+
right--;
37+
}
38+
}
39+
if (diff < 2) {
40+
return true;
41+
}
42+
diff = 0;
43+
left = 0;
44+
right = s.length() - 1;
45+
while (left < right) {
46+
if (s.charAt(left) != s.charAt(right)) {
47+
right--;
48+
diff++;
49+
if (diff > 1) {
50+
break;
51+
}
52+
} else {
53+
left++;
54+
right--;
55+
}
56+
}
57+
return diff < 2;
58+
}
59+
}
60+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._680;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _680Test {
10+
private static _680.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _680.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.validPalindrome("aba"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(true, solution1.validPalindrome("abcca"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(true, solution1.validPalindrome("acbca"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(false, solution1.validPalindrome("accbba"));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(true, solution1.validPalindrome("abdeeda"));
40+
}
41+
42+
@Test
43+
public void test6() {
44+
assertEquals(true, solution1.validPalindrome("cbbcc"));
45+
}
46+
47+
@Test
48+
public void test7() {
49+
assertEquals(false, solution1.validPalindrome("abc"));
50+
}
51+
}

0 commit comments

Comments
 (0)