public boolean wordBreak(String s, Set<String> dict) {
        boolean[] t = new boolean[s.length() + 1];
        //set first to be true, for initial state, to start the loop
        t[0] = true;

        //from 0 to n-1
        for (int i = 0; i < s.length(); i++) {
            //should continue to next string
            if (!t[i]) {
                continue;
            }

            //compare all the pieces in the dict
            for (String a : dict) {
                int len = a.length();
                int end = i + len;
                //if the string in the dict is too long, not match, check next
                if (end > s.length()) {
                    continue;
                }
                //compare the substring
                if (s.substring(i, end).equals(a)) {
                    t[end] = true;
                }
            }
        }
        return t[s.length()];
    }