-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1048. Longest String Chain.java
42 lines (36 loc) · 1.11 KB
/
1048. Longest String Chain.java
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
class Solution {
public int longestStrChain(String[] words) {
//dp solution
//time complexity : O(n logn)+O(n^2)*O(max. length of string)
//space com :O(n)
Arrays.sort(words,(a,b)->a.length()-b.length());
int n=words.length;
int dp[]=new int[n];//length
Arrays.fill(dp,1);
int maxLen=1;
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
if(checkPossible(words[i],words[j]) && dp[i]<1+dp[j]){
dp[i]=1+dp[j];
}
}
if(dp[i]>maxLen)
maxLen=dp[i];
}
return maxLen;
}
private boolean checkPossible(String s, String t) {//cur string , predecessor str.
if(s.length()!=1+t.length()) return false;
int start=0;
int sec=0;
while(start<s.length()){
if(sec<t.length() && s.charAt(start)==t.charAt(sec)){
start++;
sec++;
}else
start++;
}
if(start==s.length() && sec==t.length()) return true;
return false;
}
}