Java Word Break(单词拆解)

给定一个字符串 String s =
"leetcode"

dict =
["leet", "code"]
.

查看一下是够是字典中的词语组成。假设是返回true,否则返回false。

下边提供3种思路

1.动态算法

import java.util.HashSet;
import java.util.Set;

public class WordBreak1 {
	public static void main(String[] args) {
		//"["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]
		//String s="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
		String s ="LeetCodea";
		Set<String> dict = new HashSet<String>();
		dict.add("Leet");
		dict.add("Code");
		dict.add("a");
		System.out.println(wordBreak(s,dict));
	}
	public static boolean wordBreak(String s, Set<String> dict) {
		boolean[] t = new boolean[s.length() + 1];
		t[0] = true; // set first to be true, why?
		// Because we need initial state

		for (int i = 0; i < s.length(); i++) {
			// should continue from match position
			if (!t[i])
				continue;
			for (String a : dict) {
				int len = a.length();
				int end = i + len;
				if (end > s.length())
					continue;

				if (t[end])
					continue;

				if (s.substring(i, end).equals(a)) {
					t[end] = true;
				}
			}
		}
		return t[s.length()];
	}
}

2.普通算法(1)

import java.util.Set;

public class WorkBreak2 {
	public boolean wordBreak(String s, Set<String> dict) {
		return wordBreakHelper(s, dict, 0);
	}

	public boolean wordBreakHelper(String s, Set<String> dict, int start) {
		if (start == s.length())
			return true;

		for (String a : dict) {
			int len = a.length();
			int end = start + len;

			// end index should be <= string length
			if (end > s.length())
				continue;

			if (s.substring(start, start + len).equals(a))
				if (wordBreakHelper(s, dict, start + len))
					return true;
		}
		return false;
	}
}

3.普通算法(2)

import java.util.Set;

public class WordBreak3 {

	public static boolean wordBreak(String s, Set<String> dict) {
		// input validation
		// Base case
		if (dict.contains(s))
			return true;
		else {
			for (int i = 0; i < s.length(); i++) {
				String sstr = s.substring(0, i);
				if (dict.contains(sstr))
					return wordBreak(s.substring(i), dict);
			}
		}
		return false;
	}
}

可是以上的算法有一个问题,就是遇到这样的情况。INPUT: "programcreek", ["programcree","program","creek"]. 无能为力。

大家讨论下吧?

时间: 2024-11-03 20:56:27

Java Word Break(单词拆解)的相关文章

[Leetcode][JAVA] 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, givens = "catsanddog",dict = ["cat", "cats"

[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 &

Word Break II leetcode java

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

Word Break II 求把字符串拆分为字典里的单词的所有方案 @LeetCode

这道题类似  Word Break 判断是否能把字符串拆分为字典里的单词 @LeetCode 只不过要求计算的并不仅仅是是否能拆分,而是要求出所有的拆分方案.因此用递归. 但是直接递归做会超时,原因是LeetCode里有几个很长但是无法拆分的情况,所以就先跑一遍Word Break,先判断能否拆分,然后再进行拆分. 递归思路就是,逐一尝试字典里的每一个单词,看看哪一个单词和S的开头部分匹配,如果匹配则递归处理S的除了开头部分,直到S为空,说明可以匹配. public class Solution

Word Break leetcode java

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

Word Break II 求把字符串拆分为字典里的单词的全部方案 @LeetCode

这道题相似  Word Break 推断能否把字符串拆分为字典里的单词 @LeetCode 只不过要求计算的并不不过能否拆分,而是要求出全部的拆分方案. 因此用递归. 可是直接递归做会超时,原因是LeetCode里有几个非常长可是无法拆分的情况.所以就先跑一遍Word Break,先推断能否拆分.然后再进行拆分. 递归思路就是,逐一尝试字典里的每个单词,看看哪一个单词和S的开头部分匹配,假设匹配则递归处理S的除了开头部分,直到S为空.说明能够匹配. Given a string s and a

Leetcode:Word Break 字符串分解为单词

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, given s = "leetcode", dict = ["leet", "code"]. Return t

LeetCode: Word Break II 解题报告

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",

[LeetCode] 527. Word Abbreviation 单词缩写

Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below. Begin with the first character and then the number of characters abbreviated, which followed by the last charact