• + 0 comments

    swift solution

    func compress(input: String) -> String {
        var input = input
        var count = 1
        var output = ""
        for char in input {
            if let last = output.last, last == char {
                count += 1
            }else {
                if count > 1 {
                    output = output + String(count)
                    count = 1
                }
                output = output + String(char)
            }
        }
        if count > 1 {
            output = output + String(count)
        }
        return output
    }