zoukankan      html  css  js  c++  java
  • [LeetCode 266] Palindrome Permutation

    Palindrome Permutation

    • palindrome中,字符均是成对出现的(除了当字符串长度是单数时的中间字母)
    • 创建一个set对象
    • 遍历字符串,当遇到一个字符的时候检测set中有没有该字符。
      • 如果有则将该字符从set中删除
      • 否则,将该字符添加到set
    • 最后检测set中元素的个数
      • 个数小于等于1时,说明满足条件。

    Implementation

    public class Solution {
        public boolean canPermutePalindrome(String s) {
            HashSet<Character> set = new HashSet<Character>();
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (!set.add(c)) { // if the set contains c, add() will return false
                    set.remove(c);
                }
            }
            return set.size() <= 1;
        }
    }
    

    Palindrome Permutation II

    • 使用Palindrome Permutation的方法判断给定的字符串是否可以构造palindrome字符串
    • 选择构造palindrome一半所需要的字符和mid字符
    • 产生permutation

    Implementation

    public class Solution {
        public List<String> generatePalindromes(String s) {
            ArrayList<Character> charList = new ArrayList<Character>();
            HashSet<Character> set = new HashSet<Character>();
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (!set.add(c)) {
                    set.remove(c);
                    charList.add(c);
                }
            }
            Collections.sort(charList);
            List<String> result = new ArrayList<String>();
            if (set.size() > 1) {
                return result;
            }
            // 使用iterator遍历set
            Iterator<Character> iter = set.iterator();
            String mid = "";
            if (iter.hasNext()) {
                mid += iter.next();
            }
            generatePermutations(result, charList, new StringBuilder(), mid);
            return result;
        }
    
        private void generatePermutations(List<String> result, ArrayList<Character> charList, StringBuilder sb, String mid) {
            if (charList.size() == 0) {
                result.add(sb.toString() + mid + sb.reverse().toString());
                sb.reverse();
                return;
            }
    
            for (int i = 0; i < charList.size(); i++) {
                if (i == 0 || charList.get(i) != charList.get(i - 1)) {
                    char c = charList.get(i);
                    sb.append(c);
                    charList.remove(i);
                    generatePermutations(result, charList, sb, mid);
                    sb.deleteCharAt(sb.length() - 1);
                    charList.add(i, c);
                }
            }
        }
    }
    

    Valid Palindrome

    Implementation

    public class Solution {
        public boolean isPalindrome(String s) {
            int left = 0;
            int right = s.length() - 1;
            while (left < right) {
                if (Character.isLetterOrDigit(s.charAt(left)) && Character.isLetterOrDigit(s.charAt(right))) {
                    if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right)))
                        return false;
                    left++;
                    right--;
                }
                else {
                    if (!Character.isLetterOrDigit(s.charAt(left)))
                        left++;
                    if (!Character.isLetterOrDigit(s.charAt(right)))
                        right--;
                }
    
            }
            return true;
        }
    }
    

    Palindrome Partitioning

    • time complexity: n!

    Implementation

    public class Solution {
        public List<List<String>> partition(String s) {
            List<List<String>> result = new ArrayList<List<String>>();
            List<String> list = new ArrayList<String>();
            partition(s, 0, list, result);
            return result;
        }
    
        private void partition(String s, int start, List<String> list, List<List<String>> result) {
            if (start == s.length()) {
                result.add(new ArrayList<String>(list));
                return;
            }
    
            for (int i = start; i < s.length(); i++) {
                if (isPalindrome(s, start, i)) {
                    // pay attention to parameter of substring()
                    list.add(s.substring(start, i + 1));
                    partition(s, i + 1, list, result);
                    list.remove(list.size() - 1);
                }
            }
        }
    
        private boolean isPalindrome(String s, int left, int right) {
            while (left <= right) {
                if (s.charAt(left) != s.charAt(right)) {
                    return false;
                }
                // don't forget move pointers
                left++;
                right--;
            }
            return true;
        }
    }
    
  • 相关阅读:
    LeetCode 326. Power of Three
    LeetCode 324. Wiggle Sort II
    LeetCode 322. Coin Change
    LeetCode 321. Create Maximum Number
    LeetCode 319. Bulb Switcher
    LeetCode 318. Maximum Product of Word Lengths
    LeetCode 310. Minimum Height Trees (DFS)
    个人站点大开发!--起始篇
    LeetCode 313. Super Ugly Number
    LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (DP)
  • 原文地址:https://www.cnblogs.com/Victor-Han/p/5190212.html
Copyright © 2011-2022 走看看