One suggestion for all solutions

  • 1

    I suggest to do this treatment, before go directly DP.

    Shorten the original string, like reduce aaabbb to ab.

    The same consecutive characters won't change the result and this really help improve the efficiency.

    Besides, in python, it takes only 1 line to do it:

    s = ''.join(a for a, b in zip(s, '#' + s) if a != b)

    or use regex

    s = re.sub(r'(.)\1*', r'\1', s)

    Edited after stefan's suggestion.

  • 0

    Long line. Try regex :-)

  • 0

    Added regex :-)
    Well, I have to say, I am not good enough at this part.

  • 0

    @lee215 Well, you wrote almost the same as mine. I just used + instead of *.

    Btw, in your original you could just use zip(s, '#' + s) instead of zip(s, s[1:] + '#').

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.