返回介绍

solution / 2100-2199 / 2135.Count Words Obtained After Adding a Letter / README_EN

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

2135. Count Words Obtained After Adding a Letter

中文文档

Description

You are given two 0-indexed arrays of strings startWords and targetWords. Each string consists of lowercase English letters only.

For each string in targetWords, check if it is possible to choose a string from startWords and perform a conversion operation on it to be equal to that from targetWords.

The conversion operation is described in the following two steps:

  1. Append any lowercase letter that is not present in the string to its end.
    • For example, if the string is "abc", the letters 'd', 'e', or 'y' can be added to it, but not 'a'. If 'd' is added, the resulting string will be "abcd".
  2. Rearrange the letters of the new string in any arbitrary order.
    • For example, "abcd" can be rearranged to "acbd", "bacd", "cbda", and so on. Note that it can also be rearranged to "abcd" itself.

Return _the number of strings in _targetWords_ that can be obtained by performing the operations on any string of _startWords.

Note that you will only be verifying if the string in targetWords can be obtained from a string in startWords by performing the operations. The strings in startWords do not actually change during this process.

 

Example 1:

Input: startWords = ["ant","act","tack"], targetWords = ["tack","act","acti"]
Output: 2
Explanation:
- In order to form targetWords[0] = "tack", we use startWords[1] = "act", append 'k' to it, and rearrange "actk" to "tack".
- There is no string in startWords that can be used to obtain targetWords[1] = "act".
  Note that "act" does exist in startWords, but we must append one letter to the string before rearranging it.
- In order to form targetWords[2] = "acti", we use startWords[1] = "act", append 'i' to it, and rearrange "acti" to "acti" itself.

Example 2:

Input: startWords = ["ab","a"], targetWords = ["abc","abcd"]
Output: 1
Explanation:
- In order to form targetWords[0] = "abc", we use startWords[0] = "ab", add 'c' to it, and rearrange it to "abc".
- There is no string in startWords that can be used to obtain targetWords[1] = "abcd".

 

Constraints:

  • 1 <= startWords.length, targetWords.length <= 5 * 104
  • 1 <= startWords[i].length, targetWords[j].length <= 26
  • Each string of startWords and targetWords consists of lowercase English letters only.
  • No letter occurs more than once in any string of startWords or targetWords.

Solutions

Solution 1

class Solution:
  def wordCount(self, startWords: List[str], targetWords: List[str]) -> int:
    s = set()
    for word in startWords:
      mask = 0
      for c in word:
        mask |= 1 << (ord(c) - ord('a'))
      s.add(mask)

    ans = 0
    for word in targetWords:
      mask = 0
      for c in word:
        mask |= 1 << (ord(c) - ord('a'))
      for c in word:
        t = mask ^ (1 << (ord(c) - ord('a')))
        if t in s:
          ans += 1
          break
    return ans
class Solution {

  public int wordCount(String[] startWords, String[] targetWords) {
    Set<Integer> s = new HashSet<>();
    for (String word : startWords) {
      int mask = 0;
      for (char c : word.toCharArray()) {
        mask |= (1 << (c - 'a'));
      }
      s.add(mask);
    }
    int ans = 0;
    for (String word : targetWords) {
      int mask = 0;
      for (char c : word.toCharArray()) {
        mask |= (1 << (c - 'a'));
      }
      for (char c : word.toCharArray()) {
        int t = mask ^ (1 << (c - 'a'));
        if (s.contains(t)) {
          ++ans;
          break;
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int wordCount(vector<string>& startWords, vector<string>& targetWords) {
    unordered_set<int> s;
    for (auto& word : startWords) {
      int mask = 0;
      for (char c : word)
        mask |= (1 << (c - 'a'));
      s.insert(mask);
    }
    int ans = 0;
    for (auto& word : targetWords) {
      int mask = 0;
      for (char c : word)
        mask |= (1 << (c - 'a'));
      for (char c : word) {
        int t = mask ^ (1 << (c - 'a'));
        if (s.count(t)) {
          ++ans;
          break;
        }
      }
    }
    return ans;
  }
};
func wordCount(startWords []string, targetWords []string) int {
  s := make(map[int]bool)
  for _, word := range startWords {
    mask := 0
    for _, c := range word {
      mask |= (1 << (c - 'a'))
    }
    s[mask] = true
  }
  ans := 0
  for _, word := range targetWords {
    mask := 0
    for _, c := range word {
      mask |= (1 << (c - 'a'))
    }
    for _, c := range word {
      t := mask ^ (1 << (c - 'a'))
      if s[t] {
        ans++
        break
      }
    }
  }
  return ans
}

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

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

发布评论

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