Skip to content

Commit 9345b6e

Browse files
committed
1915. Number of Wonderful Substrings: AC
1 parent 0869405 commit 9345b6e

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1444,3 +1444,4 @@ mod s1911_maximum_alternating_subsequence_sum;
14441444
mod s1912_design_movie_rental_system;
14451445
mod s1913_maximum_product_difference_between_two_pairs;
14461446
mod s1914_cyclically_rotating_a_grid;
1447+
mod s1915_number_of_wonderful_substrings;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* [1915] Number of Wonderful Substrings
3+
*
4+
* A wonderful string is a string where at most one letter appears an odd number of times.
5+
*
6+
*
7+
* For example, "ccjjc" and "abab" are wonderful, but "ab" is not.
8+
*
9+
*
10+
* Given a string word that consists of the first ten lowercase English letters ('a' through 'j'), return the number of wonderful non-empty substrings in word. If the same substring appears multiple times in word, then count each occurrence separately.
11+
*
12+
* A substring is a contiguous sequence of characters in a string.
13+
*
14+
*
15+
* Example 1:
16+
*
17+
*
18+
* Input: word = "aba"
19+
* Output: 4
20+
* Explanation: The four wonderful substrings are underlined below:
21+
* - "<u>a</u>ba" -> "a"
22+
* - "a<u>b</u>a" -> "b"
23+
* - "ab<u>a</u>" -> "a"
24+
* - "<u>aba</u>" -> "aba"
25+
*
26+
*
27+
* Example 2:
28+
*
29+
*
30+
* Input: word = "aabb"
31+
* Output: 9
32+
* Explanation: The nine wonderful substrings are underlined below:
33+
* - "<u>a</u>abb" -> "a"
34+
* - "<u>aa</u>bb" -> "aa"
35+
* - "<u>aab</u>b" -> "aab"
36+
* - "<u>aabb</u>" -> "aabb"
37+
* - "a<u>a</u>bb" -> "a"
38+
* - "a<u>abb</u>" -> "abb"
39+
* - "aa<u>b</u>b" -> "b"
40+
* - "aa<u>bb</u>" -> "bb"
41+
* - "aab<u>b</u>" -> "b"
42+
*
43+
*
44+
* Example 3:
45+
*
46+
*
47+
* Input: word = "he"
48+
* Output: 2
49+
* Explanation: The two wonderful substrings are underlined below:
50+
* - "<u>h</u>e" -> "h"
51+
* - "h<u>e</u>" -> "e"
52+
*
53+
*
54+
*
55+
* Constraints:
56+
*
57+
*
58+
* 1 <= word.length <= 10^5
59+
* word consists of lowercase English letters from 'a' to 'j'.
60+
*
61+
*/
62+
pub struct Solution {}
63+
64+
// problem: https://leetcode.com/problems/number-of-wonderful-substrings/
65+
// discuss: https://leetcode.com/problems/number-of-wonderful-substrings/discuss/?currentPage=1&orderBy=most_votes&query=
66+
67+
// submission codes start here
68+
69+
impl Solution {
70+
pub fn wonderful_substrings(word: String) -> i64 {
71+
word.bytes()
72+
.fold(
73+
(0usize, 0i64, {
74+
let mut v = vec![0i64; 1024];
75+
v[0] = 1;
76+
v
77+
}),
78+
|(mut x, mut ans, mut v), c| {
79+
x ^= 1 << (c - 97);
80+
ans += v[x];
81+
ans += (0..10).map(|i| v[x ^ (1 << i)]).sum::<i64>();
82+
v[x] += 1;
83+
(x, ans, v)
84+
},
85+
)
86+
.1
87+
}
88+
}
89+
90+
// submission codes end
91+
92+
#[cfg(test)]
93+
mod tests {
94+
use super::*;
95+
96+
#[test]
97+
fn test_1915_example_1() {
98+
let word = "aba".to_string();
99+
100+
let result = 4;
101+
102+
assert_eq!(Solution::wonderful_substrings(word), result);
103+
}
104+
105+
#[test]
106+
fn test_1915_example_2() {
107+
let word = "aabb".to_string();
108+
109+
let result = 9;
110+
111+
assert_eq!(Solution::wonderful_substrings(word), result);
112+
}
113+
114+
#[test]
115+
fn test_1915_example_3() {
116+
let word = "he".to_string();
117+
118+
let result = 2;
119+
120+
assert_eq!(Solution::wonderful_substrings(word), result);
121+
}
122+
}

0 commit comments

Comments
 (0)