Count Substrings Without Repeating Character - Problem
You are given a string s consisting only of lowercase English letters. We call a substring special if it contains no character which has occurred at least twice (in other words, it does not contain a repeating character).
Your task is to count the number of special substrings.
For example, in the string "pop", the substring "po" is a special substring, however, "pop" is not special (since 'p' has occurred twice).
Return the number of special substrings.
A substring is a contiguous sequence of characters within a string. For example, "abc" is a substring of "abcd", but "acd" is not.
Input & Output
Example 1 — Basic Case
$
Input:
s = "abca"
›
Output:
7
💡 Note:
Special substrings are: "a" (at index 0), "ab", "abc", "b", "bc", "bca", "a" (at index 3). Total count is 7.
Example 2 — All Same Character
$
Input:
s = "aaa"
›
Output:
3
💡 Note:
Only single characters are special: "a" at index 0, "a" at index 1, "a" at index 2. Any longer substring contains repeating characters.
Example 3 — All Unique Characters
$
Input:
s = "abc"
›
Output:
6
💡 Note:
All substrings are special: "a", "ab", "abc", "b", "bc", "c". Total count is 6.
Constraints
- 1 ≤ s.length ≤ 104
- s consists only of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input String
Given string with potentially repeating characters
2
Find Valid Substrings
Identify all substrings with unique characters only
3
Count Total
Return the total count of special substrings
Key Takeaway
🎯 Key Insight: Use sliding window to efficiently count valid substrings ending at each position
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code