Learn Java and Programming through articles, code examples, and tutorials for developers of all levels.
Why Java Developers Should Learn Docker in 2023?
5 Projects You can do to learn TensorFlow in 2023 - Best of Lot
8 Best Programming Languages You Can Learn in 2023 for Better Pay and Jobs
Top 20 Nested Static and Inner Class Java Interview Questions and Answers
Hello guys, static class and nested classes are one of the tricky topic to master in Java and that's why they are quite popular on Java Interviews. Not many Java developer knows about Anonymous class or how Lambda is compiled into Anonymous class by JVM etc. If you are going for Java Interview, I highly recommend you to prepare nested class topic well in Java. Just like in the past, I have shared many Java in-depth questions on different topics like Lambda Expression, Stream API, Generics, Collections, Multithreading, String, array, Design Patterns, OOP, Spring Framework, Hibernate etc,
Top 10 Open Source Frameworks & Libraries for Java Web Developers for 2023 [UPDATED]
5 Projects to learn Android App Development in 2023
10 Things Web Developers Should Learn in 2023 [UPDATED]
5 Things You Can Gift to Programmers, Software Developers, and Tech Geeks in 2023
10 Projects To Learn Web Development in 2023
10 Projects You can Build to Learn Golang in 2023
Top 5 Functional Interface Every Java Developer Should Learn
Hello guys, functional interface in Java are an important concept but not many developer pay enough attention to them. They learn lambda and method reference but the functional interface from java.util.function package. While its not really possible to learn all the functional interfaces on that package but you can learn a few more commonly used ones like Predicate, Supplier, Consumer etc and that's what you will learn in this article. But, before we get to the top 5 functional interfaces that every Java developer should learn, let me tell you a bit about what Java really is.
How to access a value defined in the application.properties file in Spring Boot? Example Tutorial
How to access a value defined in the application.properties file in Spring Boot is a common question that arises when you dealing with large software applications. In software applications, you need to have different environments for the QA, production, and local. So as a solution for this, you can use different configurations and update the files separately without affecting other environments by using property files.
Visitor Design Patterns In Java Examples Tutorial
Hello guys, if you want to learn Visitor design pattern in Java then you have come to the right place. Earlier, I have covered many design patterns like Decorator, Strategy, State, Composite, Adapter, Command, Template, Factory, Observer and even few Microservice patterns like SAGA and Database per service and in this article, I will talk about Visitor Design Pattern and how to implement in Java. You will learn things like what is Visitor design pattern, what problem it solves, what are pros and cons of Visitor design pattern, when to use Visitor pattern as well as any alternatives of Visitor Pattern in Java. I will also show you a real world example of Visitor design pattern, but, before we get to the 5 best examples that will teach you all about design patterns in Java, let me tell you a little bit more about what it really is.
SAGA Microservice Design Pattern In Java
Hello guys, if you want to learn about SAGA Microservice design pattern then you have come to the right place. SAGA is one of the 10 essential Microservice Design Patterns I have shared earlier and it solve a critical problem related to distributed transaction. But, before we get to the different examples that will teach you all about SAGA microservice design patterns in Java, let me tell you a bit more about what it really is. A microservice-based application is basically a distributed system. The overall system consists of multiple minor services, which provide the overall application functionality. This architectural style offers numerous benefits as well as several limitations. SAGA Pattern is also one of the popular Microservice interview question and if you are going for interview, it make sense to learn and understand this design pattern in depth.
Top 5 Features Of Spring Boot 3 To Learn In 2023
Top 20 Apache Spark Interview Questions and Answers
10 Examples Of Spring 5 WebClient In Java
Hello guys, if you are wondering how to use WebClient on Spring Framework and looking for simple how to examples of WebClient then you have come to the right place. Earlier, I have shared 10 example of RestTemplate in Spring Framework and in this article, I Am going to share 10 example of WebClient in Spring. These examples covers everything from sending GET request to REST API and consuming response, and also sending POST, PUT, and PATCH request to RESTful Web Services. You will also learn how to set header like Basic Authentication and Authorization header, cookies and much more.
Top 5 Solidity courses for Beginners to Learn in 2023 - Best of Lot
Hello friends, we are here again today for another exciting topic to discuss. But, today, we are not gonna discuss something related to Java or any other language or spring boot. Today we are gonna discuss something which is immensely practical and has the potential to land you very high paying jobs. Today, we are going to take a look at the best available Solidity courses online.
Top 20 Oracle Database Interview Questions with Answers for 3 to 5 Years Experienced
Top 5 Features Of Spring 6 To Learn In 2023
Top 21 Python Interview Questions Answers for 1 to 2 Years Experienced Programmers
Difference between 32-bit vs 64-bit JVM in Java?
How to remove duplicate(s) from linked list in Java? Example Tutorial
10 Examples Of Mockito + JUnit in Java for Unit Testing
How to convert Java 8 Stream to Array and ArrayList in Java? Example Tutorial
10 Examples Of RestTemplate in Spring Framework, Spring Boot and Java
Array length vs ArrayList Size in Java [Example]
How to create an ArrayList from Array in Java? Arrays.asList() Example Tutorial
One of the common problems faced by junior and less experienced Java developers is converting an array to ArrayList e.g. they are getting an array from somewhere in their code and then want to create an ArrayList out of that so that they can add more elements and use other library methods which operate with ArrayList or List. The simplest way to convert an array to ArrayList is by using the Arrays.asList() method, which acts as a bridge between Collection classes and array data structure. This method returns a List that contains elements from an array.
Difference between Class and Object in Java? Example
How to check If two Strings Array are equal in Java? Example Tutorial
How to find Factorial in Java using Recursion and Iteration - Example Tutorial
Is it Possible to add static or private methods in Java interface?
10 Examples Of Scanner Class In Java
What is Diamond operator in Java? Example Tutorial
Hello guys, if you are reading Java code and come across closed angle bracket with a diamond like shape and wondering what they are and what they do then you have come to the right place. They are known as Diamond operator in Java and I will explain you what they are and where should you use them in this article. The Diamond operator is a relatively new operator in Java which was first introduced in JDK 7 to improve type inference and reduce boilerplate Java coding. It is denoted with a closed angle bracket that resembles the shape of a diamond (<>) and that's why it's called the Diamond operator. If used correctly it can reduce typing and boilerplate coding in Java and result in much cleaner and readable code especially when you use Generics.
Parallel Array Sorting in Java - Arrays.parallelSort() Example
Difference between Thread vs Process in Java? Example
Difference between yield and wait method in Java? Answer
10 points about wait(), notify() and notifyAll() in Java Thread?
How to run Threads in an Order in Java - Thread.Join() Example
10 Examples Of Lombok Libarary In Java
How to set base URL for REST in Spring Boot? Example Tutorial
10 Examples Of Ternary Operator In Java
Hello guys, if you are wondering how to use ternary operator in Java then you have come to the right place. Ternary operator is a great operator and you can use ternary operator to replace a simple if-else statement. Since ternary operator allows you to write code in one line, its much more succinct and readable and that's what many Java developer love it, including me. In the last article, I shared 10 example of XOR bitwise operator in Java and in this article, I am going to share 10 example of ternary operator in Java so that you not only know what is ternary operator but also you can use it effectively to write better Java code. But, before we get to the 10 best examples that will teach you everything there is to know about ternary operators in Java, let me tell you a little bit more about what it all really is.
How to do Integration Testing in Spring Framework? @SpringBootTest Example Java
Hello guys, if you are wondering how to test your Spring Boot application then you have come to the right place. Earlier, I have shared popular Spring Boot Testing Interview Questions and Spring Security Interview Questions and In this article, I will show you examples to test your Spring Boot application using @SpringBootTest annotation, which allows you to write Integration tests. When I talk about spring boot integration testing, I am talking about launching an application in ApplicationContext and performing tests. For integration testing, Spring Framework has a specific test module. It's referred to as a spring test. If we're going to utilize spring-boot, we'll need to use spring-boot-starter-test, which uses spring-test and other dependencies inside.
5 Essential Spring Boot Annotations with Examples in Java - Tutorial
How @Configuration, @Component, and @ComponentScan work in Spring Framework? Component Scanning Example
10 Examples of XOR Operator In Java
Top 35 Java String Interview Questions with Answers for 2 to 5 Years Experienced Programmers
How to calculate perimeter and area of square in Java? Example Tutorial
How Long It Take To Become a DBA (Database Administrator) in 2023? 7 days, 1 month or 1 year?
Hello guys, if you want to become a Database Administrator in 2023 and wondering how long does it take to learn SQL and Database and become a professional Database Administrator or Junior DBA then you are at right place. In the past, I have shared free SQL and Database courses as well roadmap to become a Web Developer and in this article, I am going to share how long it will take you to become a DBA in 2023. Along the way, I will share best way to learn SQL and Database and what is the best way to become a DBA quickly. So continue reading and find out what it takes to become a DBA and get a job in real world. I will also share all the skills you need to become a DBA in 2023.
Top 10 Courses to Learn System Design and Software Architecture in 2023 - Best of Lot
How to convert float, double, String, Map, List to Set, Integer, Date in Java - Example Tutorial
How to solve word break problem in Java using dynamic programming? Example
You are given a dictionary of words and the input string. Determine input string can be segmented into a space-separated sequence of given dictionary words.
Note: This question is based on dynamic programming and asked multiple times in top product-based companies.
Inputs:
Dict = {i, like, am, boy, e, o, dog, cat, g};
word = "iIikedog" --------can be segmented into space-separated words--------> i, like, dog
Ask yourself that 'I', 'like', and 'dog' are presented in the dictionary?
Yes, so we can say that the given string can be segmented.
Now let's take another example.
word = "ilikecatsanddog" --------- can be segmented as-----> i, like, cats, and, dog
i, like, cat, s, and, dog
We can see that the words 'cats', 'and', 's' are not present in the dictionary so such a string can't be segmented into space-separated words.
Let's try to solve it.....
Like another dynamic programming problem, we will create a matrix and will use previously calculated results to calculate the current result.Consider the given string as an array like this.
i | l | i | k | e | d | o | g |
The columns and rows represent the same given string in the matrix.
Now let's try to understand what each cell represents in the matrix.
The cells can have two kinds of values either '0' or '1'. Suppose the cells (3,6) and (3,7) have the value '1' this means the substrings from 3 to 6 and 3 to 7 are present in the given input dictionary.
If you notice the cell (5,3) and the corresponding substring in the given word the direction is reverse and there is no point in considering the reverse computation of the given string. So we will mark all such cells as '0'.
We filled half of the matrix with value '0' so we no need to perform the reverse computations for these cells. This way we can save both memory and time.
Now start filling rest of the cells manually by comparing the row-column pair value against the given dictionary.
So the cell (0, 0) i.e. 'i' is present in the dictionary so matrix[0][0] = 1.
Similarly cell (4, 4) i.e. 'e' is also present in the dictionary so matrix[4][4] = 1.
Now let's take one substring 'ilike' and see the logic to solve this problem.
i | l | i | k | e | d | o | g |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
i l i k e
0 1 2 3 4
We will try to separate this string into 2 parts in all the possible ways.
Let say we have first 2 parts:
(0, 0)--------> 'i' and (1, 1)----------> 'l'
matrix[0][0] && matrix[1][1]
1 && 0
So 'il' is not present in the dictionary.
Consider another combination.
(4, 3)--------> 'k' and (4, 4)----------> 'e'
matrix[4][3] && matrix[4][4]
0 && 1
This is false means 'ke' is not present in the dictionary.
So I will keep on taking the substrings from the given word to be space separated and divide them into 2 parts in different ways and will check against the matrix that if both the parts are true i.e. '1' means the substring is present in the dictionary.
So finally we will consider the entire given word and divide it into 2 parts in all the possible ways and we will use the values of the previously computed cells to calculate the value of the marked cell and if the the value is '1' means the given word can be separated into the space separated segments.
Start Implementing it...
Consider below code snippet.
i | l | i | k | e | d | o | g |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
For i = 0 and N = 7
for(int k = 0; k < N+1; k++){
if(matrix[i][i+k]) && matrix[k+1][N])
{
return true;
}
}
The if condition shows the logic to divide the given word into 2 parts in all possible ways.
Complete Code:
public class Main {
public String wordBreakProblem(String word, Set<String> dict){
int matrix[][] = new int[word.length()][word.length()];
// fill all the cells with '-1'.
for(int i=0; i < matrix.length; i++){
for(int j=0; j < matrix[i].length ; j++){
matrix[i][j] = -1;
}
}
//If the substring is present in the dictionary then fill the corresponding cell with non-negative value.
for(int l = 1; l <= word.length(); l++){
for(int i=0; i < word.length() -l + 1 ; i++){
int j = i + l-1;
String str = word.substring(i,j+1);
if(dict.contains(str)){
matrix[i][j] = i;
continue;
}
// Filling the value of the corresponding cell for the taken substring using value of the previously calculated cell.
for(int k=i+1; k <= j; k++){
if(matrix[i][k-1] != -1 && matrix[k][j] != -1){
matrix[i][j] = k;
break;
}
}
}
}
if(matrix[0][word.length()-1] == -1){
return null;
}
//Finally segregate the given word into the words available in the dictionary.
StringBuffer buffer = new StringBuffer();
int i = 0; int j = word.length() -1;
while(i < j){
int k = matrix[i][j];
if(i == k){
buffer.append(word.substring(i, j+1));
break;
}
buffer.append(word.substring(i,k) + " ");
i = k;
}
return buffer.toString();
}
public static void main(String args[]){
Set<String> dictionary = new HashSet<String>();
dictionary.add("I");
dictionary.add("like");
dictionary.add("had");
dictionary.add("play");
dictionary.add("to");
String str = "Ihadliketoplay";
Main bmw = new Main();
String result1 = bmw.wordBreakProblem(str, dictionary);
System.out.print(result1);
}
}
Output:
I had like to play
Test your understanding...
Q. 1) Given dict = {'li, 'sop', 'tree', 'ding', 'g'} and word = 'sopptreeg'. Is it possible to segment the given string into space separated segments of the given dict. words ?
Ans. 'sop' , 'p', 'tree', 'g'
'so', 'pp', 'tree', 'g'
It looks like the given word can't be separated.
Before you leave...
Knowledge of data structure and algorithms is must to simulate the real world problem in code.If you want to learn more about this article, drop a comment below and reach out to us to let us know your interest.
If you enjoyed learning the fundamentals of DSA share your knowledge to your fellow programmers and social circle. May be someone out really needs this resource, and you might be helping them out by sharing it.
eeeeellldldkonoioid