We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Find the K-th Character in String Game II

Number: 3601

Difficulty: Hard

Paid? No

Companies: N/A


Problem Description

Alice starts with word = "a". She then performs a sequence of operations given in an array. For each operation: • If the operation is 0, she appends a copy of the current string to itself. • If the operation is 1, she generates a new string from the current one by replacing each character with its next character in the cyclic English alphabet (with 'z' wrapping to 'a'), and appends this transformed copy to the current string. Given a positive integer k, determine the k-th character (1-indexed) of the final string. The input is guaranteed so that the final string length is at least k.


Key Insights

  • Each operation doubles the string length; the final length is 2^(# operations).
  • It is infeasible to construct the string explicitly because k can be very large.
  • The final string is built recursively. The first half is exactly the previous string, while the second half is either the same as the previous string (for op 0) or the previous string with each character “advanced” by one (for op 1).
  • We can “reverse” the process starting from the final (doubled) string. At each operation (processed in reverse), decide if k fell in the first half (no change) or in the second half (subtract half‐length and record a transformation if op==1).
  • By keeping a counter (or offset) of how many times an op==1 the k-th character came from the appended portion, we can determine the net transformation.
  • Finally, the base string has a single character "a". The answer will be "a" shifted forward by the net number of op==1 transformations (modulo 26).

Space and Time Complexity

Time Complexity: O(m), where m is the number of operations (m ≤ 100) Space Complexity: O(1)


Solution

The solution uses a reverse simulation of the operations. Instead of building the string, we determine the origin of the k-th character by “rewinding” the operations. At each step, the current string length is a power of two. Let L_prev be half of that length. If k is in the first half, the character is the same as in the previous stage. If k is in the second half, we update k to represent the position in the previous stage (i.e. k − L_prev) and, if the operation is 1, note that the character had been transformed forward by one position. By accumulating these transformation counts, we can apply the effective shift modulo 26 to the base character “a” at the end.

The key data structures used are only simple numeric counters and arithmetic operations. The algorithm avoids recursion by iterating backwards through the operations array. This reverse traversal technique is a common trick for problems in which the construction process (here doubling with transformation) is reversible.


Code Solutions

# Python solution with detailed comments
def findKthCharacter(k, operations):
    # Number of operations performed
    m = len(operations)
    # This counter counts how many times we picked the second half under a type-1 operation
    transformation_count = 0
    # For each operation, the string length doubles.
    # The final length is 2^(m). Process operations in reverse.
    for i in range(m - 1, -1, -1):
        # Determine the half-length before this operation.
        half_length = 1 << i  # equivalent to 2**i
        # If kth character is in the second half of the string
        if k > half_length:
            # Adjust k to be the corresponding position in the previous string
            k -= half_length
            # If the operation was type 1, record the transformation.
            # Each type-1 operation means that this character was advanced (cyclically) by one.
            if operations[i] == 1:
                transformation_count += 1
    # The base string is simply "a". Compute the final character by shifting 'a'
    # forward by transformation_count steps modulo 26.
    final_char = chr(((ord('a') - ord('a') + transformation_count) % 26) + ord('a'))
    return final_char

# Example usage:
print(findKthCharacter(5, [0, 0, 0]))  # Expected output: "a"
print(findKthCharacter(10, [0, 1, 0, 1]))  # Expected output: "b"
← Back to All Questions