Problem · String

K-th Character in an Infinite String

Learn this problem
MediumArcesium logoArcesiumFULLTIMEOA

Problem statement

Build an infinite string from non-empty strings s and t in numbered blocks:

  1. Append s once.
  2. Append t twice.
  3. Append s three times.
  4. Append t four times.
  5. Continue alternating strings while increasing the repetition count by one for each block.

Given a 1-indexed position k, return the character at that position.

Function

kthInfiniteCharacter(s: String, t: String, k: long) → char

Examples

Example 1

s = "a"t = "bc"k = 4return = b

The beginning is abcbcaaabcbcbcbc.... Position 4 contains b.

Example 2

s = "ab"t = "x"k = 7return = a

The first three blocks are ab, xx, and ababab. Position 7 is the third character of the third block, which is a.

Constraints

  • 1 <= s.length, t.length <= 100.
  • 1 <= k <= 10^16.

More Arcesium problems

drafts saved locally
public char kthInfiniteCharacter(String s, String t, long k) {
  // Write your code here.
}
s"a"
t"bc"
k4
expectedb
checking account