• + 0 comments

    C# Solution:

    public static string happyLadybugs(string b)
        {
            Dictionary<char,int> map = new Dictionary<char,int>();
            // Case for checking if string contains all "_"
            bool checkUnderScore = false; 
            bool containsUnderScore  = false;
            for(int i=0;i<b.Length;i++)
            {
                if(b[i]>='A' && b[i] <= 'Z')
                {
                    checkUnderScore = true;
                    
                }
                else
                {
                   containsUnderScore = true; 
                } 
                
            }
            if(checkUnderScore == false)
            {
                return "YES";
            }
            if(containsUnderScore==false)
            {
                int left=0;
                int right=0;
               for(int i=1;i<b.Length-1;i++)
               {
                   left = i-1;
                   right= i+1;
                   if(b[left] != b[i] && b[right] != b[i])
                   {
                       return "NO";
                   }
               } 
            }
            // Other Cases
            for(int i=0;i<b.Length;i++)
            {
                if(map.ContainsKey(b[i]))
                {
                    map[b[i]]++;
                }
                else
                {
                    map[b[i]] = 1;
                }
            }      
            foreach(var item in map)
            {
                if(item.Key!='_' && item.Value == 1)
                {
                    return "NO";
                }
            }
            return "YES";
        }