From cd518a4b7fb4fb8276751af7c0d7294389f34632 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Wed, 2 Jul 2025 18:28:10 -0600 Subject: 3304. find the kth character in string game 1 --- find_kth_char_str_game1.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 find_kth_char_str_game1.py (limited to 'find_kth_char_str_game1.py') diff --git a/find_kth_char_str_game1.py b/find_kth_char_str_game1.py new file mode 100644 index 0000000..6b24764 --- /dev/null +++ b/find_kth_char_str_game1.py @@ -0,0 +1,26 @@ +# 3304. Find the K-th Character in String Game I + +""" +alice and bob are playing a game. initially, alice has a string 'word = "a"'. +you are given a positive integer 'k'. now, bob will ask alice to perform the +following operation forwever: generate a new string by changing each +character in 'word' to its next character in the english alphabet and append +it to the original 'word'. return the value of the k'th character in 'word' +after enough operations have been done for 'word' to have at least 'k' +characters. note that character 'z' can be changed to 'a' in the operation. +""" + + +class Solution(object): + def kthCharacter(self, k): + """ + :type k: int + :rtype: str + """ + return chr(ord("a") + (k - 1).bit_count()) + + +if __name__ == "__main__": + obj = Solution() + print(obj.kthCharacter(k=5)) + print(obj.kthCharacter(k=10)) -- cgit v1.2.3