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

Longest Substring Without Repeating Characters

בינוני
שאלה מראיונות עבודה ממאגר שאלות של LeetCode שאלה מספר 3 נשאל ב: Microsoft
Given a string `s`, find the length of the longest substring without repeating characters.

פתרון קוד

JavaScript
Python
function lengthOfLongestSubstring(s) {
  let maxLen = 0;
  let start = 0;
  const lastSeen = new Map();

  for (let i = 0; i < s.length; i++) {
    const ch = s[i];
    if (lastSeen.has(ch) && lastSeen.get(ch) >= start) {
      start = lastSeen.get(ch) + 1;
    }
    lastSeen.set(ch, i);
    maxLen = Math.max(maxLen, i - start + 1);
  }

  return maxLen;
}

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

Book Cover

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

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

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