File tree 1 file changed +36
-0
lines changed
scripts/algorithms/F/Find the String with LCP
1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 197 ms (Top 74.42%) | Memory: 81.00 MB (Top 25.58%)
2
+
3
+ class Solution {
4
+ public:
5
+ string findTheString (vector<vector<int >>& lcp) {
6
+ int n=lcp.size ();
7
+ string res (n,' a' );
8
+ res[0 ]=' a' ;
9
+ char mn=' b' ;
10
+ for (int i=1 ;i<n;i++)
11
+ {
12
+ bool set=false ;
13
+ for (int j=0 ;j<i;j++){
14
+ if (lcp[i][j]!=0 ){
15
+ set=true ;
16
+ res[i]=res[j];
17
+ break ;
18
+ }
19
+ }
20
+ if (!set) res[i]=mn++;
21
+ }
22
+ for (int i=0 ;i<n;i++) if (res[i]>' z' ) return " " ;
23
+ vector<vector<int >> dp (n,vector<int >(n,0 )); // suffix count
24
+
25
+ for (int i=n-1 ;i>=0 ;i--)
26
+ for (int j=n-1 ;j>=0 ;j--)
27
+ if (res[i]==res[j]) dp[i][j]=1 +((i<n-1 && j<n-1 )?dp[i+1 ][j+1 ]:0 );
28
+
29
+ for (int i=0 ;i<n;i++) // now my 2 arrays should be equal
30
+ for (int j=0 ;j<n;j++)
31
+ if (dp[i][j]!=lcp[i][j]) return " " ;
32
+
33
+
34
+ return res;
35
+ }
36
+ };
You can’t perform that action at this time.
0 commit comments