From c34a34591afabb3ba58fb45896b4fcba92deb019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Mon, 11 Apr 2022 13:36:13 +0800 Subject: [PATCH] =?UTF-8?q?266:=E5=9B=9E=E6=96=87=E6=8E=92=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/PalindromePermutation.java | 49 +++++++++++++++++++ .../cn/doc/content/PalindromePermutation.md | 17 +++++++ 2 files changed, 66 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/PalindromePermutation.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/PalindromePermutation.md diff --git a/src/main/java/leetcode/editor/cn/PalindromePermutation.java b/src/main/java/leetcode/editor/cn/PalindromePermutation.java new file mode 100644 index 0000000..564ab12 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/PalindromePermutation.java @@ -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) + +} diff --git a/src/main/java/leetcode/editor/cn/doc/content/PalindromePermutation.md b/src/main/java/leetcode/editor/cn/doc/content/PalindromePermutation.md new file mode 100644 index 0000000..222ea19 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/PalindromePermutation.md @@ -0,0 +1,17 @@ +

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

+ +

示例 1:

+ +
输入: "code"
+输出: false
+ +

示例 2:

+ +
输入: "aab"
+输出: true
+ +

示例 3:

+ +
输入: "carerac"
+输出: true
+
Related Topics
  • 位运算
  • 哈希表
  • 字符串

  • 👍 59
  • 👎 0
  • \ No newline at end of file