返回介绍

solution / 2000-2099 / 2068.Check Whether Two Strings are Almost Equivalent / README_EN

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

2068. Check Whether Two Strings are Almost Equivalent

中文文档

Description

Two strings word1 and word2 are considered almost equivalent if the differences between the frequencies of each letter from 'a' to 'z' between word1 and word2 is at most 3.

Given two strings word1 and word2, each of length n, return true _if _word1 _and_ word2 _are almost equivalent, or_ false _otherwise_.

The frequency of a letter x is the number of times it occurs in the string.

 

Example 1:

Input: word1 = "aaaa", word2 = "bccb"
Output: false
Explanation: There are 4 'a's in "aaaa" but 0 'a's in "bccb".
The difference is 4, which is more than the allowed 3.

Example 2:

Input: word1 = "abcdeef", word2 = "abaaacc"
Output: true
Explanation: The differences between the frequencies of each letter in word1 and word2 are at most 3:
- 'a' appears 1 time in word1 and 4 times in word2. The difference is 3.
- 'b' appears 1 time in word1 and 1 time in word2. The difference is 0.
- 'c' appears 1 time in word1 and 2 times in word2. The difference is 1.
- 'd' appears 1 time in word1 and 0 times in word2. The difference is 1.
- 'e' appears 2 times in word1 and 0 times in word2. The difference is 2.
- 'f' appears 1 time in word1 and 0 times in word2. The difference is 1.

Example 3:

Input: word1 = "cccddabba", word2 = "babababab"
Output: true
Explanation: The differences between the frequencies of each letter in word1 and word2 are at most 3:
- 'a' appears 2 times in word1 and 4 times in word2. The difference is 2.
- 'b' appears 2 times in word1 and 5 times in word2. The difference is 3.
- 'c' appears 3 times in word1 and 0 times in word2. The difference is 3.
- 'd' appears 2 times in word1 and 0 times in word2. The difference is 2.

 

Constraints:

  • n == word1.length == word2.length
  • 1 <= n <= 100
  • word1 and word2 consist only of lowercase English letters.

Solutions

Solution 1: Counting

We can create an array $cnt$ of length $26$ to record the difference in the number of times each letter appears in the two strings. Then we traverse $cnt$, if any letter appears the difference in the number of times greater than $3$, then return false, otherwise return true.

The time complexity is $O(n)$ and the space complexity is $O(C)$. Where $n$ is the length of the string, and $C$ is the size of the character set, and in this question $C = 26$.

class Solution:
  def checkAlmostEquivalent(self, word1: str, word2: str) -> bool:
    cnt = Counter(word1)
    for c in word2:
      cnt[c] -= 1
    return all(abs(x) <= 3 for x in cnt.values())
class Solution {
  public boolean checkAlmostEquivalent(String word1, String word2) {
    int[] cnt = new int[26];
    for (int i = 0; i < word1.length(); ++i) {
      ++cnt[word1.charAt(i) - 'a'];
    }
    for (int i = 0; i < word2.length(); ++i) {
      --cnt[word2.charAt(i) - 'a'];
    }
    for (int x : cnt) {
      if (Math.abs(x) > 3) {
        return false;
      }
    }
    return true;
  }
}
class Solution {
public:
  bool checkAlmostEquivalent(string word1, string word2) {
    int cnt[26]{};
    for (char& c : word1) {
      ++cnt[c - 'a'];
    }
    for (char& c : word2) {
      --cnt[c - 'a'];
    }
    for (int i = 0; i < 26; ++i) {
      if (abs(cnt[i]) > 3) {
        return false;
      }
    }
    return true;
  }
};
func checkAlmostEquivalent(word1 string, word2 string) bool {
  cnt := [26]int{}
  for _, c := range word1 {
    cnt[c-'a']++
  }
  for _, c := range word2 {
    cnt[c-'a']--
  }
  for _, x := range cnt {
    if x > 3 || x < -3 {
      return false
    }
  }
  return true
}
function checkAlmostEquivalent(word1: string, word2: string): boolean {
  const cnt: number[] = new Array(26).fill(0);
  for (const c of word1) {
    ++cnt[c.charCodeAt(0) - 97];
  }
  for (const c of word2) {
    --cnt[c.charCodeAt(0) - 97];
  }
  return cnt.every(x => Math.abs(x) <= 3);
}
/**
 * @param {string} word1
 * @param {string} word2
 * @return {boolean}
 */
var checkAlmostEquivalent = function (word1, word2) {
  const m = new Map();
  for (let i = 0; i < word1.length; i++) {
    m.set(word1[i], (m.get(word1[i]) || 0) + 1);
    m.set(word2[i], (m.get(word2[i]) || 0) - 1);
  }
  for (const v of m.values()) {
    if (Math.abs(v) > 3) {
      return false;
    }
  }
  return true;
};
public class Solution {
  public bool CheckAlmostEquivalent(string word1, string word2) {
    int[] cnt = new int[26];
    foreach (var c in word1) {
      cnt[c - 'a']++;
    }
    foreach (var c in word2) {
      cnt[c - 'a']--;
    }
    return cnt.All(x => Math.Abs(x) <= 3);
  }
}
class Solution {
  /**
   * @param String $word1
   * @param String $word2
   * @return Boolean
   */
  function checkAlmostEquivalent($word1, $word2) {
    for ($i = 0; $i < strlen($word1); $i++) {
      $hashtable[$word1[$i]] += 1;
      $hashtable[$word2[$i]] -= 1;
    }
    $keys = array_keys($hashtable);
    for ($j = 0; $j < count($keys); $j++) {
      if (abs($hashtable[$keys[$j]]) > 3) {
        return false;
      }
    }
    return true;
  }
}

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

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

发布评论

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