[LeetCode] 2068. Check Whether Two Strings are Almost Equivalent

Posted CNoodle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 2068. Check Whether Two Strings are Almost Equivalent相关的知识,希望对你有一定的参考价值。

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.

检查两个字符串是否几乎相等。

如果两个字符串 word1 和 word2 中从 \'a\' 到 \'z\' 每一个字母出现频率之差都 不超过 3 ,那么我们称这两个字符串 word1 和 word2 几乎相等 。

给你两个长度都为 n 的字符串 word1 和 word2 ,如果 word1 和 word2 几乎相等 ,请你返回 true ,否则返回 false 。

一个字母 x 的出现 频率 指的是它在字符串中出现的次数。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/check-whether-two-strings-are-almost-equivalent
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是用 hashmap 记录 word1 和 word2 中每个字母的出现次数,然后比较两个单词中相同字母的出现次数,如果有两个单词中任何一个字母的出现次数的差值超过 3 则返回 false。

时间O(n)

空间O(n)

Java实现

 class Solution 
     public boolean checkAlmostEquivalent(String word1, String word2) 
         int n = word1.length();
         int[] map1 = new int[26];
         int[] map2 = new int[26];
         for (int i = 0; i < n; i++) 
             map1[word1.charAt(i) - \'a\']++;
             map2[word2.charAt(i) - \'a\']++;
         
 
         for (int i = 0; i < 26; i++) 
             if (Math.abs(map1[i] - map2[i]) > 3) 
                 return false;
             
         
         return true;
     
 

 

LeetCode 题目总结

以上是关于[LeetCode] 2068. Check Whether Two Strings are Almost Equivalent的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 958. Check Completeness of a Binary Tree

LeetCode --- 1640. Check Array Formation Through Concatenation 解题报告

LeetCode --- 1640. Check Array Formation Through Concatenation 解题报告

leetcode1346. Check If N and Its Double Exist

LeetCode --- 1232. Check If It Is a Straight Line 解题报告

LeetCode --- 1232. Check If It Is a Straight Line 解题报告