Talentd
FleetCode

3744. Find Kth Character in Expanded String

Medium56.4% acceptancePremium elsewhere, free here

You are given a string s consisting of one or more words separated by single spaces. Each word in s consists of lowercase English letters.

We obtain the expanded string t from s as follows:

  • For each word in s, repeat its first character once, then its second character twice, and so on.

For example, if s = "hello world", then t = "heelllllllooooo woorrrllllddddd".

You are also given an integer k, representing a valid index of the string t.

Return the kth character of the string t.

Example 1:

Input: s = "hello world", k = 0

Output: "h"

Explanation:

t = "heelllllllooooo woorrrllllddddd". Therefore, the answer is t[0] = "h".

Example 2:

Input: s = "hello world", k = 15

Output: " "

Explanation:

t = "heelllllllooooo woorrrllllddddd". Therefore, the answer is t[15] = " ".

Constraints:

  • 1 <= s.length <= 105
  • s contains only lowercase English letters and spaces ' '.
  • s does not contain any leading or trailing spaces.
  • All the words in s are separated by a single space.
  • 0 <= k < t.length. That is, k is a valid index of t.

Topics

Hints open one at a time

Hint 1
Use the index of a character in the word to calculate how many times it is repeated in t.
Hint 2
Reduce k by that repetition count as you iterate through the characters.
Hint 3
Handle spaces as single characters (cost 1) and reset the index counter for each new word.
2:06
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
"hello world"
0
Input
"hello world"
15
Your code is saved on this device.Solution