返回介绍

solution / 0800-0899 / 0893.Groups of Special-Equivalent Strings / README_EN

发布于 2024-06-17 01:03:33 字数 4915 浏览 0 评论 0 收藏 0

893. Groups of Special-Equivalent Strings

中文文档

Description

You are given an array of strings of the same length words.

In one move, you can swap any two even indexed characters or any two odd indexed characters of a string words[i].

Two strings words[i] and words[j] are special-equivalent if after any number of moves, words[i] == words[j].

  • For example, words[i] = "zzxy" and words[j] = "xyzz" are special-equivalent because we may make the moves "zzxy" -> "xzzy" -> "xyzz".

A group of special-equivalent strings from words is a non-empty subset of words such that:

  • Every pair of strings in the group are special equivalent, and
  • The group is the largest size possible (i.e., there is not a string words[i] not in the group such that words[i] is special-equivalent to every string in the group).

Return _the number of groups of special-equivalent strings from _words.

 

Example 1:

Input: words = ["abcd","cdab","cbad","xyzz","zzxy","zzyx"]
Output: 3
Explanation: 
One group is ["abcd", "cdab", "cbad"], since they are all pairwise special equivalent, and none of the other strings is all pairwise special equivalent to these.
The other two groups are ["xyzz", "zzxy"] and ["zzyx"].
Note that in particular, "zzxy" is not special equivalent to "zzyx".

Example 2:

Input: words = ["abc","acb","bac","bca","cab","cba"]
Output: 3

 

Constraints:

  • 1 <= words.length <= 1000
  • 1 <= words[i].length <= 20
  • words[i] consist of lowercase English letters.
  • All the strings are of the same length.

Solutions

Solution 1

class Solution:
  def numSpecialEquivGroups(self, words: List[str]) -> int:
    s = {''.join(sorted(word[::2]) + sorted(word[1::2])) for word in words}
    return len(s)
class Solution {
  public int numSpecialEquivGroups(String[] words) {
    Set<String> s = new HashSet<>();
    for (String word : words) {
      s.add(convert(word));
    }
    return s.size();
  }

  private String convert(String word) {
    List<Character> a = new ArrayList<>();
    List<Character> b = new ArrayList<>();
    for (int i = 0; i < word.length(); ++i) {
      char ch = word.charAt(i);
      if (i % 2 == 0) {
        a.add(ch);
      } else {
        b.add(ch);
      }
    }
    Collections.sort(a);
    Collections.sort(b);
    StringBuilder sb = new StringBuilder();
    for (char c : a) {
      sb.append(c);
    }
    for (char c : b) {
      sb.append(c);
    }
    return sb.toString();
  }
}
class Solution {
public:
  int numSpecialEquivGroups(vector<string>& words) {
    unordered_set<string> s;
    for (auto& word : words) {
      string a = "", b = "";
      for (int i = 0; i < word.size(); ++i) {
        if (i & 1)
          a += word[i];
        else
          b += word[i];
      }
      sort(a.begin(), a.end());
      sort(b.begin(), b.end());
      s.insert(a + b);
    }
    return s.size();
  }
};
func numSpecialEquivGroups(words []string) int {
  s := map[string]bool{}
  for _, word := range words {
    a, b := []rune{}, []rune{}
    for i, c := range word {
      if i&1 == 1 {
        a = append(a, c)
      } else {
        b = append(b, c)
      }
    }
    sort.Slice(a, func(i, j int) bool {
      return a[i] < a[j]
    })
    sort.Slice(b, func(i, j int) bool {
      return b[i] < b[j]
    })
    s[string(a)+string(b)] = true
  }
  return len(s)
}

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

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

发布评论

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