The Count Substrings Without Repeating Character interview question requires you to find the total count of all substrings in a string that contain only unique characters. Unlike the problem of finding the longest such substring, here you must count every possible valid substring.
For example, in "abc", all substrings ("a", "b", "c", "ab", "bc", "abc") are valid because none of them contain duplicates. In "aba", the substring "aba" is invalid.
Interviews at Yandex and Oracle frequently feature this to test proficiency with the sliding window interview pattern. It’s a step up from the "Longest Unique Substring" problem because it requires a deeper understanding of how the window size relates to the total count. It demonstrates a candidate's ability to maintain a set or hash table of seen characters and efficiently update a running total.
The Sliding Window (Two Pointers) combined with a Hash Table is the standard approach.
left = 0, right = 0, and a count = 0.right pointer to include a character.left pointer forward until the duplicate is removed.left to right, the number of unique substrings ending at right is exactly right - left + 1.count.s = "abac"
right = 0: "a". Valid. Count += 1 (Sub: "a").right = 1: "ab". Valid. Count += 2 (Subs: "b", "ab"). Total = 3.right = 2: "aba". Duplicate 'a'!
left to index 1. Window is now "ba".right = 3: "bac". Valid. Count += 3 (Subs: "c", "ac", "bac"). Total = 8.left pointer moves forward.In sliding window problems, remember the "endpoint contribution" rule: the number of subarrays ending at index j that start at or after index i is exactly j - i + 1. This is a powerful way to count total valid subarrays in time.