Skip to content

Commit 4724bfe

Browse files
committed
LC 1684. Count the Number of Consistent Strings (Rust)
1 parent 8ce55e3 commit 4724bfe

File tree

4 files changed

+94
-4
lines changed

4 files changed

+94
-4
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v3.2.0
3+
rev: v4.6.0
44
hooks:
55
- id: trailing-whitespace
66
- id: end-of-file-fixer
77
- id: check-added-large-files
88

99
- repo: https://github.com/pre-commit/mirrors-prettier
10-
rev: v2.7.1
10+
rev: v3.1.0
1111
hooks:
1212
- id: prettier
13+
additional_dependencies:
14+
- [email protected] # SEE: https://github.com/pre-commit/pre-commit/issues/3133
1315

1416
- repo: https://github.com/psf/black
15-
rev: 23.3.0
17+
rev: 24.8.0
1618
hooks:
1719
- id: black
1820
args:
@@ -23,6 +25,6 @@ repos:
2325
# hooks:
2426
# - id: markdownlint
2527
- repo: https://github.com/crate-ci/typos
26-
rev: v1.14.10
28+
rev: v1.24.5
2729
hooks:
2830
- id: typos

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
575575
| [1669. Merge In Between Linked Lists][lc1669] | 🟠 Medium | [![python](res/py.png)][lc1669py] |
576576
| [1675. Minimize Deviation in Array][lc1675] | 🔴 Hard | [![python](res/py.png)][lc1675py] [![rust](res/rs.png)][lc1675rs] |
577577
| [1680. Concatenation of Consecutive Binary Numbers][lc1680] | 🟠 Medium | [![python](res/py.png)][lc1680py] |
578+
| [1684. Count the Number of Consistent Strings][lc1684] | 🟢 Easy | [![rust](res/rs.png)][lc1684rs] |
578579
| [1685. Sum of Absolute Differences in a Sorted Array][lc1685] | 🟠 Medium | [![rust](res/rs.png)][lc1685rs] |
579580
| [1689. Partitioning Into Minimum Number Of Deci-Binary Numbers][lc1689] | 🟠 Medium | [![python](res/py.png)][lc1689py] |
580581
| [1695. Maximum Erasure Value][lc1695] | 🟠 Medium | [![python](res/py.png)][lc1695py] |
@@ -2032,6 +2033,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
20322033
[lc1675rs]: leetcode/minimize-deviation-in-array.rs
20332034
[lc1680]: https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/
20342035
[lc1680py]: leetcode/concatenation-of-consecutive-binary-numbers.py
2036+
[lc1684]: https://leetcode.com/problems/count-the-number-of-consistent-strings/
2037+
[lc1684rs]: leetcode/count-the-number-of-consistent-strings.rs
20352038
[lc1685]: https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/
20362039
[lc1685rs]: leetcode/sum-of-absolute-differences-in-a-sorted-array.rs
20372040
[lc1689]: https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/

_typos.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
[default]
22
extend-ignore-identifiers-re = []
33

4+
extend-ignore-re = [
5+
"(?Rm)^.*(#|//)\\s*typos:ignore$",
6+
"(?s)(#|//)\\s*typos:off.*?\\n\\s*(#|//)\\s*typos:on",
7+
]
8+
49
[default.extend-identifiers]
510

611
[default.extend-words]
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// 1684. Count the Number of Consistent Strings
2+
// 🟢 Easy
3+
//
4+
// https://leetcode.com/problems/count-the-number-of-consistent-strings/
5+
//
6+
// Tags: Array - Hash Table - String - Bit Manipulation - Counting
7+
8+
use std::collections::HashSet;
9+
10+
struct Solution;
11+
impl Solution {
12+
/// Use a way to check in O(1) whether a character is allowed, I used a hash set but we could
13+
/// also have used a binary array or the bits of a value with >26 bits, for example an i32.
14+
///
15+
/// Time complexity: O(n) - Where n is the number of characters in all the words combined, we
16+
/// iterate over all characters checking that they are allowed.
17+
/// Space complexity: O(1) - The hashset can grow to a max of size 26.
18+
///
19+
/// Runtime 16 ms Beats 48%
20+
/// Memory 2.62 MB Beats 55%
21+
pub fn count_consistent_strings(allowed: String, words: Vec<String>) -> i32 {
22+
let allowed: HashSet<char> = allowed.chars().collect();
23+
let mut res = 0;
24+
let mut good;
25+
for word in words {
26+
good = true;
27+
for c in word.chars() {
28+
if !allowed.contains(&c) {
29+
good = false;
30+
break;
31+
}
32+
}
33+
if good {
34+
res += 1;
35+
}
36+
}
37+
res
38+
}
39+
}
40+
41+
// Tests.
42+
fn main() {
43+
let tests = [
44+
("ab", vec!["ad", "bd", "aaab", "baa", "badab"], 2),
45+
("abc", vec!["a", "b", "c", "ab", "ac", "bc", "abc"], 7),
46+
(
47+
"cad",
48+
vec!["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"], // typos:ignore
49+
4,
50+
),
51+
];
52+
println!("\n\x1b[92m» Running {} tests...\x1b[0m", tests.len());
53+
let mut success = 0;
54+
for (i, t) in tests.iter().enumerate() {
55+
let res = Solution::count_consistent_strings(
56+
t.0.to_string(),
57+
t.1.iter().map(|s| s.to_string()).collect::<_>(),
58+
);
59+
if res == t.2 {
60+
success += 1;
61+
println!("\x1b[92m✔\x1b[95m Test {} passed!\x1b[0m", i);
62+
} else {
63+
println!(
64+
"\x1b[31mx\x1b[95m Test {} failed expected: {:?} but got {}!!\x1b[0m",
65+
i, t.2, res
66+
);
67+
}
68+
}
69+
println!();
70+
if success == tests.len() {
71+
println!("\x1b[30;42m✔ All tests passed!\x1b[0m")
72+
} else if success == 0 {
73+
println!("\x1b[31mx \x1b[41;37mAll tests failed!\x1b[0m")
74+
} else {
75+
println!(
76+
"\x1b[31mx\x1b[95m {} tests failed!\x1b[0m",
77+
tests.len() - success
78+
)
79+
}
80+
}

0 commit comments

Comments
 (0)