You are viewing a single comment's thread. Return to all comments →
Can anyone tell me why test cases 7 & 13 are failing:
public static string isValid(string s) { if (s.Length == 1) return "YES"; Dictionary dict = new Dictionary(); foreach(char c in s){ if(!dict.ContainsKey(c)){ dict.Add(c, 1); }else{ dict[c]++; } }
// Get a list of counts var counts = dict.Values.ToList(); // Check if there's only 1 unique count -> YES if(counts.Distinct().Count() == 1) return "YES"; // If >2 unique counts -> NO if(counts.Distinct().Count() > 2) return "NO"; // Try differnet approach, with sorting counts.Sort(); var first = counts[0]; var second = counts[1]; var secondLast = counts[counts.Count-2]; var last = counts[counts.Count-1]; if(first == last) return "YES"; if((first-1)==second && second==last) return "YES"; if(first==1 && second==2 && last==2) return "YES"; if(counts.Count >=2){ if(first==secondLast && secondLast==(last-1)) return "YES"; if(first==2 && secondLast==2 && last==1) return "YES"; } return "NO"; }
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and the Valid String
You are viewing a single comment's thread. Return to all comments →
Can anyone tell me why test cases 7 & 13 are failing:
public static string isValid(string s) { if (s.Length == 1) return "YES"; Dictionary dict = new Dictionary(); foreach(char c in s){ if(!dict.ContainsKey(c)){ dict.Add(c, 1); }else{ dict[c]++; } }