Algorithms Verify Palindrome String-Palindrome Chain List

Posted by JeremyTiki on Mon, 13 Jan 2020 01:49:09 +0100

00125 Verify Palindrome String

Title Description

Given a string, verify that it is a palindrome string, considering only alphabetic and numeric characters, ignoring case.

Description: In this topic, we define an empty string as a valid palindrome string.

Example 1:

Input:'A man, a plan, a canal: Panama'
Output: true

Example 2:

Input:'race a car'
Output: false

Force Button Address

shopping spree

Use double pointer

This question is provided by WeChat Public Ape Brush Title. Errors are welcome to correct.

  • Scan from the beginning to the end (only letters and numbers are scanned) to compare if the corresponding positions are the same letters (uniform uppercase/lowercase comparisons, taking into account case).
  • Different corresponding bits indicate that they are not palindrome strings until the end of the comparison, and equal means palindrome strings.
/**
 *  WeChat Public Number "Ape Brush Title"
 */
class Solution {
    public boolean isPalindrome(String s) {
        int i = 0;
        int j = s.length() - 1;
        while(i <= j) {
            char ci = s.charAt(i);
            char cj = s.charAt(j);
            if(!Character.isLetterOrDigit(ci)){
                i ++;
            } else if(!Character.isLetterOrDigit(cj)){
                j --;
            } else {
                // Determine equality (case insensitive)
                if(Character.toLowerCase(ci) != Character.toLowerCase(cj)){
                    return false;
                }
                j --;
                i ++;
            }
        }
        return true;
    }
}

By filtering letters/numbers for comparison during traversal, the double pointer above can also be used to string neatly (regularly replace characters other than letters and numbers, unify uppercase/lowercase), and then compare at the beginning and end.

/**
 *  WeChat Public Number "Ape Brush Title"
 */
class Solution {
    public boolean isPalindrome(String s) {
        s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
        for(int i = 0; i < s.length() / 2; i ++){
            if(s.charAt(i) != s.charAt(s.length() - 1 -i)){
                return false;
            }
        }
        return true;
    }
}

00234 Palindrome Chain List

Title Description

Please determine if a list is a palindrome list.

Example 1:

Input: 1->2
 Output: false

Example 2:

Input: 1->2->2->1
 Output: true

Advanced:

  • Can you solve this problem with O(n) time complexity and O(1) space complexity?

Force Button Address

shopping spree

Utilize Stack

This question is provided by WeChat Public Ape Brush Title. Errors are welcome to correct.

Use the stack's characteristics (FIFO) to save the list of chains, and then compare them one by one from the list of chains as the elements move out of the stack.

/**
 * WeChat Public Number "Ape Brush Title"
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {
        Stack<Integer> stack = new Stack<>();
        ListNode curr = head;
        while(curr != null){
            stack.add(curr.val);
            curr = curr.next;
        }
        curr = head;
        while(!stack.isEmpty()){
            int val = stack.pop();
            if(val != curr.val){
                return false;
            }
            curr = curr.next;
        }
        return true;
    }
}

Utilize List

This question is provided by WeChat Public Ape Brush Title. Errors are welcome to correct.

Now that you can use the stack, you can also use lists as well. Chain lists store lists, and then use the properties of lists to iterate through the first and last comparisons in turn.

/**
 * WeChat Public Number "Ape Brush Title"
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {
        List<Integer> list = new ArrayList<>();
        ListNode curr = head;
        while(curr != null){
            list.add(curr.val);
            curr = curr.next;
        }
        for (int i = 0; i < list.size() / 2; i++){
            if((int)list.get(i) != (int)list.get(list.size() - 1 -i)){
                return false;
            }
        }
        return true;
    }
}

Double Pointer

This question is provided by WeChat Public Ape Brush Title. Errors are welcome to correct.

  • Fast and slow pointers move twice, full pointers move once, and when the fast pointer moves to the end, the slow pointer is just in the middle of the list.
  • Reverse the slow pointer, comparing it with the chain header elements in turn, and encounter different elements to represent non-palindromic tables until the end.
/**
 * WeChat Public Number "Ape Brush Title"
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        fast = head;
        slow = reverse(slow);
        while (slow != null) {
            if (slow.val != fast.val) {
                return false;
            }
            slow = slow.next;
            fast = fast.next;
        }
        return true;
    }

    // Chain list inversion
    public ListNode reverse(ListNode head) {
        ListNode target = null;
        ListNode current = head;
        while (current != null) {
            ListNode nextTemp = current.next;
            current.next = target;
            target = current;
            current = nextTemp;
        }
        return target;
    }
}

Topics: Programming