Given a string s of lowercase letters, build a new string in which each character is repeated as many times as its 1-based position in s. The first character appears once, the second appears twice, the third appears three times, and so on. Characters keep their original order.
Return the expanded string.
Function Signature
def expand_string(s: str) -> str:
Rules
-
Positions are counted from 1, so the character at 0-based index
i
is written
i + 1
times.
-
A letter that occurs more than once is expanded separately at each of its positions. In
"hello"
the two
l
characters are written 3 times and 4 times.
Constraints
-
1 <= len(s) <= 100
-
s
consists of lowercase English letters.
Examples
Input: s = "abc"
Output: "abbccc"
a is written once, b twice and c three times, which gives "abbccc" (6 characters, matching 1 + 2 + 3).
Input: s = "hello"
Output: "heelllllllooooo"
The pieces are h, ee, lll, llll and ooooo. The two l runs sit next to each other, so the output has seven l characters in a row, and the total length is 1 + 2 + 3 + 4 + 5 = 15.
Input: s = "z"
Output: "z"
A single character is at position 1, so it appears once.