Sunday, July 19, 2015

longest substring in s1 with all chars of s2

// example:
      |--|
s: aabaaaa
o: ac
string (s, other):
    maxstart = 0, maxcount = 0
    start = 0, count = 0
    set = toSet(other)
    for i = 0, i < s.length(), i++:
        if set.has(s[i]):
            count++
        else:
            count = 0
            start = i +1
        if count > maxcount:
            maxcount = count
            maxstart = start
    return s.substring(maxstart, maxcount)

No comments:

Post a Comment