c# Dictionary solution


  • 0
    N
    public class Solution {
        Dictionary<string, int> dict = new Dictionary<string, int>();
        int max = -1;
        public int FindLUSlength(string[] strs) {
            
            foreach(var str in strs) {
                var subseqs = GetSubSeq(str);
                
                foreach(var subseq in subseqs) {
                    if(!dict.ContainsKey(subseq)){
                        dict[subseq] = 0;
                    }
                    dict[subseq]++;
                }
            }
            
            foreach(var kv in dict){
                if(kv.Value == 1) {
                    max = Math.Max(max, kv.Key.Length);
                }
            }
            
            return max;
        }
        
        public IList<string> GetSubSeq(string s) {
            var list = new List<string>();
            if(string.IsNullOrEmpty(s)) {
                list.Add(string.Empty);
                return list;
            }
            
            var subseqs= GetSubSeq(s.Substring(1));
            var res = new List<string>(subseqs);
            foreach(var sub in subseqs){
                list.Add(s[0] + sub);
            }
            res.AddRange(list);
            return res;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.