public boolean wordBreak(String s, Set<String> dict) {
        if (s == null || dict == null) return false;
        boolean dp[] = new boolean[s.length() + 1];
        dp[0] = true;
        for (int i = 0; i < s.length(); ++i) {
            for (int j = 0; j <= i; ++j) {
                if (dp[j] && dict.contains(s.substring(j, i + 1))) {
                    dp[i + 1] = true;
                }
            }
        }
        return dp[s.length()];
    }