-
Notifications
You must be signed in to change notification settings - Fork 115
/
1048-LongestStringChain.cs
49 lines (42 loc) · 1.43 KB
/
1048-LongestStringChain.cs
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
43
44
45
46
47
48
49
//-----------------------------------------------------------------------------
// Runtime: 216ms
// Memory Usage: 31.5 MB
// Link: https://leetcode.com/submissions/detail/372933116/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace LeetCode
{
public class _1048_LongestStringChain
{
public int LongestStrChain(string[] words)
{
var chainLength = new Dictionary<string, int>();
var set = new HashSet<string>(words);
int max = 0;
foreach (var word in words)
{
DFS(word, set, chainLength);
max = Math.Max(max, chainLength[word]);
}
return max;
}
private int DFS(string word, HashSet<string> set, Dictionary<string, int> chainLength)
{
if (string.IsNullOrEmpty(word) || !set.Contains(word)) return 0;
if (chainLength.ContainsKey(word)) return chainLength[word];
var sb = new StringBuilder(word);
var max = 0;
for (int i = 0; i < word.Length; i++)
{
var ch = sb[i];
sb.Remove(i, 1);
max = Math.Max(max, DFS(sb.ToString(), set, chainLength) + 1);
sb.Insert(i, ch);
}
chainLength[word] = max;
return max;
}
}
}