← 返回 nvidia 的题目列表Compress and Decompress String (Excel-like Encoding)
类型:online_judge
Given a string s consisting of uppercase letters A-Z, implement two functions compress(s) and decompress(t) such that:
decompress(compress(s)) == s
The encoding uses an Excel-like count representation: for each run of identical consecutive characters, encode it as char + count, where count is written in Excel column style (A=1, B=2, ..., Z=26, AA=27, etc.).
Examples:
AAAA has count 4, which is D in Excel style, so it becomes AD.
ZZ has count 2, which is B, so it becomes ZB.
Requirements
Implement:
compress(s: str) -> str
decompress(t: str) -> str
Constraints
1 <= len(s) <= 2e5
s contains only A-Z
t is guaranteed to be valid output of compress
Target O(n) time and reasonable extra space
Example
Input: s = "AAAABCC" -> output compress(s) (according to the rule)
Input: t = "AD" -> output decompress(t) = "AAAA"
(If you assume a different but self-consistent definition of "Excel-like" counting, state it clearly.)
Example
Input
AAAA
Output
AD