• + 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