125. Valid Palindrome

125. Valid Palindrome

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.

Method 1:

Step 1:

Take two pointers start=0, last = string length.

Step 2:

while loop start < last (0 < string length)

currentFirst = character at start position

currentLast = character at last position

Step 3:

checking conditions

Here if the character at start or last is any special character (space, “ , ”, . , : ,…..)

it skip the checking the characters are same or not and increment the start and decrement the last.

Character.isLetterOrDigit(): It check the character is alphabet or digit.

if any one is not matched than return false otherwise return true.

CODE:

public class ValidPalindrome {
  public static void main(String[] args) {
    String s = "A man, a plan, a canal: Panama";
    System.out.println("Is valid palindrome: "+isPalindrome(s));
  }

  public static boolean isPalindrome(String s) {
    int start =0,last =s.length()-1;
    while(start < last){
      char currentFirst = s.charAt(start);
      char currentLast = s.charAt(last);
      if (!Character.isLetterOrDigit(currentFirst )) {
        start++;
      } else if(!Character.isLetterOrDigit(currentLast)) {
        last--;
      }
      else 
      {
        if (Character.toLowerCase(currentFirst) != Character.toLowerCase(currentLast)) {
          return false;
        }
        start++;
        last--;
      }
    }
    return true;
  }
}
Is valid palindrome: true

Solution Link: https://leetcode.com/problems/valid-palindrome/submissions/1109610225/

Method 2:

Step 1:

String s = "A man, a plan, a canal: Panama";

convert string to lower case using toLowerCase() method.

Remove all the special characters and spaces in a string.

Create a list and add all the characters and digits in a string .

Step 2:

After step 1 the string “amanaplanacanalpanama”

checking the string is palindrome or not.

CODE:

public class ValidPalindrome {
  public static void main(String[] args) {
    String s = "A man, a plan, a canal: Panama";
    System.out.println("Is valid palindrome: "+isPalindrome(s));
  }

  public static boolean isPalindrome(String s) {
    List<Character> list = new ArrayList<>();

    s = s.toLowerCase();
    //step 1
    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      if (Character.isLetterOrDigit(c)) {
        list.add(c);
      }
    }

    //step 2.check if Palindrome or not
    for (int i = 0; i < list.size() / 2; i++) {
      if (list.get(i) != list.get(list.size() - 1 - i)) {
        return false;
      }
    }

    return true;
  }
}
Is valid palindrome: true

Solution Link: https://leetcode.com/problems/valid-palindrome/submissions/1110036377/

Thank you for reading.

Happy Coding :)

Did you find this article valuable?

Support Sirisha Challagiri by becoming a sponsor. Any amount is appreciated!