|
| 1 | +--- |
| 2 | +id: valid-palindrome |
| 3 | +title: Valid Palindrome Solution |
| 4 | +sidebar_label: 0125 Valid Palindrome |
| 5 | +tags: |
| 6 | + - String |
| 7 | + - Two Pointers |
| 8 | + - LeetCode |
| 9 | + - Java |
| 10 | + - Python |
| 11 | +description: "This is a solution to the Valid Palindrome problem on LeetCode." |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +Given a string s, return true if it is a palindrome, or false otherwise. |
| 19 | + |
| 20 | +### Examples |
| 21 | + |
| 22 | +**Example 1:** |
| 23 | + |
| 24 | +``` |
| 25 | +Input: s = "A man, a plan, a canal: Panama" |
| 26 | +Output: true |
| 27 | +Explanation: "amanaplanacanalpanama" is a palindrome. |
| 28 | +``` |
| 29 | + |
| 30 | +**Example 2:** |
| 31 | + |
| 32 | +``` |
| 33 | +Input: s = "race a car" |
| 34 | +Output: false |
| 35 | +Explanation: "raceacar" is not a palindrome. |
| 36 | +``` |
| 37 | + |
| 38 | +**Example 3:** |
| 39 | + |
| 40 | +``` |
| 41 | +Input: s = " " |
| 42 | +Output: true |
| 43 | +Explanation: s is an empty string "" after removing non-alphanumeric characters. |
| 44 | +Since an empty string reads the same forward and backward, it is a palindrome. |
| 45 | +``` |
| 46 | + |
| 47 | +### Constraints |
| 48 | + |
| 49 | +- $ (1 <= s.length <= 2 * 10^5) $ |
| 50 | +- `s` consists only of printable ASCII characters. |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Solution for Valid Palindrome Problem |
| 55 | + |
| 56 | +### Approach |
| 57 | + |
| 58 | +This code is an implementation of a solution to determine if a given string is a palindrome. A string is considered a palindrome if it reads the same forwards and backwards, ignoring spaces, punctuation, and letter casing. |
| 59 | + |
| 60 | +The approach used in this solution is a two-pointer technique, where two pointers are maintained, one at the start of the string and the other at the end of the string. The two pointers move towards each other until they meet in the middle of the string. |
| 61 | + |
| 62 | +At each iteration of the while loop, the characters pointed to by the start and last pointers are checked. If either of the characters is not a letter or digit (e.g., a space or punctuation), the pointer is moved one step to the right (for start) or one step to the left (for last) until a letter or digit is found. |
| 63 | + |
| 64 | +If both characters are letters or digits, they are converted to lowercase and compared. If they are not equal, the function returns false, as the string is not a palindrome. If they are equal, both pointers are moved one step to the right and left, respectively. |
| 65 | + |
| 66 | +The while loop continues until the start pointer is greater than the last pointer, indicating that all the characters have been checked and that the string is a palindrome. The function then returns true. |
| 67 | + |
| 68 | +### Complexity |
| 69 | + |
| 70 | +- Time Complexity: O(n), where n is the length of the string. This is because, in the worst case, all characters in the string need to be checked once, so the number of operations is proportional to the length of the string. |
| 71 | +- Space Complexity: O(1), as no additional data structures are used, and only a constant amount of memory is required for the start and last pointers and a few variables. |
| 72 | + |
| 73 | +### Code |
| 74 | + |
| 75 | +#### Java |
| 76 | + |
| 77 | +```java |
| 78 | +class Solution { |
| 79 | + public boolean isPalindrome(String s) { |
| 80 | + if (s.isEmpty()) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + int start = 0; |
| 84 | + int last = s.length() - 1; |
| 85 | + while(start <= last) { |
| 86 | + char currFirst = s.charAt(start); |
| 87 | + char currLast = s.charAt(last); |
| 88 | + if (!Character.isLetterOrDigit(currFirst)) { |
| 89 | + start++; |
| 90 | + } else if (!Character.isLetterOrDigit(currLast)) { |
| 91 | + last--; |
| 92 | + } else { |
| 93 | + if (Character.toLowerCase(currFirst) != Character.toLowerCase(currLast)) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + start++; |
| 97 | + last--; |
| 98 | + } |
| 99 | + } |
| 100 | + return true; |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +#### Python |
| 106 | + |
| 107 | +```python |
| 108 | +class Solution: |
| 109 | + def isPalindrome(self, s: str) -> bool: |
| 110 | + if not s: |
| 111 | + return True |
| 112 | + start = 0 |
| 113 | + last = len(s) - 1 |
| 114 | + while start <= last: |
| 115 | + curr_first = s[start] |
| 116 | + curr_last = s[last] |
| 117 | + if not curr_first.isalnum(): |
| 118 | + start += 1 |
| 119 | + elif not curr_last.isalnum(): |
| 120 | + last -= 1 |
| 121 | + else: |
| 122 | + if curr_first.lower() != curr_last.lower(): |
| 123 | + return False |
| 124 | + start += 1 |
| 125 | + last -= 1 |
| 126 | + return True |
| 127 | +``` |
| 128 | + |
| 129 | +--- |
| 130 | + |
| 131 | +## References |
| 132 | + |
| 133 | +- **LeetCode Problem**: [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) |
| 134 | +- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/valid-palindrome/solution/) |
0 commit comments