1915. Number of Wonderful Substrings Medium

@problem@discussion
#Hash Table#String#Bit Manipulation#Prefix Sum



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 */
62pub 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
69impl Solution {
70    pub fn wonderful_substrings(word: String) -> i64 {
71        
72    }
73}
74
75// submission codes end
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn test_1915() {
83    }
84}
85


Back
© 2025 bowen.ge All Rights Reserved.