← 返回 nvidia 的题目列表Count Substrings Without Repeating Characters
类型:qbank
Given a string, count its non-empty contiguous substrings whose characters are all distinct. The interview used LeetCode 2743 without a stated modification.
Requirements
Accept a string as input.
Count every non-empty contiguous substring that contains no repeated character.
Return the total count.
Notes
This is the canonical distinct-character substring-counting problem, with no variant described. Confirm character-set and input-size constraints before coding if the interviewer does not state them.
Use a sliding window and store the most recent index of each character. For each right endpoint right, advance left to max(left, last_seen[s[right]] + 1) when the character has appeared inside the current window, then update its last-seen index. Every substring ending at right and starting from left through right is valid, so add right - left + 1 to the answer.
The algorithm runs in O(n) time and uses O(min(n, alphabet_size)) space. Empty input contributes zero; repeated characters outside the current window must not move left backward.
Preparation
Practice deriving the count while maintaining the valid left boundary as the right boundary advances.
Explain the time and space bounds and test repeated characters at the beginning, middle, and end of the string.