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
Count Substrings Without Repeating CharacterInput: "abca"abcaSpecial Substrings (no repeating characters):"a", "ab", "abc""b", "bc""c", "bca""a"Invalid: "abca" (duplicate 'a')Output: 7 special substrings
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
Asked in
Google 25 Amazon 20 Microsoft 15
32.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen