Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 33ee000

Browse files
LC#680 check if it is palindrome by deleting at max one character
1 parent e9ee867 commit 33ee000

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package LeetcodeDaily.April2022;
2+
3+
public class Day2ValidPalindromeII680 {
4+
5+
public static boolean validPalindrome(String s) {
6+
if (s.length() == 1)
7+
return true;
8+
9+
return checkDeletion(s, 0, s.length() - 1, 1);
10+
}
11+
12+
private static boolean checkDeletion(String s, int low, int high, int deletioncount) {
13+
14+
if (low >= high)
15+
return true;
16+
17+
// normal case
18+
if (s.charAt(low) == s.charAt(high))
19+
return checkDeletion(s, low + 1, high - 1, deletioncount);
20+
21+
if (deletioncount == 0)
22+
return false;
23+
24+
// if we need to delete one character either from low or from high cursor
25+
boolean deleteStart = checkDeletion(s, low + 1, high, deletioncount - 1);
26+
boolean deleteEnd = checkDeletion(s, low, high - 1, deletioncount - 1);
27+
28+
return deleteStart || deleteEnd;
29+
}
30+
31+
public static void main(String[] args) {
32+
System.out.println(validPalindrome("aba"));
33+
System.out.println(validPalindrome("abca"));
34+
System.out.println(validPalindrome("abc"));
35+
System.out.println(validPalindrome("deeee"));
36+
}
37+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /