Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
HAPPY CASE
Input: s = "A man, a plan, a canal: Panama"
Output: true
Input: s = "race a car"
Output: false
EDGE CASE
Input: s = " "
Output: true
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Array/Strings, common solution patterns include:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Start traversing inwards, from both ends of the input string, and we can expect to see the same characters, in the same order.
1. What we can do is take two pointer variables, start and end and point them with the two ends of the input string.
2. Now move the start pointer to right so it points to a alphanumeric character. Similarly move end pointer to left so it also points to a alphanumeric character.
3. Now check if both the characters are same or not (ignoring cases):
If it is not equal then we know string is not a valid palindrome, hence return false.
Else continue to next iteration and repeat the same process of moving both pointers to point to next alphanumeric character till start<end.
4. After loop finishes, the string is said to be palindrome, hence return true.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution:
def isPalindrome(self, s: str) -> bool:
# Use two pointer variables: one at left, one at right
l = 0; r = len(s) - 1;
# Handle all case differences
s = s.lower()
# Move both l and r until they meet
while l < r:
# Skip none alpha numerical letters
while l < r and not s[l].isalnum():
l += 1
while l < r and not s[r].isalnum():
r -= 1
# Return false when left if greater than right or not equal
if l > r or s[l] != s[r]:
return False
# iterate
l += 1
r -= 1
return True
class Solution {
public boolean isPalindrome(String s) {
// create pointers to the front and back of the string
int back = s.length()-1;
int front = 0;
// transform the string to upperCase
s = s.toUpperCase();
// while front pointer != back pointer
while (front < back){
// move until we find a alphanumeric character
while (!Character.isLetterOrDigit(s.charAt(front)) && front < back){
front++;
}
while (!Character.isLetterOrDigit(s.charAt(back)) && back > front){
back--;
}
// make sure they are the same
if (s.charAt(front) != s.charAt(back)){
return false;
}
back--;
front++;
}
return true;
}
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N represents the number of characters in the string.