Grid Challenge Discussions | Algorithms | HackerRank

Grid Challenge

Sort by

recency

|

591 Discussions

|

  • + 0 comments

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

    string gridChallenge(vector<string> grid) {
        for(int i = 0; i < grid.size(); i++) sort(grid[i].begin(), grid[i].end());
        for(int col = 0; col < grid.size(); col++){
           for(int row = 1; row < grid.size(); row++)
                if(grid[row][col] < grid[row-1][col]) return "NO";
        }
        return "YES";
    }
    
  • + 0 comments

    Haskell

    module Main where
    
    import Control.Monad (replicateM, replicateM_)
    import Data.List (sort, transpose)
    
    solve :: (Ord a) => [[a]] -> Bool
    solve grid = all (\xs -> xs == sort xs) $ transpose $ map sort grid
    
    main :: IO ()
    main = do
        cases <- readLn :: IO Int
        replicateM_
            cases
            ( do
                n <- readLn :: IO Int
                grid <- replicateM n getLine
                putStrLn $ if solve grid then "YES" else "NO"
            )
    
  • + 0 comments

    Kotlin:

    fun gridChallenge(grid: Array<String>): String
    {
        val sorted_grid = grid.map { it.toCharArray().sorted() }
        
        for (i in 0..<grid.first().length)
        {
            val col_list = sorted_grid.map { it.get(i) }
            if (col_list != col_list.sortedBy { it }) return "NO"
        }
        
        return "YES"
    }
    
  • + 0 comments
    def gridChallenge(grid):
        sorted_words = [''.join(sorted(word)) for word in grid]
        sorted_words = [''.join(word) for word in list(zip(*sorted_words))]
        for word in sorted_words:
            for i in range(len(word)-1):
                # print(word[i+1], word[i])
                if word[i+1] >= word[i]:
                    pass
                else:
                    return 'NO'     
        return 'YES'
        
    
  • + 0 comments
    def gridChallenge(grid):
        import copy
        Grid = copy.deepcopy(grid)
        for i in range(len(Grid)):
            Grid[i] = sorted(list(Grid[i]))
        for row in zip(*Grid):
            if list(row) != sorted(row):
                return 'NO'
        return 'YES'