3614. Process String with Special Operations II
Description
You are given a string s consisting of lowercase English letters and the special characters: '*', '#', and '%'.
You are also given an integer k.
Build a new string result by processing s according to the following rules from left to right:
- If the letter is a lowercase English letter append it to
result. - A
'*'removes the last character fromresult, if it exists. - A
'#'duplicates the currentresultand appends it to itself. - A
'%'reverses the currentresult.
Return the kth character of the final string result. If k is out of the bounds of result, return '.'.
Β
Example 1:
Input: s = "a#b%*", k = 1
Output: "a"
Explanation:
i | s[i] | Operation | Current result |
|---|---|---|---|
| 0 | 'a' | Append 'a' | "a" |
| 1 | '#' | Duplicate result | "aa" |
| 2 | 'b' | Append 'b' | "aab" |
| 3 | '%' | Reverse result | "baa" |
| 4 | '*' | Remove the last character | "ba" |
The final result is "ba". The character at index k = 1 is 'a'.
Example 2:
Input: s = "cd%#*#", k = 3
Output: "d"
Explanation:
i | s[i] | Operation | Current result |
|---|---|---|---|
| 0 | 'c' | Append 'c' | "c" |
| 1 | 'd' | Append 'd' | "cd" |
| 2 | '%' | Reverse result | "dc" |
| 3 | '#' | Duplicate result | "dcdc" |
| 4 | '*' | Remove the last character | "dcd" |
| 5 | '#' | Duplicate result | "dcddcd" |
The final result is "dcddcd". The character at index k = 3 is 'd'.
Example 3:
Input: s = "z*#", k = 0
Output: "."
Explanation:
i | s[i] | Operation | Current result |
|---|---|---|---|
| 0 | 'z' | Append 'z' | "z" |
| 1 | '*' | Remove the last character | "" |
| 2 | '#' | Duplicate the string | "" |
The final result is "". Since index k = 0 is out of bounds, the output is '.'.
Β
Constraints:
1 <= s.length <= 105sconsists of only lowercase English letters and special characters'*','#', and'%'.0 <= k <= 1015- The length of
resultafter processingswill not exceed1015.
Solutions
Solution 1: Reverse Tracking
We first calculate the length \(m\) of the processed result string \(\textit{result}\). If \(k \geq m\), it indicates that \(k\) exceeds the valid indices of the result string, so we return '.'.
Otherwise, we traverse the string \(s\) in reverse order and handle each character based on the following cases:
- If \(s[i]\) is '*', we increase \(m\) by \(1\).
- If \(s[i]\) is '#', we divide \(m\) by \(2\). At this point, if \(k \geq m\), we subtract \(m\) from \(k\).
- If \(s[i]\) is '%', we update \(k\) to \(m - 1 - k\).
- Otherwise, \(s[i]\) is a letter. We decrease \(m\) by \(1\). If \(k = m\), it means we have found the \(k\)-th character, so we return \(s[i]\).
The time complexity is \(O(n)\), where \(n\) is the length of the string \(s\). The space complexity is \(O(1)\).
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 | |
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 36 | |
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 36 | |
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 36 37 38 | |
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 36 | |