• + 0 comments

    C++ Solution:

    struct p {
      int cnt = 0;
      int lp = 0;
    };
    
    string happyLadybugs(string b) {
      
      map<char, p> m;
    
      bool right = true;
      int pos = 0;
      for(auto& a : b) {
        m[a].cnt += 1;
        if(a != '_' && m[a].lp > 0 &&  m[a].lp != pos-1)
          right = false;
        m[a].lp = pos;
        pos++;
      }
      
      for(auto& a : m){
        if(a.first != '_' && a.second.cnt < 2) {
          return "NO";
        }
      }
      
      if(right)
        return "YES ";
    
      if(m.count('_') == 0)
        if(m.size() == 1 && (*m.begin()).second.cnt > 1)
          return "YES ";
        else
          return "NO";
      else {
        if(m.size() == 2 && 
           (*m.begin()).second.cnt + (*m.begin()++).second.cnt == b.size())
          return "YES ";
      }
    
      return "YES";
    }