[LeetCode] Find the Sequence of Strings Appeared on the Screen

3324. Find the Sequence of Strings Appeared on the Screen

You are given a string target.

Alice is going to type target on her computer using a special keyboard that has only two keys:

  • Key 1 appends the character "a" to the string on the screen.
  • Key 2 changes the last character of the string on the screen to its next character in the English alphabet. For example, "c" changes to "d" and "z" changes to "a".

Note that initially there is an empty string "" on the screen, so she can only press key 1.

Return a list of all strings that appear on the screen as Alice types target, in the order they appear, using the minimum key presses.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
vector<string> stringSequence(string target) {
vector<string> res;
string now = "";
for(auto& ch : target) {
now.push_back('#');
for(char c = 'a'; c <= ch; c++) {
now.pop_back();
now.push_back(c);
res.push_back(now);
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/10/20/PS/LeetCode/find-the-sequence-of-strings-appeared-on-the-screen/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.