原文链接: https://leetcode-cn.com/problems/decoded-string-at-index
英文原文
You are given an encoded string s. To decode the string to a tape, the encoded string is read one character at a time and the following steps are taken:
- If the character read is a letter, that letter is written onto the tape.
- If the character read is a digit
d, the entire current tape is repeatedly writtend - 1more times in total.
Given an integer k, return the kth letter (1-indexed) in the decoded string.
Example 1:
Input: s = "leet2code3", k = 10 Output: "o" Explanation: The decoded string is "leetleetcodeleetleetcodeleetleetcode". The 10th letter in the string is "o".
Example 2:
Input: s = "ha22", k = 5 Output: "h" Explanation: The decoded string is "hahahaha". The 5th letter is "h".
Example 3:
Input: s = "a2345678999999999999999", k = 1 Output: "a" Explanation: The decoded string is "a" repeated 8301530446056247680 times. The 1st letter is "a".
Constraints:
2 <= s.length <= 100sconsists of lowercase English letters and digits2through9.sstarts with a letter.1 <= k <= 109- It is guaranteed that
kis less than or equal to the length of the decoded string. - The decoded string is guaranteed to have less than
263letters.
中文题目
给定一个编码字符串 S。请你找出 解码字符串 并将其写入磁带。解码时,从编码字符串中 每次读取一个字符 ,并采取以下步骤:
- 如果所读的字符是字母,则将该字母写在磁带上。
- 如果所读的字符是数字(例如
d),则整个当前磁带总共会被重复写d-1次。
现在,对于给定的编码字符串 S 和索引 K,查找并返回解码字符串中的第 K 个字母。
示例 1:
输入:S = "leet2code3", K = 10 输出:"o" 解释: 解码后的字符串为 "leetleetcodeleetleetcodeleetleetcode"。 字符串中的第 10 个字母是 "o"。
示例 2:
输入:S = "ha22", K = 5 输出:"h" 解释: 解码后的字符串为 "hahahaha"。第 5 个字母是 "h"。
示例 3:
输入:S = "a2345678999999999999999", K = 1 输出:"a" 解释: 解码后的字符串为 "a" 重复 8301530446056247680 次。第 1 个字母是 "a"。
提示:
2 <= S.length <= 100S只包含小写字母与数字2到9。S以字母开头。1 <= K <= 10^9- 题目保证
K小于或等于解码字符串的长度。 - 解码后的字符串保证少于
2^63个字母。
通过代码
官方题解
方法:逆向工作法
思路
如果我们有一个像 appleappleappleappleappleapple 这样的解码字符串和一个像 K=24 这样的索引,那么如果 K=4,答案是相同的。
一般来说,当解码的字符串等于某个长度为 size 的单词重复某些次数(例如 apple 与 size=5 组合重复6次)时,索引 K 的答案与索引 K % size 的答案相同。
我们可以通过逆向工作,跟踪解码字符串的大小来使用这种洞察力。每当解码的字符串等于某些单词 word 重复 d 次时,我们就可以将 k 减少到 K % (Word.Length)。
算法
首先,找出解码字符串的长度。之后,我们将逆向工作,跟踪 size:解析符号 S[0], S[1], ..., S[i] 后解码字符串的长度。
如果我们看到一个数字 S [i],则表示在解析 S [0],S [1],...,S [i-1] 之后解码字符串的大小将是 size / Integer(S[i])。 否则,将是 size - 1。
[2ooN4yc4-C++]class Solution { public: string decodeAtIndex(string S, int K) { long size = 0; int N = S.size(); // Find size = length of decoded string for (int i = 0; i < N; ++i) { if (isdigit(S[i])) size *= S[i] - '0'; else size++; } for (int i = N-1; i >=0; --i) { K %= size; if (K == 0 && isalpha(S[i])) return (string) "" + S[i]; if (isdigit(S[i])) size /= S[i] - '0'; else size--; } return ""; } };
[2ooN4yc4-Java]class Solution { public String decodeAtIndex(String S, int K) { long size = 0; int N = S.length(); // Find size = length of decoded string for (int i = 0; i < N; ++i) { char c = S.charAt(i); if (Character.isDigit(c)) size *= c - '0'; else size++; } for (int i = N-1; i >= 0; --i) { char c = S.charAt(i); K %= size; if (K == 0 && Character.isLetter(c)) return Character.toString(c); if (Character.isDigit(c)) size /= c - '0'; else size--; } throw null; } }
[2ooN4yc4-Python]class Solution(object): def decodeAtIndex(self, S, K): size = 0 # Find size = length of decoded string for c in S: if c.isdigit(): size *= int(c) else: size += 1 for c in reversed(S): K %= size if K == 0 and c.isalpha(): return c if c.isdigit(): size /= int(c) else: size -= 1
复杂度分析
- 时间复杂度:$O(N)$,其中 $N$ 是
S的长度。 - 空间复杂度:$O(1)$。
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 6226 | 24493 | 25.4% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|