Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

Valid Palindrome (easy)

Problem Statement

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: sentence = "A man, a plan, a canal, Panama!"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.

Example 2:

Input: sentence = "Was it a car or a cat I saw?"
Output: true

.....

.....

.....

Like the course? Get enrolled and start learning!
T

Thai Minh

· 3 years ago

class Solution:   def isPalindrome(self, s: str) -> bool:     # TODO: Write your code here     str_array = []     for char in s:       if char.isalpha() or char.isdigit():         str_array.append(char.lower())     front, back = 0, len(str_array) - 1     while front < back:       if str_array[front] != str_array[back]:         return False       front += 1       back -= 1     return True
Akshay Hedau

Akshay Hedau

· 3 years ago

class Solution {   public boolean isPalindrome(String s) {     // TODO: Write your code here     s = s.toLowerCase();     int st = 0;     s = s.toLowerCase();     int ed = s.length() - 1;     char[] curS = s.toCharArray();     while (st < ed) {       char curSt = curS[st];       char curEd = curS[ed];       if (curSt > 'a' && curSt < 'z' && curEd > 'a' && curEd < 'z') {         if (curSt != curEd) return false;         st++;         ed--;       } else if (curSt > 'a' && curSt < 'z') {         ed--;       } else if (curEd > 'a' && curEd < 'z') {         st++;       } else {         st++;         ed--;       }     }     return true;   } }
Amber Wolf

Amber Wolf

· a year ago

public bool IsPalindrome(string s) { // use Regex to easily remove special characters and spaces from our string var trimmedString = Regex.Replace(s, "[^a-zA-Z0-9]", "").ToLower(); // create an array from the string so we can iterate through it trimmedString.ToCharArray(); // we need to track both the Forward facing and Backward facing iterations of this array so I am initializing the index starting at the end of the array here. int j = trimmedString.Length - 1; // each time we loop through our array we will increase i and decrease j to compare both the forward and backward letter of the string. for (int i = 0; i < trimmedString.Length; i++, j--) { if (trimmedString[i] != trimmedString[j])
Ana Calin

Ana Calin

· a year ago

Here's my solution for the palindrome problem that compiles and runs successfully on my editor:

package main import ( "fmt" "regexp" "strings" ) type Solution struct { } func (sol *Solution) isPalindrome(s string) bool { first, last := 0, len(s)-1 for first < last { if s[first] != s[last] { return false } first++ last-- } return true } func normalise(s string) string { toLowerStr := strings.ToLower(s) reg := regexp.MustCompile(`[^a-z]`) transformedStr := reg.ReplaceAllString(toLowerStr, "") return transformedStr } func main() { str := "A man, a plan, a canal, Panama!" normalisedStr := normalise(str) solution := Solution{} fmt.Println("Is my string a palindrome?", solution.isPalindrome(normalisedStr)) }

However

Sushant Gautam

Sushant Gautam

· 7 days ago

class Solution: def isPalindrome(self, s: str) -> bool: cleaned_l = [char.lower() for char in s if char.isalpha() or char.isdigit()] # two pointer l = 0 r = len(cleaned_l) - 1 while l < r: if cleaned_l[l] != cleaned_l[r]: return False l += 1 r -= 1 return True