• + 0 comments

    C#:

    public static string happyLadybugs(string b)
        {
            Dictionary<char, int> d = new Dictionary<char, int>();
            foreach(char c in b)
            {
                if(!d.ContainsKey(c))
                {
                    d.Add(c, 0);
                }
                d[c]++;
            }
            bool has_ = false;
            foreach(var item in d)
            {
                if(item.Key != '_' && item.Value == 1)
                {
                    return "NO";
                }
                if(item.Key == '_')
                {
                    has_ = true;
                }
            }
            
            if(!has_)
            {
                for(int i = 0; i < b.Count() - 1; i++)
                {
                    if(b[i] != b[i+1] && (i != 0 && b[i] != b[i-1]))
                    {
                        return "NO";
                    }
                }
            }
            
            return "YES";
        }