• + 0 comments

    Java clean solutaion

    public static List<Character> getAdjacent(List<String> grid, int i,int j){
        return Arrays.asList(grid.get(i+1).charAt(j), grid.get(i-1).charAt(j),
                grid.get(i).charAt(j+1), grid.get(i).charAt(j-1));
        }
        
        public static int getInt(char c){
            return c - '0';
            }
           
        public static List<String> cavityMap(List<String> grid) {
        int n = grid.size();
        List<String> new_grid = new ArrayList<>();
        
        for(int i = 0; i < n; i++){
            String line = "";
            for(int j = 0; j < n; j++){
                char c = grid.get(i).charAt(j);
                if(i == 0 || i == n-1 || j == 0 || j == n-1){
                    line += c;
                    continue;
                    }
                boolean is_cavity = true;
                
                
                for(char adj: getAdjacent(grid, i, j)){
                    if(getInt(adj) >= getInt(c))
                      is_cavity = false; 
                    }
                if(is_cavity)
                    c = 'X';
                
                line += c;
                
                }
            new_grid.add(line);
            }
        
        return new_grid;
    
        }