Find the K-th Character in String Game II

Solution
import math

class Solution:
    def kthCharacter(self, k: int, operations: List[int]) -> str:
        if k == 1:
            return 'a'
        
        op_index = math.ceil(math.log2(k)) - 1
        length = 1 << op_index
        
        prev_char = self.kthCharacter(k - length, operations)
        
        if operations[op_index] == 1:
            return 'a' if prev_char == 'z' else chr(ord(prev_char) + 1)
        return prev_char

Time Complexity

O(log k) or O(N)

Space Complexity

O(1) or O(log k)
Video GuideLeetcode Daily