Skip to main content

Word Break Problem - auto separate the words from a given string checking dictionary.


Word Break Problem - auto separate the words from a given string checking dictionary.
Function to segment given String into a space-separated sequence of one or more dictionary words



import java.util.Arrays;
import java.util.List;

public class TestMain {


// Function to segment given String into a space-separated
// sequence of one or more dictionary words

public static void wordBreak(List<String> dict, String str, String out)
{
// if we have reached the end of the String,
// print the output String

if (str.length() == 0)
{
System.out.println(out);
return;
}

for (int i = 1; i <= str.length(); i++)
{
// consider all prefixes of current String
String prefix = str.substring(0, i);

// if the prefix is present in the dictionary, add prefix to the
// output String and recur for remaining String

if (dict.contains(prefix)) {
wordBreak(dict, str.substring(i), out + " " + prefix);
}
}
}

// main function
public static void main(String[] args)
{
// List of Strings to represent dictionary
List<String> dict = Arrays.asList("this", "love", "is", "famous",
"Word", "break", "problem", "java","i");

// input String
String str = "Wordbreakproblem";
String str1 = "ilovejava";

wordBreak(dict, str, "");
wordBreak(dict, str1, "");
}


}



Comments

Popular posts from this blog

Camunda Spring Boot with Shared Engine with War file

This is the only one website (may be first) to show an example to create working war file for camunda shared engine using Spring Boot. We analysed the problem using pure spring boot "starter" dependencies that it contains camunda engine plus tomcat, so even when you remove tomcat using either <provided> or <exclude>, it continues to run on tomcat as its already with in camunda engine. And when you try to create war file and paste in into shared camunda engine outside then that war will no work. This is because that war you created containing camunda engine and a tomcat inside it and when you paste into another outside camunda engine, it gets confused to which camunda to use. Hence I worked on this and brought a mix version of spring and spring boot implementation of camunda. So here you go....and thanks me later :) Here is the pom file -------------------------------------------------------------------------------------------- <?xml version=...

Introduction to Reactive Programming in Java with Project Reactor

Introduction to Reactive Programming in Java with Project Reactor we will go through below: 1. Introduction to Reactive Programming What is reactive programming? Key principles of reactive systems: Responsive : Systems should respond in a timely manner. Resilient : Systems should be fault-tolerant. Elastic : Systems should scale as needed. Message-driven : Systems should use asynchronous messaging. Comparison between imperative programming and reactive programming. 2. Understanding Reactive Streams Publisher , Subscriber , Subscription , and Processor interfaces. The four key signals: onNext() , onComplete() , onError() , and onSubscribe() . Backpressure handling in reactive systems. 3. Introduction to Project Reactor What is Project Reactor? Key classes: Mono and Flux . Mono : Represents 0 or 1 item. Flux : Represents 0 to N items. Non-blocking nature and how it helps in building scalable systems. 4. Building a Reactive Application with Project Reactor Demonstrating how to use Mono ...

Hibernate interview questions

Question : Why JPA Entity or Hibernate Persistence Class Should Not be Final?  Answer : One of the interesting hibernate interview questions is, why you should not make a Hibernate persistent class final? I'll try to answer this question in this short blog post. The use of proxies is the core feature of Hibernate (one of the most popular ORM frameworks for Java Projects) for implementing key performance features e.g. lazy loading and lazy associations fetching. In order to use a proxy in place of a real class, your hibernate persistence class must be either non-final or the implementation of an interface that declares all of the public methods. Difference between save vs persist and saveOrUpdate in Hibernate Save vs. saveOrUpdate vs. persist in Hibernate? What is the difference between save and saveOrUpdate or Difference between saving and persist are common interview question in any Hibernate interview, much like the difference between get and load method in Hibernate. H...