<filename>Strings/Compress.py
"""
Compress a string such that 'AAABCCDDDD' becomes 'A3BC2D4'. Only compress the string if it saves space.

"""


def compress(s):
    if not s:
        return s
    res = ''
    prev = ''
    for i in range(len(s)):

        if prev and s[i] != prev[-1]:
            if len(prev) == 1:
                res += prev
            else:
                res += prev[0]
                res += str(len(prev))
            prev = s[i]
        else:
            prev += s[i]
    if prev:
        if len(prev) == 1:
            res += prev
        else:
            res += prev[0]
            res += str(len(prev))

    return res if len(s) > len(res) else s


print(compress('AAABAACCDDDD'))

<filename>Strings/Compress.py
"""
Compress a string such that 'AAABCCDDDD' becomes 'A3BC2D4'. Only compress the string if it saves space.

"""


def compress(s):
    if not s:
        return s
    res = ''
    prev = ''
    for i in range(len(s)):

        if prev and s[i] != prev[-1]:
            if len(prev) == 1:
                res += prev
            else:
                res += prev[0]
                res += str(len(prev))
            prev = s[i]
        else:
            prev += s[i]
    if prev:
        if len(prev) == 1:
            res += prev
        else:
            res += prev[0]
            res += str(len(prev))

    return res if len(s) > len(res) else s


print(compress('AAABAACCDDDD'))

Compress a string such that 'AAABCCDDDD' becomes 'A3BC2D4'. Only compress the string if it saves space.