Personal Blog
  • 💻Notes for Computer Science
  • Leetcode
    • Array
      • Container with most water
      • 3Sum
      • Next Permutation
      • Valid Sudoku
      • Permutation II
      • Combination Sum
      • Triangle
      • Maximal Square
      • Pairs of Songs with Total Duration Divisible by 60
      • Numbers At Most N Given Digit Set
      • Possible Sum
      • Swap Lex Order
      • Partition Equal Subset Sum
      • Domino and Tromino
      • Numbers At Most N Given Digits
      • Car Pooling
      • Surrounding Regions
      • Min Size Subarray Sum
      • Burst Balloons
      • Jump Game I
      • Jump Game II
      • House Robber II
      • Delete and Earn
      • Word Break
      • Decode Ways
      • Longest Increasing Subsequence
      • Cherry Pickup
      • Rotate Image
    • LinkedList
      • IsListPalindrome
      • Linked List Cycle
      • MergeTwoLinkedList
      • ReverseNodeInKGroup
      • RearrangeLastN
      • Remove Duplicates From Sorted List
      • RemoveKFromList
    • String
      • Generate Parentheses
      • Longest Valid Parentheses
      • Longest Common Subsequence
      • Count and Say
      • Decode String
      • Permutation in String
    • Tree
      • House Robber III
      • Convert Sorted Array to Binary Search Tree
      • Restore Binary Tree
      • Populating Next Right Pointers in Each Node II
      • Subtree of Another Tree
    • Graph
      • All Paths from Source to Target
      • Reorder Routes to Make All Paths Lead to the City Zero
      • Max Points on a Line
  • DBMS
    • DBMS Notes
  • Web App
    • Web Design
    • JavaScript
    • React.js
    • ReactNative
    • Mobile Design
    • Dialogue Flow
  • AnaplanIntern
    • Splunk
    • Docker
    • Kubernetes
  • 💰 Notes for Finance Concept
  • Analysis Concept
    • Volume Spread Analysis
    • Smart Money Concepts
Powered by GitBook
On this page
  • Idea
  • Code
  1. Leetcode
  2. Array

Word Break

ID:139

Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words.

Note that the same word in the dictionary may be reused multiple times in the segmentation.

Input: s = "leetcode", wordDict = ["leet","code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".

Idea

Recursion + Memorization

Always start from the beginning of the string, check if there's a fit in wordDict for the starting substring.

If so, check for the later part of the string to check the starting substring in wordDict......

If the resulting substring after truncating the part that is equals to the dict word has a length=0, meaning all characters in the string found its corresponding dict word, then return true, since the entire string can be partitioned based on wordDict

Also, use a List or HashMap to keep track of already seen pattern. In corner cases like s = "aaaaaaaaaaaaab" and wordDict = ["a","aa","aaa","aaaaa"], we can save a lot of time when a previous pattern is already seen by returning false

Code

HashMap<String, Boolean> map = new HashMap<>();
// List<String> seen = new ArrayList<>();
public boolean wordBreak(String s, List<String> wordDict) {
    if(s.length()==0){
        return true;
    }
    if(map.containsKey(s)){
        return false;
    }
    for(String dict: wordDict){
        if(dict.length()<=s.length() && s.substring(0, dict.length()).equals(dict)){
            if(wordBreak(s.substring(dict.length()), wordDict)){
                return true;
            }
        }
    }
        
    map.put(s, false);
    return false;
}

PreviousDelete and EarnNextDecode Ways

Last updated 3 years ago