We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Sherlock and the Valid String
Sherlock and the Valid String
+ 0 comments Python 3 solution
def isValid(s): dict1 = {} for i in s: if i in dict1: dict1[i] += 1 else: dict1[i] = 1 dict2 = {} for j in dict1.values(): if j in dict2: dict2[j] += 1 else: dict2[j] = 1 if len(dict2) == 1: return "YES" if len(dict2) == 2: count = list(dict2.keys()) freqs = list(dict2.values()) if (count[0] == 1 and freqs[0] == 1) or (count[1] == 1 and freqs[1] == 1): return "YES" if abs(count[0] - count[1]) == 1 and (freqs[0] == 1 or freqs[1] == 1): return "YES" return "NO"
+ 0 comments include
include
include
using namespace std;
string isValid(string s) { unordered_map charCount;
// Count the frequency of each character in the string for (char c : s) { charCount[c]++; } unordered_map<int, int> freqCount; // Count the frequency of frequencies for (const auto& pair : charCount) { freqCount[pair.second]++; } // If there's only one unique frequency, it's already valid if (freqCount.size() == 1) { return "YES"; } // If there are two unique frequencies, check if we can remove one character if (freqCount.size() == 2) { auto it = freqCount.begin(); int freq1 = it->first; int count1 = it->second; ++it; int freq2 = it->first; int count2 = it->second; // Check if we can remove one character to make it valid if ((count1 == 1 && (freq1 - 1 == freq2 || freq1 == 1)) || (count2 == 1 && (freq2 - 1 == freq1 || freq2 == 1))) { return "YES"; } } // If none of the conditions are met, it's not valid return "NO";
}
int main() { string s; cin >> s; string result = isValid(s); cout << result << endl; return 0; }
+ 0 comments Solution in C#
- Case I was stumped on was 'aaabbbc'. Take away 'c' and it satisfies the conditions. The obvious case for me was 'aaabbbcccc'.
if(String.IsNullOrEmpty(s)) { return "YES"; } List<char> sorted = s.OrderBy(x => x).ToList(); List<char> distinctChars = s.Distinct().OrderBy(x => x).ToList(); Dictionary<char, int> charCounter = new(); int count = 0; foreach(char c in distinctChars) { count = sorted.Count(x => x == c); charCounter.Add(c, count); } int distinctCharCounts = charCounter.Values.Distinct().Count(); if (distinctCharCounts == 1) { return "YES"; } if(distinctCharCounts == 2) { var equalToCount = charCounter.Where(x => x.Value == count).ToList(); var notEqualToCount = charCounter.Where(x => x.Value != count).ToList(); if (equalToCount.Count == 1) { if (count - 1 == 0 || count - 1 == notEqualToCount[0].Value) { return "YES"; } } if (notEqualToCount.Count == 1) { if (notEqualToCount[0].Value - 1 == 0 || notEqualToCount[0].Value - 1 == count) { return "YES"; } } } return "NO";
+ 0 comments My solution gets 100% on all tests but returns "yes" to the string "aaaabbbbcc" - I believe the testing may be incomplete or incorrect
+ 0 comments DON'T READ IF NOT LOOKING FOR SOLUTION my java soln
public static String isValid(String s) {
char[] c = s.toCharArray(); Arrays.sort(c); int temp=1; int res=1;//aabbccc for(int i=1;i<c.length;i++) { if(c[i]==c[i-1]) { temp++; } else{ res=temp; temp=i; break; } } int i=temp; int x=1,y=0; for( ;i<c.length;i++) { if(c[i]==c[i-1]) { temp++; } else{ if(Math.abs(res-temp) > 1) { return "NO"; } if(temp==res) { x++; } else{ y++; } temp=1; } } if(temp==res) { x++; } else{ y++; } if(x>1 && y>1) return "NO"; return "YES"; }
Load more conversations
Sort 1933 Discussions, By:
Please Login in order to post a comment