266:回文排列
This commit is contained in:
parent
a1add8f320
commit
c34a34591a
49
src/main/java/leetcode/editor/cn/PalindromePermutation.java
Normal file
49
src/main/java/leetcode/editor/cn/PalindromePermutation.java
Normal 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)
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
<p>给定一个字符串,判断该字符串中是否可以通过重新排列组合,形成一个回文字符串。</p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong> <code>"code"</code>
|
||||
<strong>输出:</strong> false</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong> <code>"aab"</code>
|
||||
<strong>输出:</strong> true</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong> <code>"carerac"</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>
|
Loading…
Reference in New Issue
Block a user