Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit80a9c16

Browse files
Added task 2068.
1 parent162f764 commit80a9c16

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+
packageg2001_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+
publicclassSolution {
6+
publicbooleancheckAlmostEquivalent(Stringword1,Stringword2) {
7+
int[]freq =newint[26];
8+
for (inti =0;i <word1.length();i++) {
9+
++freq[word1.charAt(i) -'a'];
10+
--freq[word2.charAt(i) -'a'];
11+
}
12+
for (inti :freq) {
13+
if (Math.abs(i) >3) {
14+
returnfalse;
15+
}
16+
}
17+
returntrue;
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+
packageg2001_2100.s2068_check_whether_two_strings_are_almost_equivalent;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importorg.junit.jupiter.api.Test;
7+
8+
classSolutionTest {
9+
@Test
10+
voidcheckAlmostEquivalent() {
11+
assertThat(newSolution().checkAlmostEquivalent("aaaa","bccb"),equalTo(false));
12+
}
13+
14+
@Test
15+
voidcheckAlmostEquivalent2() {
16+
assertThat(newSolution().checkAlmostEquivalent("abcdeef","abaaacc"),equalTo(true));
17+
}
18+
19+
@Test
20+
voidcheckAlmostEquivalent3() {
21+
assertThat(newSolution().checkAlmostEquivalent("cccddabba","babababab"),equalTo(true));
22+
}
23+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp