3. Longest substring without repetitions

#include <string.h>

int lengthOfLongestSubstring(char *s) {
    int lastSeen[256];
    memset(lastSeen, -1, sizeof(lastSeen));

    int maxLen = 0, left = 0;
    int len = strlen(s);

    for (int right = 0; right < len; right++) {
        unsigned char c = s[right];

        if (lastSeen[c] >= left) {
            left = lastSeen[c] + 1;
        }

        lastSeen[c] = right;

        if (right - left + 1 > maxLen) {
            maxLen = right - left + 1;
        }
    }

    return maxLen;
}
#include <string>
#include <unordered_map>
using namespace std;

class Solution {
public:
  int lengthOfLongestSubstring(string s) {
    unordered_map<char, int> lastSeen;
    int maxLen = 0, left = 0;

    for (int right = 0; right < s.size(); right++) {
      char c = s[right];
      if (lastSeen.count(c) && lastSeen[c] >= left) {
        left = lastSeen[c] + 1;
      }
      lastSeen[c] = right;
      maxLen = max(maxLen, right - left + 1);
    }

    return maxLen;
  }
};
package main

func lengthOfLongestSubstring(s string) int {
    lastSeen := make(map[byte]int)
    maxLen, left := 0, 0

    for right := 0; right < len(s); right++ {
        c := s[right]

        if idx, ok := lastSeen[c]; ok && idx >= left {
            left = idx + 1
        }

        lastSeen[c] = right

        if length := right - left + 1; length > maxLen {
            maxLen = length
        }
    }

    return maxLen
}
import java.util.HashMap;
import java.util.Map;

class Solution {
    public int lengthOfLongestSubstring(String s) {
        Map<Character, Integer> lastSeen = new HashMap<>();
        int maxLen = 0, left = 0;

        for (int right = 0; right < s.length(); right++) {
            char c = s.charAt(right);

            if (lastSeen.containsKey(c) && lastSeen.get(c) >= left) {
                left = lastSeen.get(c) + 1;
            }

            lastSeen.put(c, right);
            maxLen = Math.max(maxLen, right - left + 1);
        }

        return maxLen;
    }
}
class Solution {
  lengthOfLongestSubstring(s) {
    const lastSeen = new Map();
    let maxLen = 0;
    let left = 0;

    for (let right = 0; right < s.length; right++) {
      const c = s[right];

      if (lastSeen.has(c) && lastSeen.get(c) >= left) {
        left = lastSeen.get(c) + 1;
      }

      lastSeen.set(c, right);
      maxLen = Math.max(maxLen, right - left + 1);
    }

    return maxLen;
  }
}
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        last_seen = {}
        max_len = 0
        left = 0

        for right, c in enumerate(s):
            if c in last_seen and last_seen[c] >= left:
                left = last_seen[c] + 1

            last_seen[c] = right
            max_len = max(max_len, right - left + 1)

        return max_len
use std::collections::HashMap;

struct Solution;

impl Solution {
    fn length_of_longest_substring(s: String) -> i32 {
        let bytes = s.as_bytes();
        let mut last_seen: HashMap<u8, i32> = HashMap::new();
        let mut max_len = 0;
        let mut left: i32 = 0;

        for right in 0..bytes.len() {
            let c = bytes[right];

            if let Some(&idx) = last_seen.get(&c) {
                if idx >= left {
                    left = idx + 1;
                }
            }

            last_seen.insert(c, right as i32);
            max_len = max_len.max(right as i32 - left + 1);
        }

        max_len
    }
}