[LeetCode#125]Valid Palindrome

The problem:

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

My analysis:

This problem is not difficult, but we need to understand clearly about which cases are valid.

1. In this problem, we only recognize the character in 'a - z' , 'A - Z', '0 - 9' as valid characters. We treat other characters as invalid, we don't take them into consideration, just skip them.

We can separate a function to achieve this goal:

String temp = s.toLowerCase();

...

private boolean isValid(char c) {

    if (c >= 'a' && c<= 'z' || c >= '0' && c <= '9')

        return true;

    return false;

}

Skill: we always separte a function the check if a character is valid.



2. Since we need to skip invalid characters, we may move poiters in the outside loop. 

This kind of move is very dangerous, we should be very careful in the inner movemoent, they might exceed the boundary!!!

while (ptr1 < ptr2) {

    while (!isValid(temp.charAt(ptr1))) {

        ptr1++; //it's dangerous!!!

        ...

    }

}

How to fix it? once we move the pointer, we should make check over the pointer. The pointer could reach following state!

1. stop at a valid character, pointer1's position is different from pointer2's position.

2. pointer1 is in collision with pointer2(at the same location).



In situation 1, we could continue the invariant to check.

In situation 2, we could directly return true, because:

2.1 the previous checking over ptr1 and ptr2 must meet the standard of Palindrome, otherwise, we have already return false.

2.2 when pointer1 is in collision with pointer2, they at the same position.

    tmep.charAt(ptr1) == temp.charAt(ptr2);

My solution:

public class Solution {

    public boolean isPalindrome(String s) {

        if (s == null || s.length() == 0)

            return true;

        String temp = s.toLowerCase();

        int ptr1 = 0;

        int ptr2 = temp.length() - 1;

        while (ptr1 < ptr2) {//note the space and other commons, qutoes are allowed!

            while (!isValid(temp.charAt(ptr1))) {

                ptr1++;

                if (ptr1 == ptr2) return true;

            }

            while (!isValid(temp.charAt(ptr2))) {

                ptr2--;

                if (ptr1 == ptr2) return true;

            }

            if (temp.charAt(ptr1) != temp.charAt(ptr2))

                return false;

            ptr1++;

            ptr2--;

        }

        return true;

    }

    

    private boolean isValid(char c) {

        if (c >= 'a' && c<= 'z' || c >= '0' && c <= '9')

            return true;

        return false;

    }

}

My improved analysis:

In fact, the checking

if (ptr1 == ptr2) return true;

is the same as the outer while loop checking: while (ptr1 < ptr2), 

we could include the inner checking into the outside checking, by adjusting the codes a little.

while (!isValid(temp.charAt(ptr1))) {

    ptr1++;

    continue;

}

What a beautiful way!!!

My improved solution:

public class Solution {

    public boolean isPalindrome(String s) {

        if (s == null || s.length() == 0)

            return true;

        String temp = s.toLowerCase();

        int ptr1 = 0;

        int ptr2 = temp.length() - 1;

        while (ptr1 < ptr2) {//note the space and other commons, qutoes are allowed!

            while (!isValid(temp.charAt(ptr1))) {

                ptr1++;

                continue;

            }

            while (!isValid(temp.charAt(ptr2))) {

                ptr2--;

                continue;

            }

            if (temp.charAt(ptr1) != temp.charAt(ptr2))

                return false;

            ptr1++;

            ptr2--;

        }

        return true;

    }

    

    private boolean isValid(char c) {

        if (c >= 'a' && c<= 'z' || c >= '0' && c <= '9')

            return true;

        return false;

    }

}

 

你可能感兴趣的:(LeetCode)