< Ace Coding /> ๐
Reply/*
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 tThis one took me much longer time not gonna lie.๐ฎโ๐จ
โ
Solution : this is as efficient as it can get
python
def countSpaceialSubString(s):
seen = set()
l = 0
count = 0
for r in range(len(s)):
if s[r] in seen:
while s[l] != s[r]:
seen.remove(s[l])
l += 1
l += 1
seen.add(s[r])
count += r - l + 1
return count
print(countSpaceialSubString("abcd"))
print(countSpaceialSubString("ooo"))
print(countSpaceialSubString("abab"))
print(countSpaceialSubString("abcabc"))
3 ยท 409 ยท