Q: https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/

class Solution:

def uniqueLetterString(self, s: str) -> int:

n=len(s)

substr=[]

for i in range(n):

for j in range(i+1, n+1):

substr.append(s[i:j])

#substring lenth count

sum=0

unq=[]

for i in substr:

if i not in unq:

unq.append(i)

sum=sum+len(set(i))

print(unq)

return sum

class Solution:

def uniqueLetterString(self, s: str) -> int:

if s is None:

raise RuntimeError("Bad input; s cannot be None")

lastSeen = {}

retval = 0

lastStepCount = 0

for i in range(len(s)):

lastTwoSeenIndices = lastSeen.get(s[i], None)

if not lastTwoSeenIndices:

currentStepCount = lastStepCount + i + 1

lastSeen[s[i]] = (-1, i)

print(lastSeen[s[i]])

else:

secondLastSeenIndex, lastSeenIndex = lastTwoSeenIndices

numOfSuffixesWithoutCurrChar = i - 1 - lastSeenIndex

numOfSuffixesWithJustOneOccurrenceOfCurrChar = \

lastSeenIndex - secondLastSeenIndex

currentStepCount = \

lastStepCount + \

1 + \

numOfSuffixesWithoutCurrChar - \

numOfSuffixesWithJustOneOccurrenceOfCurrChar

lastSeen[s[i]] = (lastSeenIndex, i)

retval += currentStepCount

lastStepCount = currentStepCount


return retval