給定一個字符串,判斷該字符串中是否可以通過重新排列組合,形成一個回文字符串。
示例 1:
輸入: "code"
輸出: false
示例 2:
輸入: "aab"
輸出: true
示例 3:
輸入: "carerac"
輸出: true
思路:set記錄,最后剩0個或1個。
public class Solution {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;}
}
?