Skip to content

Commit 0b79d22

Browse files
committed
Word breaking problem added
1 parent 67c6c0d commit 0b79d22

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.Arrays;
2+
import java.util.List;
3+
4+
public class WordBreaking {
5+
public static void main(String[] args) {
6+
String str = "iloveicecreamandmango";
7+
8+
List<String> dict =
9+
Arrays.asList(
10+
"mobile",
11+
"samsung",
12+
"sam",
13+
"sung",
14+
"man",
15+
"mango",
16+
"icecream",
17+
"and",
18+
"go",
19+
"i",
20+
"love",
21+
"ice",
22+
"cream");
23+
System.out.println("First Test:");
24+
25+
wordBreakingUtil(str.length(), dict, str);
26+
}
27+
28+
private static void wordBreakingUtil(int length, List<String> dict, String str) {
29+
String ans = "";
30+
FindAndAddBreaks(length, dict, str, ans);
31+
}
32+
33+
private static void FindAndAddBreaks(int length, List<String> dict, String str, String ans) {
34+
for (int i = 1; i <= length; i++) {
35+
String word = str.substring(0, i);
36+
if (dict.contains(word)) {
37+
if (i == length) {
38+
System.out.println(ans + word);
39+
break;
40+
}
41+
FindAndAddBreaks(length - i, dict, str.substring(i, length), ans + word + " ");
42+
}
43+
}
44+
}
45+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class GetAllPathsRecursionApproach {
5+
public static void main(String[] args) {
6+
int[][] mat = {{1, 2, 3}, {0, 10, 6}};
7+
List<Integer> path = new ArrayList<>();
8+
if (mat[0][0] == 0 || mat[mat.length - 1][mat[0].length - 1] == 0) {
9+
System.out.println("No Path Available");
10+
return;
11+
}
12+
findAllValidPaths(mat, 0, 0, path);
13+
}
14+
15+
private static void findAllValidPaths(int[][] mat, int x, int y, List<Integer> path) {
16+
if (x > mat.length - 1 || y > mat[0].length - 1 || mat[x][y] == 0) return;
17+
path.add(mat[x][y]);
18+
if (x == mat.length - 1 && y == mat[0].length - 1) System.out.println(path);
19+
findAllValidPaths(mat, x + 1, y, path);
20+
findAllValidPaths(mat, x, y + 1, path);
21+
path.remove(path.size() - 1);
22+
}
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class pathFromCornerToMiddleInMaze {
2+
3+
public static void main(String[] args) {
4+
5+
int[][] maze = {
6+
{3, 5, 4, 4, 7, 3, 4, 6, 3},
7+
{6, 7, 5, 6, 6, 2, 6, 6, 2},
8+
{3, 3, 4, 3, 2, 5, 4, 7, 2},
9+
{6, 5, 5, 1, 2, 3, 6, 5, 6},
10+
{3, 3, 4, 3, 0, 1, 4, 3, 4},
11+
{3, 5, 4, 3, 2, 2, 3, 3, 5},
12+
{3, 5, 4, 3, 2, 6, 4, 4, 3},
13+
{3, 5, 1, 3, 7, 5, 3, 6, 4},
14+
{6, 2, 4, 3, 4, 5, 4, 5, 1}
15+
};
16+
17+
printPathForGivenMaze(maze, 0, 0, "");
18+
}
19+
20+
private static void printPathForGivenMaze(int[][] maze, int x, int y, String s) {
21+
if (x == maze.length / 2 && y == maze.length / 2) {
22+
s += "(" + x + ", " + y + ") -> Mid";
23+
System.out.println(s);
24+
return;
25+
}
26+
if (maze[x][y] == 0) return;
27+
int k = maze[x][y];
28+
maze[x][y] = 0;
29+
if (y + k < maze.length) printPathForGivenMaze(maze, x, y + k, s + "(" + x + ", " + y + ") ->");
30+
if (x + k < maze.length) printPathForGivenMaze(maze, x + k, y, s + "(" + x + ", " + y + ") ->");
31+
if (y - k > 0) printPathForGivenMaze(maze, x, y - k, s + "(" + x + ", " + y + ") ->");
32+
if (x - k > 0) printPathForGivenMaze(maze, x - k, y, s + "(" + x + ", " + y + ") ->");
33+
maze[x][y] = k;
34+
}
35+
}

0 commit comments

Comments
 (0)