-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsingLIS CONCEPT.java
More file actions
40 lines (36 loc) · 1.09 KB
/
UsingLIS CONCEPT.java
File metadata and controls
40 lines (36 loc) · 1.09 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
class Solution {
public int longestStrChain(String[] words) {
int n = words.length;
Arrays.sort(words, (a, b) -> a.length() - b.length());
int[] dp = new int[words.length];
Arrays.fill(dp, 1);
int maxi = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (KyaYeBanSakteHai(words[i], words[j]) && dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
}
}
maxi = Math.max(maxi, dp[i]);
}
return maxi;
}
public static boolean KyaYeBanSakteHai(String word1, String word2) {
if (word1.length() - word2.length() != 1) {
return false;
}
int first = 0;
int second = 0;
int count = 0;
while (first < word1.length() && second < word2.length()) {
if (word1.charAt(first) == word2.charAt(second)) {
first++;
second++;
} else {
first++;
count++;
}
}
return count <= 1;
}
}