LeetCode:Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].

A solution is ["cats and dog", "cat sand dog"].

第一步:先用动态规划找出所有的断点,题目中的例子来说,在位置0,可以选择cats或者cat,下一断点为3和4。我的代码中buildPos()方法实现。

第二步:使用递归列出所有解。我的代码中的printResult()方法实现。

PS:我的方法命名太烂 了,看不下去了 。

package leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class WordBreakII {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String s = "catsanddog";
		Set<String> dict = new HashSet<String>();
		dict.add("cat");
		dict.add("cats");
		dict.add("and");
		dict.add("sand");
		dict.add("dog");
		
		ArrayList<String> result  = new WordBreakII().wordBreak(s, dict);
		for (String ss : result) {
			System.out.println(ss);
		}
	}
	
	
	private Map<Integer, List<Integer>> posMap = new HashMap<Integer, List<Integer>>();
	private ArrayList<String> result = new ArrayList<String>();
	public ArrayList<String> wordBreak(String s, Set<String> dict) {
       for (List<Integer> list : posMap.values()) {
    	   list.clear();
       }
       result.clear();
       
       if (dict == null || dict.isEmpty()) {
    	   return result;
       }
       
       buildPos(s, dict);
       
       StringBuffer sb = new StringBuffer();
       printResult(s, 0, sb);
       
       return result;
    }
	
	void printResult(String s, int pos, StringBuffer sb) {
		if (pos == s.length()) {
			result.add(sb.toString());
			return;
		}

		List<Integer> list = posMap.get(pos);
		for (Integer newPos : list) {
			if (pos != 0) {
				sb.append(' ');
			}
			sb.append(s.substring(pos, newPos));

			printResult(s, newPos, sb);

			int appendLen = newPos - pos;
			if (pos != 0) {
				appendLen += 1;
			}
			sb.delete(sb.length() - appendLen, sb.length());
		}
	}
	
	public void buildPos(String s, Set<String> dict) {
		List<Integer> list = new ArrayList<Integer>();
		for (int i = s.length() - 1; i >= 0; i--) {
			int len = list.size();
			for (int j = len - 1; j >= 0; j--) {
				int index = list.get(j);
				String sub = s.substring(i, index);
				if (dict.contains(sub)) {
					List<Integer> list2 = posMap.get(i);
					if (list2 == null) {
						list2 = new ArrayList<Integer>();
						posMap.put(i, list2);
					}
					list2.add(index);
				}
			}
			
			String sub = s.substring(i);
			if (dict.contains(sub)) {
				List<Integer> list2 = posMap.get(i);
				if (list2 == null) {
					list2 = new ArrayList<Integer>();
					posMap.put(i, list2);
				}
				list2.add(s.length());
			}
			
			List<Integer> list2 = posMap.get(i);
			if (list2 != null && !list2.isEmpty()) {
				list.add(i);
			}

		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值