Grid Challenge Discussions | | HackerRank

Grid Challenge

Sort by

recency

|

207 Discussions

|

  • + 0 comments

    Java 15

    public static String gridChallenge(List<String> grid) {
            final String YES = "YES";
            final String NO = "NO";
            int columnCount = grid.get(0).length();
            int rowCount = grid.size();
            
            var sortedRowsGrid = grid.stream()
                .map(row -> 
                    row.chars().sorted().mapToObj(num -> (char) num).collect(toList())
                ).collect(toList());
            
            for (int j = 0; j < columnCount; j++) {
                for (int i = 1; i < rowCount; i++) {
                    char currentLetter = sortedRowsGrid.get(i).get(j);
                    char previousLetter = sortedRowsGrid.get(i - 1).get(j);
                    
                    if (currentLetter < previousLetter) {
                        return NO;
                    }  
                } 
            }
            
            return YES;
        }
    
  • + 0 comments

    Easiest approch I found on CPP:

    string gridChallenge(vector grid) { bool ok = true; int n = grid.size();

    for (int i = 0; i < n; i++) {
        sort(grid[i].begin(), grid[i].end());    //Sorting taking care of row's
    }
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < grid[i].size()-1; j++) {
            if(grid[j][i] > grid[j+1][i]) //Taking care of coloumb
            {
                return "NO";
            }
        }
    }
    
    return "YES";
    

    }

  • + 0 comments

    C#

    public static string gridChallenge(List grid) {

    List<string> arrangedGrid = new List<string>();
    
        foreach(string s in grid)
        {
            arrangedGrid.Add(new string(s.OrderBy(x=>x).ToArray()));
    
        }               
    
        for(int row = 0; row < arrangedGrid.Count - 1; row++)
        {
            for (int column = 0; column < arrangedGrid[0].Count(); column++)
            {
                if(arrangedGrid[row][column] > arrangedGrid[row + 1][column])
                {
                    return "NO";
                }
            }
        }
    
        return "YES";
    
    }
    
  • + 0 comments

    Java

        public static String gridChallenge(List<String> grid) {
            int[][] values = new int[grid.size()][grid.get(0).length()];
            for (int i=0 ; i<grid.size() ; i++) {
                char[] chars = grid.get(i).toCharArray();
                Arrays.sort(chars);
                
                for (int j=0 ; j<chars.length ; j++) {
                    values[i][j] = (int) chars[j];
                    if (i > 0 && values[i-1][j] > values[i][j]) {
                         return "NO";
                    }
                }     
            }
            return "YES";
        }
    
  • + 0 comments

    There is no way to run out of O(n²) because of the column's checking, but follow a O(n) way to sort each row's string,

    function gridChallenge(grid) {
        for (let i = 0; i < grid.length; i++) {
            grid[i] = sortString(grid[i]);
        }
        let answer = 'YES';
        for (let idr = 1; idr < grid.length; idr++) {
            for (let idc = 0; idc < grid[0].length; idc++) {
                if (grid[idr][idc] < grid[idr-1][idc]) {
                    answer = 'NO';
                    break;
                }
            }
        }
        return answer;
    }
    
    function sortString(str) {
        const ALPHABET_SIZE = 26;
        const charCount = Array(ALPHABET_SIZE).fill(0);
        const asciiStartLowercase = "a".charCodeAt(0);
    
        for (const char of str) {
            charCount[char.charCodeAt(0) - asciiStartLowercase]++;
        }
    
        let sortedStr = "";
        for (let i = 0; i < charCount.length; i++) {
            if (charCount[i] > 0) {
                sortedStr += String.fromCharCode(i + asciiStartLowercase).repeat(charCount[i]);
            }
        }
        return sortedStr;
    }