🤖המדריך להייטיקיסט המתחיל

Letter Combinations of a Phone Number

בינוני
שאלה מראיונות עבודה ממאגר שאלות של LeetCode שאלה מספר 17
Given a string containing digits from `2-9` inclusive, return all possible letter combinations that the number could represent.

פתרון קוד

JavaScript
Python
function letterCombinations(digits) {
  if (!digits.length) return [];
  const mapping = {
    "2": "abc",
    "3": "def",
    "4": "ghi",
    "5": "jkl",
    "6": "mno",
    "7": "pqrs",
    "8": "tuv",
    "9": "wxyz",
  };
  const result = [];
  const path = [];

  function backtrack(index) {
    if (index === digits.length) {
      result.push(path.join(""));
      return;
    }
    for (const char of mapping[digits[index]]) {
      path.push(char);
      backtrack(index + 1);
      path.pop();
    }
  }

  backtrack(0);
  return result;
}

הסבר וידאו כיצד לפתור את השאלה

Book Cover

לעבור את ראיון העבודה הבא שלך בהצלחה

קורס דיגיטלי מקיף עם +25 שיעורים מעשיים, כשעתיים של וידאו, וליווי של מראיין בכיר.

התחל עכשיו ב-99 ₪ בלבד! 🚀