Sunday, July 19, 2015

longest substring in s1 with all chars of s2

// example:
       |--|
s1: aabaaaa
s2: ac
(s1, s2):
    if s1 == null || s2 == null:
        throw "args"
    maxcount = start = count = 0
    s2set = s2.toset()
    for i = 0, i < s1.length(), i++:
        c = s1[i]
        if s2set.has(c):
            count++
        else:
            if count > maxcount:
                maxcount = count
                start = i-count
            count = 0
    return s1.substring(start, maxcount)

No comments:

Post a Comment