{"id":"6b093ddebd4755deb67ee07d","slug":"longest-unique-substring","title":"Najdłuższy fragment bez powtórzeń","summary":"Szukamy najdłuższego spójnego fragmentu napisu, w którym żaden znak się nie powtarza. Okno przesuwne z mapą ostatnich wystąpień – w jednym przejściu.","servedLang":"pl","availableLangs":["pl","en"],"kind":"code","category":"algorithms","status":"published","difficulty":"medium","topics":[],"techs":["typescript","javascript","python","dotnet","java","go"],"tags":["sliding-window","two-pointers","hashing","strings"],"languages":[{"lang":"typescript","runner":"browser","starter":"/** Returns the leftmost longest substring of s without repeating characters. */\nfunction longestUniqueSubstring(s: string): string {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":8,"tests":"test('classic examples', () => {\n  expect(longestUniqueSubstring('abcabcbb')).toBe('abc');\n  expect(longestUniqueSubstring('bbbbb')).toBe('b');\n  expect(longestUniqueSubstring('pwwkew')).toBe('wke');\n});\n\ntest('empty and one-character strings', () => {\n  expect(longestUniqueSubstring('')).toBe('');\n  expect(longestUniqueSubstring('x')).toBe('x');\n});\n\ntest('the left edge never moves back', () => {\n  expect(longestUniqueSubstring('abba')).toBe('ab');\n  expect(longestUniqueSubstring('tmmzuxt')).toBe('mzuxt');\n});\n\ntest('repeat in the middle of the window', () => {\n  expect(longestUniqueSubstring('dvdf')).toBe('vdf');\n});\n\ntest('the leftmost of equally long fragments', () => {\n  expect(longestUniqueSubstring('abcdab')).toBe('abcd');\n  expect(longestUniqueSubstring('xyzzyx')).toBe('xyz');\n});\n\ntest('spaces and punctuation are characters too', () => {\n  expect(longestUniqueSubstring('a b!a')).toBe('a b!');\n  expect(longestUniqueSubstring('  ')).toBe(' ');\n});\n\ntest('the whole string when nothing repeats', () => {\n  expect(longestUniqueSubstring('abcdefg')).toBe('abcdefg');\n});\n\ntest('performance: 200 000 characters', () => {\n  let printable = '';\n  for (let c = 32; c <= 126; c++) printable += String.fromCharCode(c);\n  const head = 'ab'.repeat(100000 - 50);\n  expect(longestUniqueSubstring(head + printable)).toBe(printable);\n  expect(longestUniqueSubstring(printable.repeat(2000)).length).toBe(95);\n});\n"},{"lang":"javascript","runner":"browser","starter":"/**\n * Returns the leftmost longest substring of s without repeating characters.\n * @param {string} s\n * @returns {string}\n */\nfunction longestUniqueSubstring(s) {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":8,"tests":"test('classic examples', () => {\n  expect(longestUniqueSubstring('abcabcbb')).toBe('abc');\n  expect(longestUniqueSubstring('bbbbb')).toBe('b');\n  expect(longestUniqueSubstring('pwwkew')).toBe('wke');\n});\n\ntest('empty and one-character strings', () => {\n  expect(longestUniqueSubstring('')).toBe('');\n  expect(longestUniqueSubstring('x')).toBe('x');\n});\n\ntest('the left edge never moves back', () => {\n  expect(longestUniqueSubstring('abba')).toBe('ab');\n  expect(longestUniqueSubstring('tmmzuxt')).toBe('mzuxt');\n});\n\ntest('repeat in the middle of the window', () => {\n  expect(longestUniqueSubstring('dvdf')).toBe('vdf');\n});\n\ntest('the leftmost of equally long fragments', () => {\n  expect(longestUniqueSubstring('abcdab')).toBe('abcd');\n  expect(longestUniqueSubstring('xyzzyx')).toBe('xyz');\n});\n\ntest('spaces and punctuation are characters too', () => {\n  expect(longestUniqueSubstring('a b!a')).toBe('a b!');\n  expect(longestUniqueSubstring('  ')).toBe(' ');\n});\n\ntest('the whole string when nothing repeats', () => {\n  expect(longestUniqueSubstring('abcdefg')).toBe('abcdefg');\n});\n\ntest('performance: 200 000 characters', () => {\n  let printable = '';\n  for (let c = 32; c <= 126; c++) printable += String.fromCharCode(c);\n  const head = 'ab'.repeat(100000 - 50);\n  expect(longestUniqueSubstring(head + printable)).toBe(printable);\n  expect(longestUniqueSubstring(printable.repeat(2000)).length).toBe(95);\n});\n"},{"lang":"python","runner":"browser","starter":"def longest_unique_substring(s: str) -> str:\n    \"\"\"Returns the leftmost longest substring of s without repeating characters.\"\"\"\n    raise NotImplementedError\n","testCount":8,"tests":"def check(s, expected):\n    got = longest_unique_substring(s)\n    assert got == expected, f\"longest_unique_substring({s[:30]!r}): expected {expected[:30]!r}, got {got[:30]!r}\"\n\n\ndef test_classic_examples():\n    check(\"abcabcbb\", \"abc\")\n    check(\"bbbbb\", \"b\")\n    check(\"pwwkew\", \"wke\")\n\n\ndef test_empty_and_one_character():\n    check(\"\", \"\")\n    check(\"x\", \"x\")\n\n\ndef test_left_edge_never_moves_back():\n    check(\"abba\", \"ab\")\n    check(\"tmmzuxt\", \"mzuxt\")\n\n\ndef test_repeat_in_the_middle_of_the_window():\n    check(\"dvdf\", \"vdf\")\n\n\ndef test_leftmost_of_equally_long_fragments():\n    check(\"abcdab\", \"abcd\")\n    check(\"xyzzyx\", \"xyz\")\n\n\ndef test_spaces_and_punctuation():\n    check(\"a b!a\", \"a b!\")\n    check(\"  \", \" \")\n\n\ndef test_whole_string_when_nothing_repeats():\n    check(\"abcdefg\", \"abcdefg\")\n\n\ndef test_performance_200k():\n    printable = \"\".join(chr(c) for c in range(32, 127))\n    head = \"ab\" * (100_000 - 50)\n    check(head + printable, printable)\n    got = longest_unique_substring(printable * 2000)\n    assert len(got) == 95, f\"expected length 95, got {len(got)}\"\n"},{"lang":"csharp","runner":"judge0","starter":"using System;\n\npublic static class Solution\n{\n    /// <summary>Returns the leftmost longest substring of s without repeating characters.</summary>\n    public static string LongestUniqueSubstring(string s)\n    {\n        throw new NotImplementedException();\n    }\n}\n","testCount":8},{"lang":"java","runner":"judge0","starter":"class Solution {\n    /** Returns the leftmost longest substring of s without repeating characters. */\n    static String longestUniqueSubstring(String s) {\n        throw new UnsupportedOperationException(\"Not implemented\");\n    }\n}\n","testCount":8},{"lang":"go","runner":"judge0","starter":"package main\n\n// LongestUniqueSubstring returns the leftmost longest substring of s without repeating characters.\nfunc LongestUniqueSubstring(s string) string {\n\tpanic(\"not implemented\")\n}\n","testCount":8}],"questionCount":null,"xp":20,"stats":{"attempts":0,"solves":0,"solutions":0,"comments":0},"solveRate":null,"publishedAt":"2026-11-27T08:22:54.597Z","requestedLang":"pl","statement":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Generator identyfikatorów sprawdza, jak długi fragment logu da się wziąć bez powtórzenia znaku. Dla napisu "},{"type":"text","text":"s","marks":[{"type":"code"}]},{"type":"text","text":" szukamy najdłuższego "},{"type":"text","text":"spójnego","marks":[{"type":"bold"}]},{"type":"text","text":" fragmentu (podnapisu), w którym każdy znak występuje co najwyżej raz."}]},{"type":"heading","attrs":{"level":2,"id":"zadanie"},"content":[{"type":"text","text":"Zadanie"}]},{"type":"paragraph","content":[{"type":"text","text":"Funkcja zwraca sam fragment (nie długość). Gdy kilka fragmentów ma tę samą, maksymalną długość, zwracamy "},{"type":"text","text":"pierwszy od lewej","marks":[{"type":"bold"}]},{"type":"text","text":". Dla pustego napisu – pusty napis. Spacje i znaki interpunkcyjne są zwykłymi znakami."}]},{"type":"paragraph","content":[{"type":"text","text":"Sygnatury: "},{"type":"text","text":"longestUniqueSubstring(s)","marks":[{"type":"code"}]},{"type":"text","text":" (JS/TS), "},{"type":"text","text":"longest_unique_substring(s)","marks":[{"type":"code"}]},{"type":"text","text":" (Python), "},{"type":"text","text":"Solution.LongestUniqueSubstring","marks":[{"type":"code"}]},{"type":"text","text":" (C#), "},{"type":"text","text":"Solution.longestUniqueSubstring","marks":[{"type":"code"}]},{"type":"text","text":" (Java), "},{"type":"text","text":"LongestUniqueSubstring","marks":[{"type":"code"}]},{"type":"text","text":" (Go)."}]},{"type":"heading","attrs":{"level":2,"id":"przyklady"},"content":[{"type":"text","text":"Przykłady"}]},{"type":"table","content":[{"type":"tableRow","content":[{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"s","marks":[{"type":"code"}]}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"Wynik"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"abcabcbb\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"abc\"","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"bbbbb\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"b\"","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"pwwkew\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"wke\"","marks":[{"type":"code"}]},{"type":"text","text":" – "},{"type":"text","text":"\"pwke\"","marks":[{"type":"code"}]},{"type":"text","text":" nie jest spójnym fragmentem"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"abba\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"ab\"","marks":[{"type":"code"}]},{"type":"text","text":" – "},{"type":"text","text":"\"ba\"","marks":[{"type":"code"}]},{"type":"text","text":" jest tak samo długie, ale dalej"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"dvdf\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"vdf\"","marks":[{"type":"code"}]}]}]}]}]},{"type":"heading","attrs":{"level":2,"id":"ograniczenia"},"content":[{"type":"text","text":"Ograniczenia"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"0 ≤ s.length ≤ 200 000","marks":[{"type":"code"}]},{"type":"text","text":", znaki ASCII (kody 32–126)."}]}]}]},{"type":"heading","attrs":{"level":2,"id":"oczekiwana-zlozonosc"},"content":[{"type":"text","text":"Oczekiwana złożoność"}]},{"type":"paragraph","content":[{"type":"text","text":"Czas "},{"type":"text","text":"O(n)","marks":[{"type":"bold"}]},{"type":"text","text":", pamięć "},{"type":"text","text":"O(σ)","marks":[{"type":"bold"}]},{"type":"text","text":", gdzie "},{"type":"text","text":"σ","marks":[{"type":"code"}]},{"type":"text","text":" to rozmiar alfabetu (tu najwyżej 95)."}]}]},"hints":[],"hintsCount":3,"quiz":null,"solutionNotes":null,"unlocked":false,"external":[{"provider":"leetcode","url":"https://leetcode.com/problems/longest-substring-without-repeating-characters/","title":"LeetCode 3 – Longest Substring Without Repeating Characters","difficulty":null}],"articles":[],"topicsInfo":[],"author":{"id":"6b08bd2e80884893657cc8cf","name":"Redakcja Art of Devs","avatar":null,"handle":"redakcja","community":false},"capabilities":{"judge0":false,"aiReview":false},"createdAt":"2026-11-27T08:22:54.599Z","updatedAt":"2026-11-27T08:22:54.599Z"}