File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -22,4 +22,29 @@ public boolean SecondWay(String x) {
22
22
23
23
return SecondWay (x .substring (1 , x .length () - 1 ));
24
24
}
25
+
26
+ /**
27
+ * This method ignores all non-alphanumeric characters and case runs in O(n)
28
+ * where n is the length of s
29
+ *
30
+ * @param s String to check
31
+ * @return true if s is palindrome else false
32
+ */
33
+ public boolean isPalindrome (String s ) {
34
+ s = s .toLowerCase ().trim ();
35
+ StringBuilder sb = new StringBuilder ();
36
+ for (char c : s .toCharArray ()) {
37
+ if (Character .isLetter (c ) || Character .isDigit (c ))
38
+ sb .append (c );
39
+ }
40
+ s = sb .toString ();
41
+ int start = 0 ;
42
+ int end = s .length () - 1 ;
43
+ while (start <= end ) {
44
+ if (s .charAt (start ++) != s .charAt (end --))
45
+ return false ;
46
+
47
+ }
48
+ return true ;
49
+ }
25
50
}
You can’t perform that action at this time.
0 commit comments