Damon's Blog
  • Damon's Blog
  • 💻Tech
    • Understand C++ Special Member Function Generation
    • Understand C++ Template Type Deduction
    • Using Windows Keymap in Mac and Intellij
    • Setup C++ Dev Env in MacOS using Docker
    • Floating-point Arithmetic
    • Java HFT Toolbox
    • Interesting Bitwise Operation
    • Python Metaclass
    • Memory Order
    • Grind 75+
      • Array
      • String
      • Matrix
      • Binary Search
      • Graph
      • Tree
      • Math
      • Hash Table
      • Recursion
      • Linked List
      • Stack
      • Heap
      • Trie
      • Dynamic Programming
      • Binary Operation
    • C++ for Java Developers
    • Typical Domain Driven Design Application Architecture
    • How to Deploy Applications in Linux Properly
    • Design Patterns with Java Examples
    • Tools for Reliability
    • MVCC in Database
    • Two Microservice Tech Stacks
    • The Difference between 127.0.0.1 and 0.0.0.0
  • âž—Math
    • Local Volatility in Terms of Implied Volatility
    • Mean and Variance of Squared Gaussian
    • A Simple Explanation of Information Gain
  • 💲Trading
    • Understanding Risk-Neutral Density Functions
    • The Deriving of Black-Scholes Equation
    • Quant Questions
  • 💎Gems
    • 2024
  • 📖Books
    • Performance Analysis and Tuning on Modern CPUs
Powered by GitBook
On this page
  • 68. Implement Trie (Prefix Tree)
  • 69. 139. Word Break

Was this helpful?

  1. Tech
  2. Grind 75+

Trie

PreviousHeapNextDynamic Programming

Last updated 10 months ago

Was this helpful?

68.

TODO

69.

class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        //final Boolean[] cache = new Boolean[s.length()];
        //return memorized(0, s, wordDict, cache);
        return dp(s, wordDict);
    }

    private boolean recursive(final int i, final String s, final List<String> wordDict) {
        if (i >= s.length()) {
            return true;
        }

        for (int j = i; j < s.length(); j++) {
            if (wordDict.contains(s.substring(i, j+1)) && recursive(j+1, s, wordDict)) {
                return true;
            }
        }

        return false;
    }

    private boolean memorized(final int i, final String s, final List<String> wordDict, final Boolean[] cache) {
        if (i >= s.length()) {
            return true;
        }

        if (cache[i] != null)
            return cache[i];

        for (int j = i; j < s.length(); j++) {
            if (wordDict.contains(s.substring(i, j+1)) && memorized(j+1, s, wordDict, cache)) {
                cache[i] = true;
                return true;
            }
        }
        
        cache[i] = false;
        return false;
    }

    private boolean dp(final String s, final List<String> wordDict) {
        final boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;

        for (int i = 1; i < s.length()+1; i++) {
            for (int j = 0; j < i; j++) {
                if (dp[j] && wordDict.contains(s.substring(j, i))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[s.length()];
    }
}
💻
Implement Trie (Prefix Tree)
139. Word Break
[DP] Building up DP intuition with 3 solutions: Recursive, Memoization and Tabulation