Repeat each character of a string by its 1-based position

Quick Overview

Build a new string from a lowercase string by repeating each character as many times as its 1-based position. The problem tests careful index handling, efficient string building and reasoning about how large the output grows.

Repeat each character of a string by its 1-based position

Company: Cognizant

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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 ```python 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 ```text Input: s = "abc" Output: "abbccc" ``` `a` is written once, `b` twice and `c` three times, which gives `"abbccc"` (6 characters, matching 1 + 2 + 3). ```text 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. ```text Input: s = "z" Output: "z" ``` A single character is at position 1, so it appears once.

Overview: Build a new string from a lowercase string by repeating each character as many times as its 1-based position. The problem tests careful index handling, efficient string building and reasoning about how large the output grows.

Cognizant logo
Cognizant
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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.