{"id":22513490,"url":"https://github.com/emahtab/word-break","last_synced_at":"2026-02-11T15:01:37.516Z","repository":{"id":79525985,"uuid":"259707852","full_name":"eMahtab/word-break","owner":"eMahtab","description":"Word Break","archived":false,"fork":false,"pushed_at":"2021-09-10T05:45:22.000Z","size":16,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-10T16:34:01.707Z","etag":null,"topics":["dynamic-programming","leetcode","problem-solving","word-break"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-04-28T17:37:12.000Z","updated_at":"2021-09-10T05:45:25.000Z","dependencies_parsed_at":"2023-03-18T13:41:30.465Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/word-break","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/word-break","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fword-break","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fword-break/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fword-break/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fword-break/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/word-break/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fword-break/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29336006,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-11T14:34:07.188Z","status":"ssl_error","status_checked_at":"2026-02-11T14:34:06.809Z","response_time":97,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dynamic-programming","leetcode","problem-solving","word-break"],"created_at":"2024-12-07T03:12:45.016Z","updated_at":"2026-02-11T15:01:37.511Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Word Break\n## https://leetcode.com/problems/word-break\n\nGiven a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.\n\n**Note:**\n\n1. The same word in the dictionary may be reused multiple times in the segmentation.\n2. You may assume the dictionary does not contain duplicate words.\n```\nExample 1:\n\nInput: s = \"leetcode\", wordDict = [\"leet\", \"code\"]\nOutput: true\nExplanation: Return true because \"leetcode\" can be segmented as \"leet code\".\n\nExample 2:\n\nInput: s = \"applepenapple\", wordDict = [\"apple\", \"pen\"]\nOutput: true\nExplanation: Return true because \"applepenapple\" can be segmented as \"apple pen apple\".\n             Note that you are allowed to reuse a dictionary word.\n\nExample 3:\n\nInput: s = \"catsandog\", wordDict = [\"cats\", \"dog\", \"sand\", \"and\", \"cat\"]\nOutput: false\n```\n\n## Initial Thought Process : Will not work\nTry to match dictionary words in the given string, if the dictionary word matches, update the given string by removing the matched dictionary word.\nSince we can use same dictionary word multiple times to create the given string, try to find a match of the dictionary word in the given string as many times as we can.\nIf the given string becomes an empty string after update, it means we can create the given string from the given dictionary words, so we return true.   \n\nBut soon we realize, that this approach is not going to work.\n```\nString s \"cars\"  ,  wordDict = [\"car\",\"ca\",\"rs\"]\n\nAfter `car` is matched the updated string will become just `s` and our program will return false, since there is no single `s` in wordDict.\n```\n\n# Implementation 1 : Recursive (Time Limit Exceeded 😀)\n```java\nclass Solution {\n    public boolean wordBreak(String s, List\u003cString\u003e wordDict) {\n        if(s == null)\n            return false;\n        // Add the dictionary words to a set for faster lookup\n        Set\u003cString\u003e set = new HashSet\u003c\u003e();\n        for(String str : wordDict)\n            set.add(str);\n        \n        return helper(s, set);\n    }\n    \n    private boolean helper(String s, Set\u003cString\u003e set) {\n        if(s.length() == 0)\n            return true;\n        for(int i = 0; i \u003c s.length(); i++) {\n            String s1 = s.substring(0, i+1);\n            if(set.contains(s1) \u0026\u0026 helper(s.substring(i+1), set))\n                return true;\n        }\n        return false;\n    }\n}\n```\n\n# Implementation 2 : Memoization\n```java\nclass Solution {\n    public boolean wordBreak(String s, List\u003cString\u003e wordDict) {\n        if(s == null)\n            return false;\n        // Add the dictionary words to a set for faster lookup\n        Set\u003cString\u003e set = new HashSet\u003c\u003e();\n        for(String str : wordDict)\n            set.add(str);\n        Map\u003cString,Boolean\u003e map = new HashMap\u003c\u003e();\n        return helper(s, set, map);\n    }\n    \n    private boolean helper(String s, Set\u003cString\u003e set, Map\u003cString, Boolean\u003e map) {\n        if(s.length() == 0)\n            return true;\n        if(map.containsKey(s))\n            return map.get(s);\n        for(int i = 0; i \u003c s.length(); i++) {\n            String s1 = s.substring(0, i+1);\n            if(set.contains(s1) \u0026\u0026 helper(s.substring(i+1), set, map)) {\n                map.put(s, true);\n                return true;\n            }\n        }\n        map.put(s, false);\n        return false;\n    }\n}\n```\n\n# Implementation 3 : Dynamic Programming\n```java\nclass Solution {\n    public boolean wordBreak(String s, List\u003cString\u003e wordDict) {\n        Set\u003cString\u003e wordDictSet=new HashSet(wordDict);\n        boolean[] isWordBreak = new boolean[s.length()+1];\n        isWordBreak[0] = true;\n        \n        for(int i = 1; i \u003c= s.length(); i++) {\n            for(int j = 0; j \u003c i; j++) {\n                if(isWordBreak[j] \u0026\u0026 wordDictSet.contains(s.substring(j,i))) {\n                    isWordBreak[i] = true;\n                    break;\n                }\n            }\n        }\n        return isWordBreak[s.length()];\n    }\n}\n```\n\n# References :\n1. https://www.youtube.com/watch?v=hLQYQ4zj0qg (Very good explanation)\n2. https://www.youtube.com/watch?v=YxtQUbKbdUs\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fword-break","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fword-break","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fword-break/lists"}