You are viewing a single comment's thread. Return to all comments →
if you run the code that exceed the time limit, try to use the hashmap to store the data C# code
public static void noPrefix(List<string> words) { var hashSetData = new Dictionary<string,List<int>>(); var num = words.Count; var prefixes = new List<string>(); for(int i = 0 ; i< num ; i++) { var word = words[i]; if(hashSetData.ContainsKey(word)) { hashSetData[word].Add(i);} else { var data = new List<int>(); data.Add(i); hashSetData.Add(word , data ); } } var nextIndexCheckData = num + 1; for(int i = 0 ; i < num ; i++) { var word = words[i]; if(i == nextIndexCheckData) { Console.WriteLine("BAD SET\n" + word); return; } var length = word.Length; for(int j = length ; j >= 1 ; j--) { var subString = word.Substring(0 , j); if(hashSetData.ContainsKey(subString)) { var data = hashSetData[subString]; foreach(var idx in data) { if(idx < i) { Console.WriteLine("BAD SET\n" + word); return; } else if (idx > i && idx < nextIndexCheckData) { nextIndexCheckData = idx; } } } } } Console.WriteLine("GOOD SET"); }
Seems like cookies are disabled on this browser, please enable them to open this website
No Prefix Set
You are viewing a single comment's thread. Return to all comments →
if you run the code that exceed the time limit, try to use the hashmap to store the data C# code