Interview question 01.04. Palindrome arrangement

Keywords: Java Algorithm Interview

subject

Given a string, write a function to determine whether it is one of the permutations of a palindrome string.
Palindrome strings refer to words or phrases with the same positive and negative directions. Arrangement refers to the rearrangement of letters.
Palindrome strings are not necessarily words in the dictionary.

Example 1:

Input: "tactcoa"
Output: true (arranged with "tacocat", "atcocta", etc.)

Problem solving ideas

1. Use hashMap to count the occurrence frequency

thinking

  • The occurrence frequency of each character that can be palindrome is regular. At most, only one character can appear odd, and the occurrence times of other characters must be even!
  • Like: abbba or acbca

  • Use hashMap to count the frequency of characters
  • Finally, it is judged that as long as the occurrence frequency of more than one character is odd, it will return false, otherwise it will return true

code

public class Test {
    public static void main(String[] args) {
        String string = "aaabbdd";
        System.out.println(solution(string));
    }

    public static boolean solution(String s) {
        if (s.length() == 0){
            return true;
        }
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if (!map.containsKey(s.charAt(i))) {
                map.put(s.charAt(i), 0);
            }
            map.put(s.charAt(i), map.get(s.charAt(i)) + 1);
        }
        //At most one can be an odd number. If it is greater than 1, false will be returned
        int flag = 0;
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            if (entry.getValue() % 2 == 1) {
                flag++;
            }
            if (flag>1){
                return false;
            }
        }
        return true;
    }
}

performance

  • Both time complexity and space complexity are O(n)

1.5 you can also use set to judge!

thinking

  • Use set set to remove duplicate
  • Because the add method of set has a return value. If there are duplicate elements in set, false will be returned if put fails. This is why set a fixed value of Object instead of null for value in set
    Look at the source code of the add() method of set
    public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }

Because the put method of map returns null if it is not overwritten
If there is a duplicate key, the previous old value will be returned
The bottom layer of set is to use the Key of map to ensure the uniqueness of elements
If you insert an a into the set first, it will actually be saved in the map * * (the key is' a 'and the value is obj)**
If another a enters the set, it is equivalent to put ting it into the map, repeating the key, overwriting it, and returning the old value, that is, obj
At this time, the add method returns map.put(e, PRESENT)==null; Is false.

Look at the code

    public boolean solution(String s) {
        Set<Character> set = new HashSet<>();
        for (char ch : s.toCharArray()) {
            if (!set.add(ch)) {
                set.remove(ch);
            }
        }
        return set.size() <= 1;
    }

2. Use the stack to eliminate music~

thinking

  • First program the string char [], and sort it
  • Then check whether the top of the stack is the character. If not, check whether the stack space is greater than 1. If greater than 1, it indicates that there are multiple characters with odd occurrences
  • If it is this character, then Xiaole! pop the top element in the stack and view the next character.
  • Here, you should first judge whether it is empty, otherwise stack.peek() will report a null pointer when the stack is empty

code

public class Test {
    public static void main(String[] args) {
        String string = "aaabbdd";
        System.out.println(solution(string));
    }

    public static boolean solution(String s) {
        if (s.length() == 0) {
            return true;
        }
        char[] chars = s.toCharArray();
        Arrays.sort(chars);
        Stack<Character> stack = new Stack<>();
        for (char c : chars) {
        //Here, you should first judge whether it is empty, otherwise stack.peek() will report a null pointer when the stack is empty
            if (stack.size() == 0 || stack.peek() != c) {
                if (stack.size() > 1) {
                    return false;
                }
                stack.push(c);
            } else {
                stack.pop();
            }
        }
        if (stack.size() > 1) {
            return false;
        }
        return true;
    }
}

performance

Posted by TheDefender on Fri, 17 Sep 2021 04:24:44 -0700