Skip to content

Commit d548e4e

Browse files
committed
Solution as on 26-06-2022 08:30 am
1 parent cc8a783 commit d548e4e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

1048. Longest String Chain.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// 1048.✅ Longest String Chain
2+
3+
class Solution
4+
{
5+
public:
6+
int longestStrChain(vector<string> &words)
7+
{
8+
unordered_map<string, int> dp;
9+
int res = 1;
10+
sort(words.begin(), words.end(), [](const string &l, const string &r)
11+
{ return l.size() < r.size(); });
12+
for (string word : words)
13+
{
14+
dp[word] = 1;
15+
for (int i = 0; i < word.size(); i++)
16+
{
17+
string prev = word.substr(0, i) + word.substr(i + 1);
18+
if (dp.find(prev) != dp.end())
19+
{
20+
dp[word] = dp[prev] + 1;
21+
res = max(res, dp[word]);
22+
}
23+
}
24+
}
25+
return res;
26+
}
27+
};

0 commit comments

Comments
 (0)