Find the Sequence of Strings Appeared on the Screen

MediumStringSimulation

Solution

export function stringSequence(target: string): string[] {
  const answer = [];
  let s = '';
  for (const char of target) {
    for (const alphabet of toAlphabet(char)) {
      answer.push(s + alphabet);
    }
    s += char;
  }
  return answer;
}
 
function* toAlphabet(char: string) {
  const alphabets = 'abcdefghijklmnopqrstuvwxyz';
  for (const alphabet of alphabets) {
    if (char < alphabet) return;
    yield alphabet;
  }
}

Complexity

  • Time: O(n)O(n)
  • Space: O(1)O(1), 정답에 대한 복잡도 O(n)O(n)