Skip to content

3324. Find the Sequence of Strings Appeared on the Screen 👍

  • Time: $O(26n^2) = O(n^2)$
  • Space: $O(26n^2) = O(n^2)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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