Problem
Problem page: https://leetcode.com/problems/count-and-say/
Topics: String
Difficulty: Easy
Similar problems: (M) Encode and Decode Strings
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1is read off as"one 1"or11.
11is read off as"two 1s"or21.
21is read off as"one 2, thenone 1"or1211.Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
Analysis
It's mostly about the counting logic. In each round, I stop counting when I hit a different character. In the last round, though, I have to remember to stop counting explicitly because I've reached the end of the string src. It's like what the final $ symbol does in lexical analysis in compiler theory.
Code
1 | class Solution: |
Article link: https://xnerv.wang/en/leetcode-count-and-say/
