Sort by

recency

|

512 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/khDcPEl6to0

    string happyLadybugs(string b) {
        vector<int> occ(26, 0);
        bool happy = true, underscore = false;
        b = "0"+b+"0";
        for(int i = 1; i < b.size()-1; i++){
            if(b[i] != '_' && b[i] != b[i-1] && b[i] != b[i+1]) happy = false;
            if(b[i] == '_') underscore = true;
            else occ[b[i] - 'A']++;
        }
        if(happy) return "YES";
        if(underscore && find(occ.begin(), occ.end(), 1) == occ.end()) return "YES";
        return "NO";
    }
    
  • + 0 comments

    Haskell

    Slightly sloppy, but good enough.

    module Main where
    
    import Control.Monad (replicateM_)
    import Data.List (group)
    import Data.Map (Map, delete, fromListWith, (!?))
    
    main :: IO ()
    main = do
        cases <- readLn :: IO Int
        replicateM_ cases $ do
            _ <- getLine
            s <- getLine
            putStrLn $ if solve s then "YES" else "NO"
    
    t1 = "RBY_YBR"
    t2 = "X_Y__X"
    t3 = "B_RRBR"
    tests = [t1, t2, t3]
    
    happyEnough :: String -> Bool
    happyEnough = all ((>= 2) . length) . group . filter (/= '_')
    
    freq :: String -> Map Char Int
    freq = fromListWith (+) . map (\x -> (x, 1))
    
    solve :: String -> Bool
    solve s = happyEnough s || (bSpace && bAlpha)
      where
        fs = freq s
        bSpace = maybe False (>= 1) (fs !? '_')
        bAlpha = all (>= 2) $ delete '_' fs
    
  • + 0 comments

    Python 3 Just filter for happy conditions

    from collections import Counter
    def happyLadybugs(b):
        # Write your code here
        if len(b) == 1 and b[0] == '_':
            return 'YES'
        if len(b) == 1 and b[0] != '_':
            return 'NO'
        c = Counter(b)
        flag = 0
        if c['_'] == 0:
            for i in range(len(b)):
                if i == 0:
                    if b[i] == b[i+1]:
                        continue
                    else:
                        flag+=1
                if i == len(b)-1:
                    if b[i] == b[i-1]:
                        continue
                    else:
                        flag+=1
                if i > 0 and i < len(b)-1:
                    if b[i] == b[i-1] or b[i] == b[i+1]:
                        continue
                    else:
                        flag+=1
                        
        for m, n in c.items():
            if n <= 1 and m != '_':
                return 'NO'
                
        return 'YES' if not flag else 'NO'
    
  • + 0 comments

    Python Solution

    def happyLadybugs(b):
            # Write your code here
    
            for colour in set(b):
                    if colour != "_" and b.count(colour) == 1:
                            return "NO"
    
            if b.count("_") > 0:
                    return "YES"
    
            for i in range(len(b)):
                    if i == 0 and b[0] != b[1]:
                            return "NO"
                    if i == len(b) - 1 and b[-1] != b[-2]:
                            return "NO"
                    if b[i] != b[i-1] and b[i] != b[i+1]:
                            return "NO"
    
            return "YES"
    
  • + 0 comments

    for Python3 Platform

    def happyLadyBugs(b):
        for i in b:
            if i != "_" and b.count(i) == 1:
                return "NO"
                break
        
        if (b.count("_") > 0):
            return "YES"
        
        z = list(zip(b, b[1:], b[2:]))
        
        return "YES" if all(map(lambda t: t[1]==t[0] or t[1]==t[2], z)) else "NO"
    
    g = int(input())
    for i in range(g):
        n = int(input())
        b = input()
        
        result = happyLadyBugs(b)
        
        print(result)