Skip to content

Commit 194eb4a

Browse files
committed
2068. Check Whether Two Strings are Almost Equivalent: AC
1 parent 99ea092 commit 194eb4a

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,3 +1559,4 @@ mod s2062_count_vowel_substrings_of_a_string;
15591559
mod s2063_vowels_of_all_substrings;
15601560
mod s2064_minimized_maximum_of_products_distributed_to_any_store;
15611561
mod s2065_maximum_path_quality_of_a_graph;
1562+
mod s2068_check_whether_two_strings_are_almost_equivalent;
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* [2068] Check Whether Two Strings are Almost Equivalent
3+
*
4+
* 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.
5+
* Given two strings word1 and word2, each of length n, return true if word1 and word2 are almost equivalent, or false otherwise.
6+
* The frequency of a letter x is the number of times it occurs in the string.
7+
*
8+
* Example 1:
9+
*
10+
* Input: word1 = "aaaa", word2 = "bccb"
11+
* Output: false
12+
* Explanation: There are 4 'a's in "aaaa" but 0 'a's in "bccb".
13+
* The difference is 4, which is more than the allowed 3.
14+
*
15+
* Example 2:
16+
*
17+
* Input: word1 = "abcdeef", word2 = "abaaacc"
18+
* Output: true
19+
* Explanation: The differences between the frequencies of each letter in word1 and word2 are at most 3:
20+
* - 'a' appears 1 time in word1 and 4 times in word2. The difference is 3.
21+
* - 'b' appears 1 time in word1 and 1 time in word2. The difference is 0.
22+
* - 'c' appears 1 time in word1 and 2 times in word2. The difference is 1.
23+
* - 'd' appears 1 time in word1 and 0 times in word2. The difference is 1.
24+
* - 'e' appears 2 times in word1 and 0 times in word2. The difference is 2.
25+
* - 'f' appears 1 time in word1 and 0 times in word2. The difference is 1.
26+
*
27+
* Example 3:
28+
*
29+
* Input: word1 = "cccddabba", word2 = "babababab"
30+
* Output: true
31+
* Explanation: The differences between the frequencies of each letter in word1 and word2 are at most 3:
32+
* - 'a' appears 2 times in word1 and 4 times in word2. The difference is 2.
33+
* - 'b' appears 2 times in word1 and 5 times in word2. The difference is 3.
34+
* - 'c' appears 3 times in word1 and 0 times in word2. The difference is 3.
35+
* - 'd' appears 2 times in word1 and 0 times in word2. The difference is 2.
36+
*
37+
*
38+
* Constraints:
39+
*
40+
* n == word1.length == word2.length
41+
* 1 <= n <= 100
42+
* word1 and word2 consist only of lowercase English letters.
43+
*
44+
*/
45+
pub struct Solution {}
46+
47+
// problem: https://leetcode.com/problems/check-whether-two-strings-are-almost-equivalent/
48+
// discuss: https://leetcode.com/problems/check-whether-two-strings-are-almost-equivalent/discuss/?currentPage=1&orderBy=most_votes&query=
49+
50+
// submission codes start here
51+
52+
impl Solution {
53+
pub fn check_almost_equivalent(word1: String, word2: String) -> bool {
54+
let mut dict = [0; 26];
55+
56+
for b in word1.as_bytes() {
57+
dict[(*b - b'a') as usize] += 1;
58+
}
59+
60+
for b in word2.as_bytes() {
61+
dict[(*b - b'a') as usize] -= 1;
62+
}
63+
for v in dict {
64+
if v > 3 || v < -3 {
65+
return false;
66+
}
67+
}
68+
69+
true
70+
}
71+
}
72+
73+
// submission codes end
74+
75+
#[cfg(test)]
76+
mod tests {
77+
use super::*;
78+
79+
#[test]
80+
fn test_2068_example_1() {
81+
let word1 = "aaaa".to_string();
82+
let word2 = "bccb".to_string();
83+
84+
let result = false;
85+
86+
assert_eq!(Solution::check_almost_equivalent(word1, word2), result);
87+
}
88+
89+
#[test]
90+
fn test_2068_example_2() {
91+
let word1 = "abcdeef".to_string();
92+
let word2 = "abaaacc".to_string();
93+
94+
let result = true;
95+
96+
assert_eq!(Solution::check_almost_equivalent(word1, word2), result);
97+
}
98+
99+
#[test]
100+
fn test_2068_example_3() {
101+
let word1 = "cccddabba".to_string();
102+
let word2 = "babababab".to_string();
103+
104+
let result = true;
105+
106+
assert_eq!(Solution::check_almost_equivalent(word1, word2), result);
107+
}
108+
}

0 commit comments

Comments
 (0)