Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // version 1:
- public class Solution {
- private void search(int index, String s, List<Integer> path,
- boolean[][] isWord, boolean[] possible,
- List<String> result) {
- if (!possible[index]) {
- return;
- }
- if (index == s.length()) {
- StringBuilder sb = new StringBuilder();
- int lastIndex = 0;
- for (int i = 0; i < path.size(); i++) {
- sb.append(s.substring(lastIndex, path.get(i)));
- if (i != path.size() - 1) sb.append(" ");
- lastIndex = path.get(i);
- }
- result.add(sb.toString());
- return;
- }
- for (int i = index; i < s.length(); i++) {
- if (!isWord[index][i]) {
- continue;
- }
- path.add(i + 1);
- search(i + 1, s, path, isWord, possible, result);
- path.remove(path.size() - 1);
- }
- }
- public List<String> wordBreak(String s, Set<String> wordDict) {
- ArrayList<String> result = new ArrayList<String>();
- if (s.length() == 0) {
- return result;
- }
- boolean[][] isWord = new boolean[s.length()][s.length()];
- for (int i = 0; i < s.length(); i++) {
- for (int j = i; j < s.length(); j++) {
- String word = s.substring(i, j + 1);
- isWord[i][j] = wordDict.contains(word);
- }
- }
- boolean[] possible = new boolean[s.length() + 1];
- possible[s.length()] = true;
- for (int i = s.length() - 1; i >= 0; i--) {
- for (int j = i; j < s.length(); j++) {
- if (isWord[i][j] && possible[j + 1]) {
- possible[i] = true;
- break;
- }
- }
- }
- List<Integer> path = new ArrayList<Integer>();
- search(0, s, path, isWord, possible, result);
- return result;
- }
- }
- // version 2:
- public class Solution {
- public ArrayList<String> wordBreak(String s, Set<String> dict) {
- // Note: The Solution object is instantiated only once and is reused by each test case.
- Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
- return wordBreakHelper(s,dict,map);
- }
- public ArrayList<String> wordBreakHelper(String s, Set<String> dict, Map<String, ArrayList<String>> memo){
- if(memo.containsKey(s)) return memo.get(s);
- ArrayList<String> result = new ArrayList<String>();
- int n = s.length();
- if(n <= 0) return result;
- for(int len = 1; len <= n; ++len){
- String subfix = s.substring(0,len);
- if(dict.contains(subfix)){
- if(len == n){
- result.add(subfix);
- }else{
- String prefix = s.substring(len);
- ArrayList<String> tmp = wordBreakHelper(prefix, dict, memo);
- for(String item:tmp){
- item = subfix + " " + item;
- result.add(item);
- }
- }
- }
- }
- memo.put(s, result);
- return result;
- }
- }
- //version:高频题班
- public class Solution {
- Map<String, List<String>> done = new HashMap<>();
- Set<String> dict;
- public List<String> wordBreak(String s, Set<String> dict) {
- this.dict = dict;
- done.put("", new ArrayList<>());
- done.get("").add("");
- return dfs(s);
- }
- List<String> dfs(String s) {
- if (done.containsKey(s)) {
- return done.get(s);
- }
- List<String> ans = new ArrayList<>();
- for (int len = 1; len <= s.length(); len++) { //将s 分割成s1 s2 其中s1长度为len
- String s1 = s.substring(0, len);
- String s2 = s.substring(len);
- if (dict.contains(s1)) {
- List<String> s2_res = dfs(s2);
- for (String item : s2_res) {
- if (item == "") {
- ans.add(s1);
- } else {
- ans.add(s1 + " " + item);
- }
- }
- }
- }
- done.put(s, ans);
- return ans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment