Grid Challenge Discussions | | HackerRank

Grid Challenge

Sort by

recency

|

199 Discussions

|

  • + 0 comments

    I thought this question was misleading. It said that we would tackle a square grid with n rows, where each row contains a string of length n, but one of the test cases had n=3 with four-letter strings.

  • + 0 comments

    c++ solution because strings are essentially vectors of chars, we can sort them alphabetically using built in sort function. then we check all columns for any pair of consecutive characters that are not in ascending order

    string gridChallenge(vector<string> grid) {
        bool ok = true;
        int n = grid.size();
        
        for (int i = 0; i < n; i++) {
            sort(grid[i].begin(), grid[i].end());    
        }
        
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < n; j++) {
                ok &= (grid[i-1][j] <= grid[i][j]);
            }
        }
        
        return ok ? "YES" : "NO";
    }
    
  • + 0 comments

    Python

    def gridChallenge(grid):
        # Write your code here 
        result = ([sorted(subarr) for subarr in grid])
        if all(result[i][j] <= result[i + 1][j] for i in range(len(result) - 1) for j in range(len(result[0]))):
    
            return "YES"
    
        return "NO"
    
  • + 0 comments

    C#

    public static string gridChallenge(List<string> grid)
        {
            for (int i = 0; i < grid.Count(); i++)
            {
                char[] characters = grid[i].ToArray();
                Array.Sort(characters);
                grid[i] = new string(characters);
            }
    
            bool alphaOrder = true;
            if (grid.Count > 1)
            {
                for (int i = 0; i < grid.Count() - 1; i++)
                {
                    for (int j = 0; j < grid[0].Count(); j++)
                    {
                        if (String.Compare(grid[i].Substring(j, 1), grid[i + 1].Substring(j, 1)) > 0)
                        {
    
                            alphaOrder = false;
                        }
                    }
                }
            }
    
            return alphaOrder ? "YES" : "NO";
    
        }
    
  • + 0 comments
    def gridChallenge(grid):
        return min(['YES' if list(c) == sorted(c) else 'NO' for c in zip(*[sorted(grid[r]) for r in range(len(grid))])])