Skip to content

Commit 80a9c16

Browse files
Added task 2068.
1 parent 162f764 commit 80a9c16

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g2001_2100.s2068_check_whether_two_strings_are_almost_equivalent;
2+
3+
// #Easy #String #Hash_Table #Counting #2022_05_28_Time_1_ms_(94.83%)_Space_42.7_MB_(23.62%)
4+
5+
public class Solution {
6+
public boolean checkAlmostEquivalent(String word1, String word2) {
7+
int[] freq = new int[26];
8+
for (int i = 0; i < word1.length(); i++) {
9+
++freq[word1.charAt(i) - 'a'];
10+
--freq[word2.charAt(i) - 'a'];
11+
}
12+
for (int i : freq) {
13+
if (Math.abs(i) > 3) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2068\. Check Whether Two Strings are Almost Equivalent
2+
3+
Easy
4+
5+
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`.
6+
7+
Given two strings `word1` and `word2`, each of length `n`, return `true` _if_ `word1` _and_ `word2` _are **almost equivalent**, or_ `false` _otherwise_.
8+
9+
The **frequency** of a letter `x` is the number of times it occurs in the string.
10+
11+
**Example 1:**
12+
13+
**Input:** word1 = "aaaa", word2 = "bccb"
14+
15+
**Output:** false
16+
17+
**Explanation:** There are 4 'a's in "aaaa" but 0 'a's in "bccb".
18+
19+
The difference is 4, which is more than the allowed 3.
20+
21+
**Example 2:**
22+
23+
**Input:** word1 = "abcdeef", word2 = "abaaacc"
24+
25+
**Output:** true
26+
27+
**Explanation:** The differences between the frequencies of each letter in word1 and word2 are at most 3:
28+
29+
- 'a' appears 1 time in word1 and 4 times in word2. The difference is 3.
30+
31+
- 'b' appears 1 time in word1 and 1 time in word2. The difference is 0.
32+
33+
- 'c' appears 1 time in word1 and 2 times in word2. The difference is 1.
34+
35+
- 'd' appears 1 time in word1 and 0 times in word2. The difference is 1.
36+
37+
- 'e' appears 2 times in word1 and 0 times in word2. The difference is 2.
38+
39+
- 'f' appears 1 time in word1 and 0 times in word2. The difference is 1.
40+
41+
**Example 3:**
42+
43+
**Input:** word1 = "cccddabba", word2 = "babababab"
44+
45+
**Output:** true
46+
47+
**Explanation:** The differences between the frequencies of each letter in word1 and word2 are at most 3:
48+
49+
- 'a' appears 2 times in word1 and 4 times in word2. The difference is 2.
50+
51+
- 'b' appears 2 times in word1 and 5 times in word2. The difference is 3.
52+
53+
- 'c' appears 3 times in word1 and 0 times in word2. The difference is 3.
54+
55+
- 'd' appears 2 times in word1 and 0 times in word2. The difference is 2.
56+
57+
**Constraints:**
58+
59+
* `n == word1.length == word2.length`
60+
* `1 <= n <= 100`
61+
* `word1` and `word2` consist only of lowercase English letters.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2001_2100.s2068_check_whether_two_strings_are_almost_equivalent;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void checkAlmostEquivalent() {
11+
assertThat(new Solution().checkAlmostEquivalent("aaaa", "bccb"), equalTo(false));
12+
}
13+
14+
@Test
15+
void checkAlmostEquivalent2() {
16+
assertThat(new Solution().checkAlmostEquivalent("abcdeef", "abaaacc"), equalTo(true));
17+
}
18+
19+
@Test
20+
void checkAlmostEquivalent3() {
21+
assertThat(new Solution().checkAlmostEquivalent("cccddabba", "babababab"), equalTo(true));
22+
}
23+
}

0 commit comments

Comments
 (0)