[리트코드] Generate Parentheses, Partition Labels




22. Generate Parentheses

리트코드

나의 풀이

/**
 * @param {number} n
 * @return {string[]}
 */
const generateParenthesis = function (n) {
  let answer = [];
  let left = n;
  let right = 0;

  const dfs = (left, right, str) => {
    if (left == 0 && right == 0) {
      answer.push(str);
      return;
    }
    if (left > 0) {
      dfs(left - 1, right + 1, str + '(');
    }

    if (right > 0) {
      dfs(left, right - 1, str + ')');
    }
  };

  dfs(left, right, '');
  return answer;
};



763. Partition Labels

리트코드

const partitionLabels = function (s) {
  const answer = [];
  let index = 0;

  while (index < s.length) {
    let start = index;
    let target = s.lastIndexOf(s[index]);

    while (index < target) {
      target = Math.max(target, s.lastIndexOf(s[index++]));
    }
    index++;
    answer.push(target - start + 1);
  }

  return answer;
};

console.log(partitionLabels('ababcbacadefegdehijhklij'));