-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequencePatternMatching.java
More file actions
47 lines (43 loc) · 1.39 KB
/
Copy pathSequencePatternMatching.java
File metadata and controls
47 lines (43 loc) · 1.39 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
package com.leetcode.year_2020.DP.longest_common_subsequence;
/**
* https://leetcode.com/problems/is-subsequence/
* @author neeraj on 09/05/20
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class SequencePatternMatching {
public static void main(String[] args) {
System.out.println(isSequencePatternMatching("AXY", "ADXCPY"));
}
public static boolean isSequencePatternMatching(String X, String Y) {
/**
* X = A X Y
* Y = A D X C Y;
*
* What we have to tell is whether X is present in Y.
* this is a simple problem of LCS(X, Y).... if Length of LCS is == X
* we are good, X is definitely present in Y, reason being self explanatory
*/
// Ensuring Always keep X smaller
if (X.length() > Y.length()) {
return isSequencePatternMatching(Y, X);
}
int lengthOfLCS = LengthOfLongestCommonSubsequence.findLengthOfLCS(X, Y);
return X.length() == lengthOfLCS;
}
/**
* Isme simple O(N) bhi kaafi hai
*/
public boolean isSubsequence(String s, String t) {
int t1 = 0, t2 = 0;
while(t1 < s.length() && t2 < t.length()) {
if (s.charAt(t1) == t.charAt(t2)) {
t1++;
t2++;
} else {
t2++;
}
}
return t1 >= s.length();
}
}