File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ bool hammingCheck(string s1,string s2){
4+ if(s1.size()!=s2.size())return false;
5+ int n=s1.size();
6+ int diff=0;
7+ for(int i=0;i<n;i++){
8+ diff+=s1[i]!=s2[i];
9+ if(diff>1)return false;
10+ }
11+ return true;
12+ }
13+ vector<string> getWordsInLongestSubsequence(vector<string>& words, vector<int>& groups) {
14+ int n=words.size();
15+ vector<int> dp(n,1);
16+ vector<int> prev(n);
17+ for(int i=0;i<n;i++){
18+ prev[i]=i;
19+ }
20+ int maxIdx=0;
21+ for(int i=0;i<n;i++){
22+ for(int j=0;j<i;j++){
23+ if((hammingCheck(words[i],words[j]) && groups[i]!=groups[j]) && dp[j]+1>dp[i]){
24+ dp[i]=dp[j]+1;
25+ prev[i]=j;
26+ }
27+ }
28+ if(dp[i]>dp[maxIdx]){
29+ maxIdx=i;
30+ }
31+ }
32+ vector<string> ans;
33+ while(prev[maxIdx]!=maxIdx){
34+ ans.push_back(words[maxIdx]);
35+ maxIdx = prev[maxIdx];
36+ }
37+ ans.push_back(words[maxIdx]);
38+ reverse(ans.begin(),ans.end());
39+ return ans;
40+ }
41+ };
You can’t perform that action at this time.
0 commit comments