{"id":"6b093de0bd4755deb67ee0b0","slug":"word-break","title":"Podział hashtagu na słowa","summary":"Czy napis bez spacji (np. hashtag) da się podzielić na słowa ze słownika? Programowanie dynamiczne zamiast wykładniczego przeszukiwania z nawrotami.","servedLang":"pl","availableLangs":["pl","en"],"kind":"code","category":"algorithms","status":"published","difficulty":"medium","topics":[],"techs":["typescript","javascript","python","dotnet","java","go"],"tags":["dynamic-programming","strings","hashing","memoization"],"languages":[{"lang":"typescript","runner":"browser","starter":"/** true when text can be split into a sequence of dictionary words (words may repeat). */\nfunction wordBreak(text: string, words: string[]): boolean {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":6,"tests":"test('simple splits', () => {\n  expect(wordBreak('cleancode', ['clean', 'code'])).toBe(true);\n  expect(wordBreak('applepenapple', ['apple', 'pen'])).toBe(true);\n});\n\ntest('no split exists', () => {\n  expect(wordBreak('catsandog', ['cats', 'dog', 'sand', 'and', 'cat'])).toBe(false);\n  expect(wordBreak('abc', [])).toBe(false);\n});\n\ntest('greedy choice is not enough', () => {\n  expect(wordBreak('goalspecial', ['go', 'goal', 'goals', 'special'])).toBe(true);\n  expect(wordBreak('aaaaaaa', ['aaaa', 'aaa'])).toBe(true);\n  expect(wordBreak('aaaaaaa', ['aaaa', 'aa'])).toBe(false);\n});\n\ntest('empty text', () => {\n  expect(wordBreak('', ['a'])).toBe(true);\n  expect(wordBreak('', [])).toBe(true);\n});\n\ntest('the whole text must be used', () => {\n  expect(wordBreak('codereview', ['code', 'review', 'rev'])).toBe(true);\n  expect(wordBreak('codereviews', ['code', 'review'])).toBe(false);\n});\n\ntest('performance: exponential trap and a long text', () => {\n  const dict = Array.from({ length: 10 }, (_, i) => 'a'.repeat(i + 1));\n  expect(wordBreak('a'.repeat(200) + 'b', dict)).toBe(false);\n  expect(wordBreak('a'.repeat(5000), dict)).toBe(true);\n  const words = ['data', 'base', 'database', 'driven', 'design', 'domain', 'do', 'main'];\n  expect(wordBreak('domaindrivendatabasedesign'.repeat(150), words)).toBe(true);\n  expect(wordBreak('domaindrivendatabasedesign'.repeat(150) + 'x', words)).toBe(false);\n});\n"},{"lang":"javascript","runner":"browser","starter":"/**\n * true when text can be split into a sequence of dictionary words (words may repeat).\n * @param {string} text\n * @param {string[]} words\n * @returns {boolean}\n */\nfunction wordBreak(text, words) {\n  // TODO\n  throw new Error('Not implemented');\n}\n","testCount":6,"tests":"test('simple splits', () => {\n  expect(wordBreak('cleancode', ['clean', 'code'])).toBe(true);\n  expect(wordBreak('applepenapple', ['apple', 'pen'])).toBe(true);\n});\n\ntest('no split exists', () => {\n  expect(wordBreak('catsandog', ['cats', 'dog', 'sand', 'and', 'cat'])).toBe(false);\n  expect(wordBreak('abc', [])).toBe(false);\n});\n\ntest('greedy choice is not enough', () => {\n  expect(wordBreak('goalspecial', ['go', 'goal', 'goals', 'special'])).toBe(true);\n  expect(wordBreak('aaaaaaa', ['aaaa', 'aaa'])).toBe(true);\n  expect(wordBreak('aaaaaaa', ['aaaa', 'aa'])).toBe(false);\n});\n\ntest('empty text', () => {\n  expect(wordBreak('', ['a'])).toBe(true);\n  expect(wordBreak('', [])).toBe(true);\n});\n\ntest('the whole text must be used', () => {\n  expect(wordBreak('codereview', ['code', 'review', 'rev'])).toBe(true);\n  expect(wordBreak('codereviews', ['code', 'review'])).toBe(false);\n});\n\ntest('performance: exponential trap and a long text', () => {\n  const dict = Array.from({ length: 10 }, (_, i) => 'a'.repeat(i + 1));\n  expect(wordBreak('a'.repeat(200) + 'b', dict)).toBe(false);\n  expect(wordBreak('a'.repeat(5000), dict)).toBe(true);\n  const words = ['data', 'base', 'database', 'driven', 'design', 'domain', 'do', 'main'];\n  expect(wordBreak('domaindrivendatabasedesign'.repeat(150), words)).toBe(true);\n  expect(wordBreak('domaindrivendatabasedesign'.repeat(150) + 'x', words)).toBe(false);\n});\n"},{"lang":"python","runner":"browser","starter":"def word_break(text: str, words: list[str]) -> bool:\n    \"\"\"True when text can be split into a sequence of dictionary words (words may repeat).\"\"\"\n    raise NotImplementedError\n","testCount":6,"tests":"def check(text, words, expected):\n    got = word_break(text, words)\n    assert got is expected, f\"word_break({text[:30]!r}, {words[:6]}): expected {expected}, got {got}\"\n\n\ndef test_simple_splits():\n    check(\"cleancode\", [\"clean\", \"code\"], True)\n    check(\"applepenapple\", [\"apple\", \"pen\"], True)\n\n\ndef test_no_split_exists():\n    check(\"catsandog\", [\"cats\", \"dog\", \"sand\", \"and\", \"cat\"], False)\n    check(\"abc\", [], False)\n\n\ndef test_greedy_choice_is_not_enough():\n    check(\"goalspecial\", [\"go\", \"goal\", \"goals\", \"special\"], True)\n    check(\"aaaaaaa\", [\"aaaa\", \"aaa\"], True)\n    check(\"aaaaaaa\", [\"aaaa\", \"aa\"], False)\n\n\ndef test_empty_text():\n    check(\"\", [\"a\"], True)\n    check(\"\", [], True)\n\n\ndef test_whole_text_must_be_used():\n    check(\"codereview\", [\"code\", \"review\", \"rev\"], True)\n    check(\"codereviews\", [\"code\", \"review\"], False)\n\n\ndef test_performance_exponential_trap_and_long_text():\n    words = [\"a\" * (i + 1) for i in range(10)]\n    check(\"a\" * 200 + \"b\", words, False)\n    check(\"a\" * 5000, words, True)\n    ddd = [\"data\", \"base\", \"database\", \"driven\", \"design\", \"domain\", \"do\", \"main\"]\n    check(\"domaindrivendatabasedesign\" * 150, ddd, True)\n    check(\"domaindrivendatabasedesign\" * 150 + \"x\", ddd, False)\n"},{"lang":"csharp","runner":"judge0","starter":"using System;\n\npublic static class Solution\n{\n    /// <summary>true when text can be split into a sequence of dictionary words (words may repeat).</summary>\n    public static bool WordBreak(string text, string[] words)\n    {\n        throw new NotImplementedException();\n    }\n}\n","testCount":6},{"lang":"java","runner":"judge0","starter":"class Solution {\n    /** true when text can be split into a sequence of dictionary words (words may repeat). */\n    static boolean wordBreak(String text, String[] words) {\n        throw new UnsupportedOperationException(\"Not implemented\");\n    }\n}\n","testCount":6},{"lang":"go","runner":"judge0","starter":"package main\n\n// WordBreak reports whether text can be split into a sequence of dictionary words (words may repeat).\nfunc WordBreak(text string, words []string) bool {\n\tpanic(\"not implemented\")\n}\n","testCount":6}],"questionCount":null,"xp":20,"stats":{"attempts":0,"solves":0,"solutions":0,"comments":0},"solveRate":null,"publishedAt":"2026-11-27T08:22:56.273Z","requestedLang":"pl","statement":{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Moduł analityki dzieli hashtagi na słowa, żeby je zindeksować: "},{"type":"text","text":"#cleancodematters","marks":[{"type":"code"}]},{"type":"text","text":" → „clean code matters”. Pierwszy krok to rozstrzygnięcie, czy podział w ogóle istnieje: czy napis da się złożyć "},{"type":"text","text":"w całości","marks":[{"type":"bold"}]},{"type":"text","text":" z kolejnych słów słownika (słowa mogą się powtarzać)."}]},{"type":"heading","attrs":{"level":2,"id":"zadanie"},"content":[{"type":"text","text":"Zadanie"}]},{"type":"paragraph","content":[{"type":"text","text":"Funkcja dostaje napis "},{"type":"text","text":"text","marks":[{"type":"code"}]},{"type":"text","text":" (małe litery, bez spacji) i listę słów "},{"type":"text","text":"words","marks":[{"type":"code"}]},{"type":"text","text":". Zwraca "},{"type":"text","text":"true","marks":[{"type":"code"}]},{"type":"text","text":", jeśli "},{"type":"text","text":"text","marks":[{"type":"code"}]},{"type":"text","text":" można podzielić na ciąg słów z "},{"type":"text","text":"words","marks":[{"type":"code"}]},{"type":"text","text":", w przeciwnym razie "},{"type":"text","text":"false","marks":[{"type":"code"}]},{"type":"text","text":". Pusty napis zawsze da się podzielić (na zero słów)."}]},{"type":"paragraph","content":[{"type":"text","text":"Sygnatury: "},{"type":"text","text":"wordBreak(text, words)","marks":[{"type":"code"}]},{"type":"text","text":" (JS/TS), "},{"type":"text","text":"word_break(text, words)","marks":[{"type":"code"}]},{"type":"text","text":" (Python), "},{"type":"text","text":"Solution.WordBreak","marks":[{"type":"code"}]},{"type":"text","text":" (C#), "},{"type":"text","text":"Solution.wordBreak","marks":[{"type":"code"}]},{"type":"text","text":" (Java), "},{"type":"text","text":"WordBreak","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":"text","marks":[{"type":"code"}]}]}]},{"type":"tableHeader","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"words","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":"\"cleancode\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[\"clean\", \"code\"]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"true","marks":[{"type":"code"}]}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"applepenapple\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[\"apple\", \"pen\"]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"true","marks":[{"type":"code"}]},{"type":"text","text":" – słowa mogą się powtarzać"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"goalspecial\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[\"go\", \"goal\", \"goals\", \"special\"]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"true","marks":[{"type":"code"}]},{"type":"text","text":" – „goal” + „special”"}]}]}]},{"type":"tableRow","content":[{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"\"catsandog\"","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"[\"cats\", \"dog\", \"sand\", \"and\", \"cat\"]","marks":[{"type":"code"}]}]}]},{"type":"tableCell","attrs":{"colspan":1,"rowspan":1},"content":[{"type":"paragraph","content":[{"type":"text","text":"false","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 ≤ text.length ≤ 5 000","marks":[{"type":"code"}]},{"type":"text","text":", do 1 000 słów, każde o długości 1–20."}]}]}]},{"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 · L)","marks":[{"type":"bold"}]},{"type":"text","text":" sprawdzeń w zbiorze ("},{"type":"text","text":"L","marks":[{"type":"code"}]},{"type":"text","text":" – długość najdłuższego słowa; każde sprawdzenie kosztuje O(L) na wycięcie fragmentu), pamięć "},{"type":"text","text":"O(n + rozmiar słownika)","marks":[{"type":"bold"}]},{"type":"text","text":"."}]}]},"hints":[],"hintsCount":3,"quiz":null,"solutionNotes":null,"unlocked":false,"external":[{"provider":"leetcode","url":"https://leetcode.com/problems/word-break/","title":"LeetCode 139 – Word Break","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:56.274Z","updatedAt":"2026-11-27T08:22:56.274Z"}