列表

详情


266. 回文排列

给定一个字符串,判断该字符串中是否可以通过重新排列组合,形成一个回文字符串。

示例 1:

输入: "code"
输出: false

示例 2:

输入: "aab"
输出: true

示例 3:

输入: "carerac"
输出: true

相似题目

最长回文子串

有效的字母异位词

回文排列 II

最长回文串

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class Solution { public: bool canPermutePalindrome(string s) { } };

golang 解法, 执行用时: 0 ms, 内存消耗: 1.8 MB, 提交时间: 2023-10-15 18:42:40

func canPermutePalindrome(s string) bool {
    var bs []byte = []byte(s)
    ns := make([]int, len(s))
    for k, v := range bs {
        ns[k] = int(v)
    }
    sort.Ints(ns)
    i := 0
    j := len(s)-1
    for i<j {
        if ns[j] == ns[j-1] {
            j-=2
        } else if ns[i] == ns[i+1] {
            i+=2
        } else {
            return false
        }
    }
    return true
}

func canPermutePalindrome2(s string) bool {
    var bs []byte = []byte(s)
    ns := make(map[byte]int, len(s))
    for _, v := range bs {
        ns[v]++
    }
    fmt.Println(ns)
    find := 0
    for _, v := range ns {
        if v % 2 == 0 {
            continue
        } else if find == 0 {
            find = 1
        } else {
            return false
        }
    }
    return true
}

python3 解法, 执行用时: 40 ms, 内存消耗: 16.1 MB, 提交时间: 2023-10-15 18:41:47

class Solution:
    def canPermutePalindrome(self, s: str) -> bool:
        dic = {}
        for i in s:
            dic[i] = dic.get(i,0)+1
        count = 0
        for i in dic.values():
            if i%2==1:
                count+=1
        return count<2

java 解法, 执行用时: 1 ms, 内存消耗: 39.5 MB, 提交时间: 2023-10-15 18:41:14

public class Solution {
   public boolean canPermutePalindrome1(String s) {
       int count = 0;
       for (char i = 0; i < 128 && count <= 1; i++) {
           int ct = 0;
           for (int j = 0; j < s.length(); j++) {
               if (s.charAt(j) == i)
                   ct++;
           }
           count += ct % 2;
       }
       return count <= 1;
   }

    public boolean canPermutePalindrome2(String s) {
        HashMap < Character, Integer > map = new HashMap < > ();
        for (int i = 0; i < s.length(); i++) {
            map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
        }
        int count = 0;
        for (char key: map.keySet()) {
            count += map.get(key) % 2;
        }
        return count <= 1;
    }

   public boolean canPermutePalindrome3(String s) {
       int[] map = new int[128];
       for (int i = 0; i < s.length(); i++) {
           map[s.charAt(i)]++;
       }
       int count = 0;
       for (int key = 0; key < map.length && count <= 1; key++) {
           count += map[key] % 2;
       }
       return count <= 1;
   }


   public boolean canPermutePalindrome4(String s) {
       int[] map = new int[128];
       int count = 0;
       for (int i = 0; i < s.length(); i++) {
           map[s.charAt(i)]++;
           if (map[s.charAt(i)] % 2 == 0)
               count--;
           else
               count++;
       }
       return count <= 1;
   }

   public boolean canPermutePalindrome(String s) {
       Set < Character > set = new HashSet < > ();
       for (int i = 0; i < s.length(); i++) {
           if (!set.add(s.charAt(i)))
               set.remove(s.charAt(i));
       }
       return set.size() <= 1;
   }
}

上一题