Grid Challenge Discussions | | HackerRank

Grid Challenge

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