Leetcode - WordBreak

用了两个办法:

(1)试着写了个前缀树,352 ms。

其中最大的trick是要用一个数组 boolean[]
searchFlag来记录从s[i]开始的子串(s[i...n-1])是否被搜索过(s[i...n-1]能否由dict的单词组成)。如果被搜索过那就不用再搜索了(因为搜索失败了,如果成功的话程序早已结束)。

import java.util.*;

public class Solution {

	class TrieNode {
		char ch;
		ArrayList<TrieNode> children = new ArrayList<TrieNode>();

		TrieNode(char c) {
			ch = c;
		}

		TrieNode() {
			ch = '\0';
		}
	}

	public void insert(String word, int ind, TrieNode node) {
		if (ind == word.length()) {
			TrieNode nNode = new TrieNode('\0');
			node.children.add(nNode);
			return;
		}

		int i;
		for (i = 0; i < node.children.size(); i++) {
			if (node.children.get(i).ch == word.charAt(ind)) {
				insert(word, ind + 1, node.children.get(i));
				break;
			}
		}

		if (i == node.children.size()) {
			TrieNode nNode = new TrieNode(word.charAt(ind));
			node.children.add(nNode);
			insert(word, ind + 1, nNode);
		}
	}

	public boolean search(String s, int ind, TrieNode root, TrieNode node, boolean[] searchFlag) {
		if (ind == s.length()) {
			for (int i = 0; i < node.children.size(); i++)
				if (node.children.get(i).ch == '\0')
					return true;
			return false;
		}

		if(node == root && searchFlag[ind] == true)//the sub string[i...n-1] has been searched before
			return false;

		int i;
		boolean flag = false;
		for (i = 0; i < node.children.size(); i++) {
			if (node.children.get(i).ch == '\0') {
				// end of word, try to search a new word again
					flag = search(s, ind, root, root, searchFlag);
					searchFlag[ind] =true;

			} else if (node.children.get(i).ch == s.charAt(ind)) {
				// go on iterate the word
				flag = search(s, ind + 1, root, node.children.get(i), searchFlag);
			}

			if (flag == true)
				return true;
		}

		return false;
	}

	public boolean wordBreak(String s, Set<String> dict) {

		TrieNode root = new TrieNode();

		for (String word : dict) {
			insert(word, 0, root);
		}

		boolean[] searchFlag = new boolean[s.length()];
		for(int i=0;i<s.length();i++)
				searchFlag[i] = false;

		boolean flag = search(s, 0, root, root, searchFlag);

		return flag;
	}

	public static void main(String args[]) {
		Solution sol = new Solution();
		String s = "abcd";
		Set<String> dict = new HashSet<String>(Arrays.asList("a","abc","b","cd"));

		System.out.println(sol.wordBreak(s, dict));
	}
}

(2)不用前缀树,直接在set里面查找,因为数据量少的缘故变得更加快了。336ms

public class Solution {

	public boolean search(String s, int beg, Set<String> dict, boolean[] searchFlag)
	{
		if(beg == s.length())
			return true;

		if(searchFlag[beg] == true)
			return false;

		boolean flag = false;
		for(int e = beg;e<=s.length();e++)
		{
			String subStr = s.substring(beg,e);
			if(dict.contains(subStr))
			{
				flag = search(s,e,dict,searchFlag);
				if(flag == true)
					return true;

				searchFlag[e] = true;
			}
		}

		return false;
	}

	public boolean wordBreak(String s, Set<String> dict) {
		boolean[] searchFlag = new boolean[s.length()+1];

		for(int i=0;i<s.length()-1;i++)
			searchFlag[i] = false;

		boolean flag = search(s,0,dict, searchFlag);

		return flag;
	}

}
时间: 2024-10-07 09:55:12

Leetcode - WordBreak的相关文章

动态规划的思想来求解字符串分割问题

LeetCode WordBreak原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", dict = ["leet", "code"]

Word Break -- leetcode

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", dict = ["leet", "code"]. Return true because

LeetCode: Word Break I &amp;&amp; II

I title: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true

[LeetCode]Word Break 特里

意甲冠军:推断字符串给定的字符串是否构成词典. 来推断目标字符串相匹配整个字典.我们需要来推断目标字符串的每个前缀开始的下一场比赛,这需要匹配目标字符串的成功,所有前缀的枚举. class TrieNode{//from http://www.cnblogs.com/x1957/p/3492926.html public: TrieNode* ch[26];//char指针数组 bool isWord; TrieNode():isWord(false){ memset(ch,0,sizeof(T

[C++]LeetCode: 113 Word Break II (DP &amp;&amp; Backtacking) 求解拆分组合

题目: 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

leetcode 139. Word Break

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

leetcode[140] Word Break II

这题是上一题的加强版,这里需要返回的是所有可能的分割词.例如: s = "catsanddog",dict = ["cat", "cats", "and", "sand", "dog"]. A solution is ["cats and dog", "cat sand dog"]. 先用dp求得每个起点到终点是否符合word break 1中的条

[Leetcode][JAVA] Word Break

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

Leetcode dp Word Break

Word Break Total Accepted: 22281 Total Submissions: 105657My Submissions Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcod