Encode Adjacent Letters

Encode a string by counting the consecutive letter.

(i.e., "aaaabbxxxyyz" might become "a4b2x3y2z1").

分析:

这个问题是一个基本的数据压缩算法,将相邻的字符计数存储。解法没有什么特别需要注意的地方,按部就班的实现代码就可以了。

class Solution:

    # @param s, letters as string
    # @return an encoded string
    def encode(self, s):
        if not s or len(s) == 0:
            return ""

        val = ""
        prev = s[0]
        count = 1
        for c in s[1:]:
            if c == prev:
                count += 1
            else:
                val += prev + str(count)
                prev = c
                count = 1
        val += prev + str(count)
        return val

if __name__ == ‘__main__‘:
    s = Solution()
    assert s.encode("") == ""
    assert s.encode("aaaabbxxxyyz") == "a4b2x3y2z1"
    assert s.encode("aaaaaaaaaaab") == "a11b1"
    print ‘PASS‘

小结:

extends下是目前不在LeetCode但是我遇到的有趣的程序问题。

时间: 2024-08-01 21:37:58

Encode Adjacent Letters的相关文章

例题 3-5 谜题 uva227

A children’s puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the frame, the frame

IOI 2009:Mecho

IOI2009 Mecho Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Original ID: CTOI09_164-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Type: None None Graph Theory 2-SAT

E - Puzzle( UVA-227)

 Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the frame, t

Uva 227-Puzzle 解题报告

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=163 Puzzle Time limit: 3.000 seconds A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small square

Puzzle UVA - 227

A children's puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the frame, the frame

ZOJ 2477 Magic Cube(魔方)

ZOJ 2477 Magic Cube(魔方) Time Limit: 2 Seconds      Memory Limit: 65536 KB This is a very popular game for children. In this game, there's a cube, which consists of 3 * 3 * 3 small cubes. We can unwrap the cube, it will become like this: 这是个有名的儿童游戏.游戏

uva227 谜题

 Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the frame, t

UVa 227 / UVALive 5166 Puzzle 谜题 (结构体)

Puzzle Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Description   A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small squares of equal size. A unique letter of the alphabet

[LeetCode] Expressive Words 富于表现力的单词

Sometimes people repeat letters to represent extra feeling, such as "hello" -> "heeellooo", "hi" -> "hiiii".  Here, we have groups, of adjacent letters that are all the same character, and adjacent characters