3324. Find the Sequence of Strings Appeared on the Screen LeetCode Solution
In this guide, you will get 3324. Find the Sequence of Strings Appeared on the Screen LeetCode Solution with the best time and space complexity. The solution to Find the Sequence of Strings Appeared on the Screen problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find the Sequence of Strings Appeared on the Screen solution in C++
- Find the Sequence of Strings Appeared on the Screen solution in Java
- Find the Sequence of Strings Appeared on the Screen solution in Python
- Additional Resources

Problem Statement of 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.
Example 1:
Input: target = “abc”
Output: [“a”,”aa”,”ab”,”aba”,”abb”,”abc”]
Explanation:
The sequence of key presses done by Alice are:
Press key 1, and the string on the screen becomes “a”.
Press key 1, and the string on the screen becomes “aa”.
Press key 2, and the string on the screen becomes “ab”.
Press key 1, and the string on the screen becomes “aba”.
Press key 2, and the string on the screen becomes “abb”.
Press key 2, and the string on the screen becomes “abc”.
Example 2:
Input: target = “he”
Output: [“a”,”b”,”c”,”d”,”e”,”f”,”g”,”h”,”ha”,”hb”,”hc”,”hd”,”he”]
Constraints:
1 <= target.length <= 400
target consists only of lowercase English letters.
Complexity Analysis
- Time Complexity: O(26n^2) = O(n^2)
- Space Complexity: O(26n^2) = O(n^2)
3324. Find the Sequence of Strings Appeared on the Screen LeetCode Solution in C++
class Solution {
public:
vector<string> stringSequence(string target) {
vector<string> ans;
string s;
for (const char targetChar : target) {
s += 'a';
ans.push_back(s);
for (char c = 'b'; c <= targetChar; c++) {
s.back() = c;
ans.push_back(s);
}
}
return ans;
}
};
/* code provided by PROGIEZ */
3324. Find the Sequence of Strings Appeared on the Screen LeetCode Solution in Java
class Solution {
public List<String> stringSequence(String target) {
List<String> ans = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (final char targetChar : target.toCharArray()) {
sb.append('a');
ans.add(sb.toString());
for (char c = 'b'; c < targetChar; ++c) {
sb.setCharAt(sb.length() - 1, c);
ans.add(sb.toString());
}
}
return ans;
}
}
// code provided by PROGIEZ
3324. Find the Sequence of Strings Appeared on the Screen LeetCode Solution in Python
class Solution:
def stringSequence(self, target: str) -> list[str]:
ans = []
s = []
for targetChar in target:
s.append('a')
ans.append(''.join(s))
for offset in range(ord('b'), ord(targetChar) + 1):
s[-1] = chr(offset)
ans.append(''.join(s))
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.