返回介绍

solution / 2400-2499 / 2452.Words Within Two Edits of Dictionary / README_EN

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

2452. Words Within Two Edits of Dictionary

中文文档

Description

You are given two string arrays, queries and dictionary. All words in each array comprise of lowercase English letters and have the same length.

In one edit you can take a word from queries, and change any letter in it to any other letter. Find all words from queries that, after a maximum of two edits, equal some word from dictionary.

Return_ a list of all words from _queries_, __that match with some word from _dictionary_ after a maximum of two edits_. Return the words in the same order they appear in queries.

 

Example 1:

Input: queries = ["word","note","ants","wood"], dictionary = ["wood","joke","moat"]
Output: ["word","note","wood"]
Explanation:
- Changing the 'r' in "word" to 'o' allows it to equal the dictionary word "wood".
- Changing the 'n' to 'j' and the 't' to 'k' in "note" changes it to "joke".
- It would take more than 2 edits for "ants" to equal a dictionary word.
- "wood" can remain unchanged (0 edits) and match the corresponding dictionary word.
Thus, we return ["word","note","wood"].

Example 2:

Input: queries = ["yes"], dictionary = ["not"]
Output: []
Explanation:
Applying any two edits to "yes" cannot make it equal to "not". Thus, we return an empty array.

 

Constraints:

  • 1 <= queries.length, dictionary.length <= 100
  • n == queries[i].length == dictionary[j].length
  • 1 <= n <= 100
  • All queries[i] and dictionary[j] are composed of lowercase English letters.

Solutions

Solution 1

class Solution:
  def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
    ans = []
    for s in queries:
      for t in dictionary:
        if sum(a != b for a, b in zip(s, t)) < 3:
          ans.append(s)
          break
    return ans
class Solution {
  public List<String> twoEditWords(String[] queries, String[] dictionary) {
    List<String> ans = new ArrayList<>();
    int n = queries[0].length();
    for (var s : queries) {
      for (var t : dictionary) {
        int cnt = 0;
        for (int i = 0; i < n; ++i) {
          if (s.charAt(i) != t.charAt(i)) {
            ++cnt;
          }
        }
        if (cnt < 3) {
          ans.add(s);
          break;
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<string> twoEditWords(vector<string>& queries, vector<string>& dictionary) {
    vector<string> ans;
    for (auto& s : queries) {
      for (auto& t : dictionary) {
        int cnt = 0;
        for (int i = 0; i < s.size(); ++i) cnt += s[i] != t[i];
        if (cnt < 3) {
          ans.emplace_back(s);
          break;
        }
      }
    }
    return ans;
  }
};
func twoEditWords(queries []string, dictionary []string) (ans []string) {
  for _, s := range queries {
    for _, t := range dictionary {
      cnt := 0
      for i := range s {
        if s[i] != t[i] {
          cnt++
        }
      }
      if cnt < 3 {
        ans = append(ans, s)
        break
      }
    }
  }
  return
}
function twoEditWords(queries: string[], dictionary: string[]): string[] {
  const n = queries[0].length;
  return queries.filter(querie => {
    for (const s of dictionary) {
      let diff = 0;
      for (let i = 0; i < n; i++) {
        if (querie[i] !== s[i] && ++diff > 2) {
          break;
        }
      }
      if (diff <= 2) {
        return true;
      }
    }
    return false;
  });
}
impl Solution {
  pub fn two_edit_words(queries: Vec<String>, dictionary: Vec<String>) -> Vec<String> {
    let n = queries[0].len();
    queries
      .into_iter()
      .filter(|querie| {
        for s in dictionary.iter() {
          let mut diff = 0;
          for i in 0..n {
            if querie.as_bytes()[i] != s.as_bytes()[i] {
              diff += 1;
            }
          }
          if diff <= 2 {
            return true;
          }
        }
        false
      })
      .collect()
  }
}

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

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

发布评论

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