File tree Expand file tree Collapse file tree 2 files changed +68
-0
lines changed
Expand file tree Collapse file tree 2 files changed +68
-0
lines changed Original file line number Diff line number Diff line change 1+ package leetcode ;
2+
3+ public class LC125 {
4+ public boolean isPalindrome (String s ) {
5+ int start = 0 ;
6+ int last = s .length () - 1 ;
7+
8+ while (start <= last ) {
9+ char currFirst = s .charAt (start );
10+ char currLast = s .charAt (last );
11+
12+ if (!Character .isLetterOrDigit (currFirst )) {
13+ start ++;
14+ } else if (!Character .isLetterOrDigit (currLast )) {
15+ last --;
16+ } else {
17+ if (Character .toLowerCase (currFirst ) != Character .toLowerCase (currLast )) {
18+ return false ;
19+ }
20+ start ++;
21+ last --;
22+ }
23+ }
24+
25+ return true ;
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ package leetcode ;
2+
3+ public class LC680 {
4+
5+ public boolean validPalindrome (String s ) {
6+ // base case
7+ if (s == null || s .isEmpty ()) {
8+ return false ;
9+ }
10+
11+ for (int i = 0 , j = s .length () -1 ; i < j ; i ++, j --) {
12+ if (s .charAt (i ) != s .charAt (j )) {
13+ int i1 = i + 1 , j1 = j , i2 = i , j2 = j - 1 ;
14+
15+ while (i1 < j1 && s .charAt (i1 ) == s .charAt (j1 )) {
16+ i1 ++;
17+ j1 --;
18+ }
19+ if (i1 >= j1 ) {
20+ return true ;
21+ }
22+
23+ while (i2 < j2 && s .charAt (i2 ) == s .charAt (j2 )) {
24+ i2 ++;
25+ j2 --;
26+ }
27+
28+ return i2 >= j2 ;
29+ }
30+ }
31+
32+ return true ;
33+ }
34+
35+ public static void main (String [] args ) {
36+ LC680 lc680 = new LC680 ();
37+ System .out .println (lc680 .validPalindrome ("aba" )); // Output: true
38+ System .out .println (lc680 .validPalindrome ("abca" )); // Output: true
39+ System .out .println (lc680 .validPalindrome ("abc" )); // Output: false
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments