g0101_0200.s0125_valid_palindrome.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0101_0200.s0125_valid_palindrome;
// #Easy #Top_Interview_Questions #String #Two_Pointers #Udemy_Two_Pointers
// #2022_06_23_Time_3_ms_(98.64%)_Space_43.2_MB_(81.23%)
/**
* 125 - Valid Palindrome\.
*
* Easy
*
* A phrase is a **palindrome** if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
*
* Given a string `s`, return `true` _if it is a **palindrome** , or_ `false` _otherwise_.
*
* **Example 1:**
*
* **Input:** s = "A man, a plan, a canal: Panama"
*
* **Output:** true
*
* **Explanation:** "amanaplanacanalpanama" is a palindrome.
*
* **Example 2:**
*
* **Input:** s = "race a car"
*
* **Output:** false
*
* **Explanation:** "raceacar" is not a palindrome.
*
* **Example 3:**
*
* **Input:** s = " "
*
* **Output:** true
*
* **Explanation:** s is an empty string "" after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome.
*
* **Constraints:**
*
* * 1 <= s.length <= 2 * 105
* * `s` consists only of printable ASCII characters.
**/
public class Solution {
public boolean isPalindrome(String s) {
int i = 0;
int j = s.length() - 1;
boolean res = true;
while (res) {
// Iterates through string to find first char which is alphanumeric.
// Done to ignore non-alphanumeric characters.
// Starts from 0 to j-1.
while (i < j && isNotAlphaNumeric(s.charAt(i))) {
i++;
}
// Similarly from j-1 to 0.
while (i < j && isNotAlphaNumeric(s.charAt(j))) {
j--;
}
// Checks if i is greater than or equal to j.
// The main loop only needs to loop n / 2 times hence this condition (where n is string
// length).
if (i >= j) {
break;
}
// Assigning found indices to variables.
// The upperToLower function is used to convert characters, if upper case, to lower
// case.
// If already lower case, it'll return as it is.
char left = upperToLower(s.charAt(i));
char right = upperToLower(s.charAt(j));
// If both variables are not same, result becomes false, and breaks out of the loop at
// next iteration.
if (left != right) {
res = false;
}
i++;
j--;
}
return res;
}
private boolean isNotAlphaNumeric(char c) {
return (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && (c < '0' || c > '9');
}
private boolean isUpper(char c) {
return c >= 'A' && c <= 'Z';
}
private char upperToLower(char c) {
if (isUpper(c)) {
c = (char) (c + 32);
}
return c;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy