|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: 中等 |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3744.Find%20Kth%20Character%20in%20Expanded%20String/README.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3744. 在展开字符串中查找第 K 个字符 🔒](https://leetcode.cn/problems/find-kth-character-in-expanded-string) |
| 10 | + |
| 11 | +[English Version](/solution/3700-3799/3744.Find%20Kth%20Character%20in%20Expanded%20String/README_EN.md) |
| 12 | + |
| 13 | +## 题目描述 |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>给定一个字符串 <code>s</code>,该字符串由一个或多个单词组成,单词之间用单个空格分隔。<code>s</code> 中的每个单词均由小写的英文字母组成。</p> |
| 18 | + |
| 19 | +<p>我们按如下步骤从 <code>s</code> 得到 <strong>展开</strong> 字符串 <code>t</code>:</p> |
| 20 | + |
| 21 | +<ul> |
| 22 | + <li>对于 <code>s</code> 中的每个 <strong>单词</strong>,重复一次它的第一个字符,然后重复两次它的第二个字符,以此类推。</li> |
| 23 | +</ul> |
| 24 | + |
| 25 | +<p>例如,如果 <code>s = "hello world"</code>,那么 <code>t = "heelllllllooooo woorrrllllddddd"</code>。</p> |
| 26 | + |
| 27 | +<p>同时给定一个整数 <code>k</code>,表示字符串 <code>t</code> 的一个 <strong>合法</strong> 下标。</p> |
| 28 | + |
| 29 | +<p>返回字符串 <code>t</code> 的第 <code>k</code> 个字符。</p> |
| 30 | + |
| 31 | +<p> </p> |
| 32 | + |
| 33 | +<p><strong class="example">示例 1:</strong></p> |
| 34 | + |
| 35 | +<div class="example-block"> |
| 36 | +<p><span class="example-io"><b>输入:</b>s = "hello world", k = 0</span></p> |
| 37 | + |
| 38 | +<p><span class="example-io"><b>输出:</b>"h"</span></p> |
| 39 | + |
| 40 | +<p><strong>解释:</strong></p> |
| 41 | + |
| 42 | +<p><code>t = "heelllllllooooo woorrrllllddddd"</code>。因此,答案是 <code>t[0] = "h"</code>。</p> |
| 43 | +</div> |
| 44 | + |
| 45 | +<p><strong class="example">示例 2:</strong></p> |
| 46 | + |
| 47 | +<div class="example-block"> |
| 48 | +<p><span class="example-io"><b>输入:</b>s = "hello world", k = 15</span></p> |
| 49 | + |
| 50 | +<p><span class="example-io"><b>输出:</b>" "</span></p> |
| 51 | + |
| 52 | +<p><strong>解释:</strong></p> |
| 53 | + |
| 54 | +<p><code>t = "heelllllllooooo woorrrllllddddd"</code>。因此,答案是 <code>t[15] = " "</code>。</p> |
| 55 | +</div> |
| 56 | + |
| 57 | +<p> </p> |
| 58 | + |
| 59 | +<p><strong>提示:</strong></p> |
| 60 | + |
| 61 | +<ul> |
| 62 | + <li><code>1 <= s.length <= 10<sup>5</sup></code></li> |
| 63 | + <li><code>s</code> 只包含小写英文字母和空格 <code>' '</code>。</li> |
| 64 | + <li><code>s</code> <strong>不包含</strong> 任何前导和后缀空格。</li> |
| 65 | + <li><code>s</code> 中的所有单词都由 <strong>一个空格</strong> 分隔。</li> |
| 66 | + <li><code>0 <= k < t.length</code>。即 <code>k</code> 是 <code>t</code> 的一个 <strong>合法 </strong>下标。</li> |
| 67 | +</ul> |
| 68 | + |
| 69 | +<!-- description:end --> |
| 70 | + |
| 71 | +## 解法 |
| 72 | + |
| 73 | +<!-- solution:start --> |
| 74 | + |
| 75 | +### 方法一:数学 + 模拟 |
| 76 | + |
| 77 | +我们首先将字符串 $\textit{s}$ 按空格拆分成若干单词。对于每个单词 $\textit{w}$,我们可以计算出它在展开字符串 $\textit{t}$ 中所占的长度 $m=\frac{(1+|\textit{w}|)\cdot |\textit{w}|}{2}$。 |
| 78 | + |
| 79 | +如果 $k = m$,说明第 $k$ 个字符是空格,直接返回空格即可。 |
| 80 | + |
| 81 | +如果 $k > m$,说明第 $k$ 个字符不在当前单词的展开部分,我们将 $k$ 减去当前单词的展开长度 $m$ 和空格的长度 $1$,继续处理下一个单词。 |
| 82 | + |
| 83 | +否则,第 $k$ 个字符在当前单词的展开部分。我们可以通过模拟展开过程来找到第 $k$ 个字符: |
| 84 | + |
| 85 | +- 初始化变量 $\textit{cur} = 0$,表示当前已经展开的字符数。 |
| 86 | +- 遍历单词 $\textit{w}$ 的每个字符 $\textit{w}[i]$: |
| 87 | + - 将 $\textit{cur}$ 增加 $i + 1$。 |
| 88 | + - 如果 $k < \textit{cur}$,说明第 $k$ 个字符就是 $\textit{w}[i]$,返回该字符。 |
| 89 | + |
| 90 | +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $\textit{s}$ 的长度。 |
| 91 | + |
| 92 | +<!-- tabs:start --> |
| 93 | + |
| 94 | +#### Python3 |
| 95 | + |
| 96 | +```python |
| 97 | +class Solution: |
| 98 | + def kthCharacter(self, s: str, k: int) -> str: |
| 99 | + for w in s.split(): |
| 100 | + m = (1 + len(w)) * len(w) // 2 |
| 101 | + if k == m: |
| 102 | + return " " |
| 103 | + if k > m: |
| 104 | + k -= m + 1 |
| 105 | + else: |
| 106 | + cur = 0 |
| 107 | + for i in range(len(w)): |
| 108 | + cur += i + 1 |
| 109 | + if k < cur: |
| 110 | + return w[i] |
| 111 | +``` |
| 112 | + |
| 113 | +#### Java |
| 114 | + |
| 115 | +```java |
| 116 | +class Solution { |
| 117 | + public char kthCharacter(String s, long k) { |
| 118 | + for (String w : s.split(" ")) { |
| 119 | + long m = (1L + w.length()) * w.length() / 2; |
| 120 | + if (k == m) { |
| 121 | + return ' '; |
| 122 | + } |
| 123 | + if (k > m) { |
| 124 | + k -= m + 1; |
| 125 | + } else { |
| 126 | + long cur = 0; |
| 127 | + for (int i = 0;; ++i) { |
| 128 | + cur += i + 1; |
| 129 | + if (k < cur) { |
| 130 | + return w.charAt(i); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + return ' '; |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +#### C++ |
| 141 | + |
| 142 | +```cpp |
| 143 | +class Solution { |
| 144 | +public: |
| 145 | + char kthCharacter(string s, long long k) { |
| 146 | + stringstream ss(s); |
| 147 | + string w; |
| 148 | + while (ss >> w) { |
| 149 | + long long m = (1 + (long long) w.size()) * (long long) w.size() / 2; |
| 150 | + if (k == m) { |
| 151 | + return ' '; |
| 152 | + } |
| 153 | + if (k > m) { |
| 154 | + k -= m + 1; |
| 155 | + } else { |
| 156 | + long long cur = 0; |
| 157 | + for (int i = 0;; ++i) { |
| 158 | + cur += i + 1; |
| 159 | + if (k < cur) { |
| 160 | + return w[i]; |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + return ' '; |
| 166 | + } |
| 167 | +}; |
| 168 | +``` |
| 169 | +
|
| 170 | +#### Go |
| 171 | +
|
| 172 | +```go |
| 173 | +func kthCharacter(s string, k int64) byte { |
| 174 | + for _, w := range strings.Split(s, " ") { |
| 175 | + m := (1 + int64(len(w))) * int64(len(w)) / 2 |
| 176 | + if k == m { |
| 177 | + return ' ' |
| 178 | + } |
| 179 | + if k > m { |
| 180 | + k -= m + 1 |
| 181 | + } else { |
| 182 | + var cur int64 |
| 183 | + for i := 0; ; i++ { |
| 184 | + cur += int64(i + 1) |
| 185 | + if k < cur { |
| 186 | + return w[i] |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + return ' ' |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +#### TypeScript |
| 196 | + |
| 197 | +```ts |
| 198 | +function kthCharacter(s: string, k: number): string { |
| 199 | + for (const w of s.split(' ')) { |
| 200 | + const m = ((1 + w.length) * w.length) / 2; |
| 201 | + if (k === m) { |
| 202 | + return ' '; |
| 203 | + } |
| 204 | + if (k > m) { |
| 205 | + k -= m + 1; |
| 206 | + } else { |
| 207 | + let cur = 0; |
| 208 | + for (let i = 0; ; ++i) { |
| 209 | + cur += i + 1; |
| 210 | + if (k < cur) { |
| 211 | + return w[i]; |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + return ' '; |
| 217 | +} |
| 218 | +``` |
| 219 | + |
| 220 | +<!-- tabs:end --> |
| 221 | + |
| 222 | +<!-- solution:end --> |
| 223 | + |
| 224 | +<!-- problem:end --> |
0 commit comments