-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidPalindrome.java
More file actions
122 lines (104 loc) · 3.65 KB
/
ValidPalindrome.java
File metadata and controls
122 lines (104 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package Algorithms.TwoPointers;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 29 June 2025
* @link 125. Valid Palindrome <a href="https://leetcode.com/problems/valid-palindrome/">LeetCode link</a>
* @topics String, Two Pointers
*/
public class ValidPalindrome {
public static void main(String[] args) {
String s = "abba";
System.out.println(isPalindromeMyApproach(s));
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(1)
*/
public boolean isPalindrome(String s) {
int l = 0, r = s.length()-1;
while (l<r) {
while (!Character.isLetterOrDigit(s.charAt(l))) l++; // or s.charAt(l) < 'a' || s.charAt(l) > 'z' || s.charAt(l) < 'A' || s.charAt(l) > 'Z'
while (!Character.isLetterOrDigit(s.charAt(r))) r--; // or s.charAt(r) < 'a' || s.charAt(r) > 'z' || s.charAt(r) < 'A' || s.charAt(r) > 'Z'
if (Character.toLowerCase(s.charAt(l)) != Character.toLowerCase(s.charAt(r))) return false;
l++; r--;
}
return true;
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(1)
* But this method is slower than {@link #isPalindrome}
*/
public static boolean isPalindromeMyApproach(String s) {
s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); // s.replaceAll("\\s+", "").replaceAll("[\\W_]","").toLowerCase();
int l=0;
int r=s.length()-1;
while(l<=r) {
if (s.charAt(l) != s.charAt(r)) {
return false;
}
l++;
r--;
}
return true;
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(n) - for StringBuilder
*/
public static boolean isPalindromeMyApproach2(String s) {
s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
return new StringBuilder(s).reverse().toString().equals(s); // or Objects.equals(new StringBuilder(s).reverse().toString(), s);
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(1)
*/
public boolean isPalindromeMyApproach3(String s) {
int l = 0, r = s.length()-1;
while (l<=r) {
char leftChar = s.charAt(l); // or
char rightChar = s.charAt(r);
boolean canCompare = true;
if(!Character.isLetterOrDigit(leftChar)) {
l++;
canCompare = false;
}
if (!Character.isLetterOrDigit(rightChar)) {
r--;
canCompare = false;
}
if (canCompare) {
if (Character.toLowerCase(leftChar) != Character.toLowerCase(rightChar)) {
return false;
}
l++;
r--;
}
}
return true;
}
public boolean isPalindrome2(String s) {
int left =0, right = s.length() -1;
while(left < right){
char c1 = Character.toLowerCase(s.charAt(left));
char c2 = Character.toLowerCase(s.charAt(right));
// if(c1 >= 'A' && c1 <= 'Z') c1= (char)(c1 + 32); // ASCII conversion of lowercase -- 32 cause we have A to Z and [\]^_` and then a to z i.e 26+6
// if(c2 >= 'A' && c2 <= 'Z') c2= (char)(c2 + 32); // '0'=48, 'A'=65, 'a'=97
if(!((c1 >= 'a' && c1 <= 'z') || (c1 >= '0' && c1 <= '9'))){
left++;
continue;
}
if(!((c2 >= 'a' && c2 <= 'z') || (c2 >= '0' && c2 <= '9'))){
right--;
continue;
}
if(c1 != c2){
return false;
}
left++;
right--;
}
return true;
}
}