aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-20 20:50:56 -0600
committerJack Sangdahl <[email protected]>2025-03-20 20:50:56 -0600
commited3708880079189955fe728c05c8f91565d9f378 (patch)
tree0f2e9a0c63cea927993063755a3d0deb1d0191c6
parent70e62baf328c657c00e1f23ebe05d924cc35d234 (diff)
downloadleetcode-ed3708880079189955fe728c05c8f91565d9f378.tar.gz
leetcode-ed3708880079189955fe728c05c8f91565d9f378.zip
3307. find the kth character in string game 2
-rw-r--r--find_kth_char_str_game2.c28
-rw-r--r--find_kth_char_str_game2.py29
2 files changed, 57 insertions, 0 deletions
diff --git a/find_kth_char_str_game2.c b/find_kth_char_str_game2.c
new file mode 100644
index 0000000..08202e6
--- /dev/null
+++ b/find_kth_char_str_game2.c
@@ -0,0 +1,28 @@
+// 3307. Find the K-th Character in String Game II
+#include "leetcode.h"
+
+/*
+ * alice and bob are playing a game. initially, alice has a string 'word = "a"'.
+ * you are given a positive integer 'k'. you are also given an integer array
+ * 'operations' where 'operations[i]' represents the type of i'th operation. now
+ * bob will ask alice to perform all operations in sequence: if 'operations[i]
+ * == 0', append a copy of 'word' to itself. if 'operations[i] == 1', generate a
+ * new string by changing each character in 'word' to its next character in the
+ * english alphabet, and append it to the origin 'word'. return the value of the
+ * k'th character in 'word' after performing all the operations.
+ */
+
+char kthCharacter(long long k, int *operations, int operationsSize) {
+ int ans = 0;
+ k--;
+ for (int i = 0; i < operationsSize && i < 60; ++i)
+ if ((k >> i) % 2)
+ ans += operations[i];
+ return (char)('a' + ans % 26);
+}
+
+int main() {
+ int o1[] = {0, 0, 0}, o2[] = {0, 1, 0, 1};
+ printf("%c\n", kthCharacter(5, o1, ARRAY_SIZE(o1))); // expect: a
+ printf("%c\n", kthCharacter(10, o2, ARRAY_SIZE(o2))); // expect: b
+}
diff --git a/find_kth_char_str_game2.py b/find_kth_char_str_game2.py
new file mode 100644
index 0000000..118641e
--- /dev/null
+++ b/find_kth_char_str_game2.py
@@ -0,0 +1,29 @@
+# 3307. Find the K-th Character in String Game II
+
+"""
+alice and bob are playing a game. initially, alice has a string 'word = "a"'.
+you are given a positive integer 'k'. you are also given an integer array
+'operations' where 'operations[i]' represents the type of i'th operation. now
+bob will ask alice to perform all operations in sequence: if 'operations[i]
+== 0', append a copy of 'word' to itself. if 'operations[i] == 1', generate a
+new string by changing each character in 'word' to its next character in the
+english alphabet, and append it to the origin 'word'. return the value of the
+k'th character in 'word' after performing all the operations.
+"""
+
+
+class Solution(object):
+ def kthCharacter(self, k, operations):
+ """
+ :type k: int
+ :type operations: List[int]
+ :rtype: str
+ """
+ ans = (k - 1) & sum(v << i for i, v in enumerate(operations))
+ return chr(ord("a") + ans.bit_count() % 26)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.kthCharacter(k=5, operations=[0, 0, 0]))
+ print(obj.kthCharacter(k=10, operations=[0, 1, 0, 1]))