Post

[LeetCode 3307] Find the K-th Character in String Game II

LeetCode 3307 (Java)
[Find the K-th Character in String Game II] 문제 풀이

[LeetCode 3307] Find the K-th Character in String Game II

문제 바로가기


Description


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 the ith operation.

Now Bob will ask Alice to perform all operations in sequence:

  • If operations[i] == 0append 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 original word. For example, performing the operation on "c" generates "cd" and performing the operation on "zb" generates "zbac".

Return the value of the kth character in word after performing all the operations.

Note that the character 'z' can be changed to 'a' in the second type of operation.


Example 1


  • Input: k = 5, operations = [0,0,0]
  • Output: ”a”
  • Explanation:
    • Initially, word == "a". Alice performs the three operations as follows:
      • Appends "a" to "a"word becomes "aa".
      • Appends "aa" to "aa"word becomes "aaaa".
      • Appends "aaaa" to "aaaa"word becomes "aaaaaaaa".


Example 2


  • Input: k = 10, operations = [0,1,0,1]
  • Output: ”b”
  • Explanation:
    • Initially, word == "a". Alice performs the four operations as follows:
    • Appends "a" to "a"word becomes "aa".
    • Appends "bb" to "aa"word becomes "aabb".
    • Appends "aabb" to "aabb"word becomes "aabbaabb".
    • Appends "bbccbbcc" to "aabbaabb"word becomes "aabbaabbbbccbbcc".


Constraints


  • 1 <= k <= 10^14
  • 1 <= operations.length <= 100
  • operations[i] is either 0 or 1.
  • The input is generated such that word has at least k characters after all operations.


Hint


Hint 1
  Try to replay the operations `k^th` character was part of.
	
Hint 2
  The `k^th` character is only affected if it is present in the first half of the string.
	







Code


내 제출


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
    public char kthCharacter(long k, int[] operations) {
        long n = 1;
        int i = 0;
        while (n < k) {
            n *= 2;
            ++i;
        }
        int d = 0;
        while (n > 1) {
            if (k > n / 2) {
                k -= n / 2;
                d += operations[i - 1];
            }
            n /= 2;
            --i;
        }
        return (char) ('a' + (d % 26));
    }
}


RuntimeMemory
1 ms43.1 MB


다른 풀이


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
    public char kthCharacter(long k, int[] arr) {
        int c = 0; k--;
        for (int i = 0; k != 0; i++, k >>= 1) 
        {
            c += ((int)(k & 1) & arr[i]);
        }
        return (char)((c % 26) + 'a');
    }
}
/*class Solution {
    public char kthCharacter(long k, int[] operations) {
        String ans = "a";

        for (int i = 0; i < operations.length; i++) {

            if (operations[i] == 0) {
                ans = ans + ans;
            } else {
                String res = "";
                for (int j = 0; j < ans.length(); j++) {
                    res += (char)(ans.charAt(j) + 1);
                }
                ans = ans + res;
            }

            if (ans.length() >= k) {
                return ans.charAt((int)k - 1);
            }
        }

        return ans.charAt((int)k - 1);
    }
}*/


Reference


  • https://github.com/doocs/leetcode/blob/main/solution/3300-3399/3307.Find%20the%20K-th%20Character%20in%20String%20Game%20II/Solution.java
This post is licensed under CC BY 4.0 by the author.