Java Word break (word splitting)

Source: Internet
Author: User

Given a string s ="leetcode"
dict = ["leet", "code"] .

It's enough to look at the words in the dictionary. It is assumed to return true, otherwise false is returned.

3 ideas are available below

1. Dynamic algorithms

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> ();d ict.add ("leet");d ict.add ("Code");d Ict.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 Bes true, why?//Because we need initial statefor (int i = 0; i < s.length (); i++) {//should continue F Rom Match positionif (!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. General algorithm (1)

Import Java.util.set;public class WorkBreak2 {public Boolean wordbreak (String s, set<string> dict) {return Wordbrea Khelper (S, dict, 0);} public Boolean wordbreakhelper (String s, set<string> dict, int start) {if (start = = S.length ()) return True;for (stri ng a:dict) {int len = a.length (); int end = start + len;//End index should be <= string Lengthif (End > S.length () ) Continue;if (s.substring (start, start + len). Equals (a)) if (Wordbreakhelper (S, dict, start + len)) return true; return false;}}

3. General algorithm (2)

Import Java.util.set;public class WordBreak3 {public static Boolean wordbreak (String s, set<string> dict) {//input validation//Base Caseif (Dict.contains (s)) return True;else {for (int i = 0; i < s.length (); i++) {String sstr = s.subs Tring (0, I), if (Dict.contains (SSTR)) return Wordbreak (s.substring (i), dict);}} return false;}}


However, the above algorithm has a problem, is to encounter such a situation. INPUT: "Programcreek", ["Programcree", "program", "Creek"]. Powerless.

Let's discuss it, will you?

Java Word break (word splitting)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.