返回介绍

lcci / 01.04.Palindrome Permutation / README_EN

发布于 2024-06-17 01:04:43 字数 4525 浏览 0 评论 0 收藏 0

01.04. Palindrome Permutation

中文文档

Description

Given a string, write a function to check if it is a permutation of a palin­ drome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.

 

Example1:


Input: "tactcoa"

Output: true(permutations: "tacocat"、"atcocta", etc.)

Solutions

Solution 1: Hash Table

We use a hash table $cnt$ to store the occurrence count of each character. If more than $1$ character has an odd count, then it is not a palindrome permutation.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.

class Solution:
  def canPermutePalindrome(self, s: str) -> bool:
    cnt = Counter(s)
    return sum(v & 1 for v in cnt.values()) < 2
class Solution {
  public boolean canPermutePalindrome(String s) {
    Map<Character, Integer> cnt = new HashMap<>();
    for (int i = 0; i < s.length(); ++i) {
      cnt.merge(s.charAt(i), 1, Integer::sum);
    }
    int sum = 0;
    for (int v : cnt.values()) {
      sum += v & 1;
    }
    return sum < 2;
  }
}
class Solution {
public:
  bool canPermutePalindrome(string s) {
    unordered_map<char, int> cnt;
    for (auto& c : s) {
      ++cnt[c];
    }
    int sum = 0;
    for (auto& [_, v] : cnt) {
      sum += v & 1;
    }
    return sum < 2;
  }
};
func canPermutePalindrome(s string) bool {
  vis := map[rune]bool{}
  cnt := 0
  for _, c := range s {
    if vis[c] {
      vis[c] = false
      cnt--
    } else {
      vis[c] = true
      cnt++
    }
  }
  return cnt < 2
}
function canPermutePalindrome(s: string): boolean {
  const set = new Set<string>();
  for (const c of s) {
    if (set.has(c)) {
      set.delete(c);
    } else {
      set.add(c);
    }
  }
  return set.size <= 1;
}
use std::collections::HashSet;

impl Solution {
  pub fn can_permute_palindrome(s: String) -> bool {
    let mut set = HashSet::new();
    for c in s.chars() {
      if set.contains(&c) {
        set.remove(&c);
      } else {
        set.insert(c);
      }
    }
    set.len() <= 1
  }
}

Solution 2: Another Implementation of Hash Table

We use a hash table $vis$ to store whether each character has appeared. If it has appeared, we remove the character from the hash table; otherwise, we add the character to the hash table.

Finally, we check whether the number of characters in the hash table is less than $2$. If it is, then it is a palindrome permutation.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string.

class Solution:
  def canPermutePalindrome(self, s: str) -> bool:
    vis = set()
    for c in s:
      if c in vis:
        vis.remove(c)
      else:
        vis.add(c)
    return len(vis) < 2
class Solution {
  public boolean canPermutePalindrome(String s) {
    Set<Character> vis = new HashSet<>();
    for (int i = 0; i < s.length(); ++i) {
      char c = s.charAt(i);
      if (!vis.add(c)) {
        vis.remove(c);
      }
    }
    return vis.size() < 2;
  }
}
class Solution {
public:
  bool canPermutePalindrome(string s) {
    unordered_set<char> vis;
    for (auto& c : s) {
      if (vis.count(c)) {
        vis.erase(c);
      } else {
        vis.insert(c);
      }
    }
    return vis.size() < 2;
  }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文