#1915
Number of Wonderful Substrings
specialist · 625 · lc medium +29 · verified · 66.6% accepted · 1,829 likes · top 72%
Description
A string is wonderful if at most one letter appears an odd number of times.
Given a string word using only letters 'a' through 'j', return the total number of non-empty wonderful substrings. Count repeated substrings at different positions separately.
Example 1:
Input: word = "aba"
Output: 4
Explanation: The four wonderful substrings are underlined below:
- "aba" -> "a"
- "aba" -> "b"
- "aba" -> "a"
- "aba" -> "aba"
Example 2:
Input: word = "aabb"
Output: 9
Explanation: The nine wonderful substrings are underlined below:
- "aabb" -> "a"
- "aabb" -> "aa"
- "aabb" -> "aab"
- "aabb" -> "aabb"
- "aabb" -> "a"
- "aabb" -> "abb"
- "aabb" -> "b"
- "aabb" -> "bb"
- "aabb" -> "b"
Example 3:
Input: word = "he"
Output: 2
Explanation: The two wonderful substrings are underlined below:
- "he" -> "h"
- "he" -> "e"
Code
1
2