266:回文排列

This commit is contained in:
轩辕龙儿 2022-04-11 13:36:13 +08:00
parent a1add8f320
commit c34a34591a
2 changed files with 66 additions and 0 deletions

View File

@ -0,0 +1,49 @@
//给定一个字符串判断该字符串中是否可以通过重新排列组合形成一个回文字符串
//
// 示例 1
//
// 输入: "code"
//输出: false
//
// 示例 2
//
// 输入: "aab"
//输出: true
//
// 示例 3
//
// 输入: "carerac"
//输出: true
// Related Topics 位运算 哈希表 字符串 👍 59 👎 0
package leetcode.editor.cn;
//266:回文排列
public class PalindromePermutation {
public static void main(String[] args) {
Solution solution = new PalindromePermutation().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean canPermutePalindrome(String s) {
int[] arrs = new int[26];
for (char ch : s.toCharArray()) {
arrs[ch - 'a']++;
}
int count = 0;
for (int i = 0; i < 26; i++) {
if (arrs[i] % 2 == 1) {
if (count == 1) {
return false;
}
count++;
}
}
return true;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,17 @@
<p>给定一个字符串,判断该字符串中是否可以通过重新排列组合,形成一个回文字符串。</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong> <code>&quot;code&quot;</code>
<strong>输出:</strong> false</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong> <code>&quot;aab&quot;</code>
<strong>输出:</strong> true</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong> <code>&quot;carerac&quot;</code>
<strong>输出:</strong> true</pre>
<div><div>Related Topics</div><div><li>位运算</li><li>哈希表</li><li>字符串</li></div></div><br><div><li>👍 59</li><li>👎 0</li></div>