forked from sambit77/Algoexpert-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterWeavingStringsOptimalRecursive.java
More file actions
90 lines (71 loc) · 2.67 KB
/
InterWeavingStringsOptimalRecursive.java
File metadata and controls
90 lines (71 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//Time Complexity O(nm) | Space Complexity O(nm)
import java.util.*;
class A
{
public static void main(String[] args)
{
String one = "aaa";
String two = "aaaf";
String three = "aaafaaa";
//check id String one & two can be interwoven to form third String
boolean result = checkForInterWeaving(one,two,three);
System.out.println("is thirs String is inter woven string of one & two ?"+result);
}
public static boolean checkForInterWeaving(String one ,String two,String three)
{
//if length of third String i sgreater than the sum of length of first two Strings
//they can never be interwoven to fm third String
if(three.length()>one.length()+two.length())
{
return false;
}
//to be used in memoization technique
//we have not used boolean[][] beacuse it can only store true false
//Boolean[][] can store null,true,false
//bydefault boolean[][] is initialized to false,but Boolean[][] initialized to null
//so we nedd a cache in whic i,j grids initialized to null
Boolean[][] cache = new Boolean[one.length()+1][two.length()+1];
return isInterWoven(one,two,three,0,0,cache);
}
public static boolean isInterWoven(String one ,String two , String three,int i ,int j,Boolean[][] cache)
{
//i pointer is used to traverse String 1
//j pointer is used to traverse String 2
//k is used to tarverse String 3
int k = i+j;
//if the value is already computed for this i,j then just return the value dont go for unnecessary recursion
if(cache[i][j] != null)
{
return cache[i][j];
}
//System.out.print(".");
//if with recursion we able to reach end of last String then yest ,it can be interwiven
if(k==three.length())
{
return true;
}
//if i is within the limit & char at i in String one is equal to char at k in String 3
if(i < one.length() && one.charAt(i)== three.charAt(k))
{
//then recursively exploe by incrementing i and return true only if the recursion returns true
//as because we have to also consider the case of starting with second String
//compute and also store the value in cache
cache[i][j] = isInterWoven(one,two,three,i+1,j,cache);
if(cache[i][j])
{
return true;
}
}
//if j is within the limit & char at j in String two is equal two char at k in String 3
if(j < two.length() && two.charAt(j) == three.charAt(k))
{
//then recursively explore for all the values of String two by incrementing j
//compute and store the value im cache
cache[i][j] = isInterWoven(one,two,three,i,j+1,cache);
return cache[i][j];
}
//compute and store the value im cache
cache[i][j] = false;
return false;
}
}