aboutsummaryrefslogtreecommitdiff
path: root/find_kth_char_str_game1.c
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-07-02 18:28:10 -0600
committerJack Sangdahl <[email protected]>2025-07-02 18:28:10 -0600
commitcd518a4b7fb4fb8276751af7c0d7294389f34632 (patch)
tree08579b44b4032835fb3bac311ef9a2621edf1cbc /find_kth_char_str_game1.c
parent0d48f3681fc9ee8ef96d05537214aaf5d200927b (diff)
downloadleetcode-cd518a4b7fb4fb8276751af7c0d7294389f34632.tar.gz
leetcode-cd518a4b7fb4fb8276751af7c0d7294389f34632.zip
3304. find the kth character in string game 1
Diffstat (limited to 'find_kth_char_str_game1.c')
-rw-r--r--find_kth_char_str_game1.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/find_kth_char_str_game1.c b/find_kth_char_str_game1.c
new file mode 100644
index 0000000..a398db6
--- /dev/null
+++ b/find_kth_char_str_game1.c
@@ -0,0 +1,19 @@
+// 3304. Find the K-th Character in String Game I
+#include "leetcode.h"
+
+/*
+ * 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.
+ */
+
+char kthCharacter(int k) { return 'a' + __builtin_popcount(k - 1); }
+
+int main() {
+ printf("%c\n", kthCharacter(5)); // expect: b
+ printf("%c\n", kthCharacter(10)); // expect: c
+}