We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 11864ca commit a4156fcCopy full SHA for a4156fc
1 file changed
Others/Palindrome.java
@@ -22,4 +22,29 @@ public boolean SecondWay(String x) {
22
23
return SecondWay(x.substring(1, x.length() - 1));
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
50
0 commit comments