I got an interview question yesterday but it had no description as it was all verbal. It was along the lines of:
Given a string, see how many possible runs there are using the seen characters in the string in the order they appear.
So if you are given a string = "abcabc", there are 4 possible runs of "abc".
abcabc
abc___
ab___c
a___bc
___abcI can't find out the problem on the site and was wondering if anyone could help me find a solution to this.
def howManyRuns(s):
if len(s) < 2:
return len(s)
hashmap = {}
for i in range(len(s)):
if i == 0:
hashmap[s[i]] = 1
else:
if s[i] in hashmap:
hashmap[s[i]] += hashmap[s[i - 1]]
else:
hashmap[s[i]] = 1
return hashmap[s[-1]]Is the code that I have thus far.