Grid Challenge Discussions | | HackerRank

Grid Challenge

Sort by

recency

|

203 Discussions

|

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

    The question says that the grid will be square, yet some of the test cases (at least Test Case 1) have nonsquare grids. This is easy enough to resolve in the code, but the question wording is misleading.

  • + 0 comments

    Here is one way to do it in javaScript

    function gridChallenge(grid) {
        // Write your code here
        const sortedGrid = grid.map(row => row.split('').sort().join(''))
    
        for(let col = 0; col < sortedGrid[0].length; col++){
            for(let row = 1; row < sortedGrid.length; row++){
                if(sortedGrid[row][col] < sortedGrid[row - 1][col]){
                    return "NO"
                }
            }
        }
    
        return "YES"
    }
    
  • + 0 comments

    Some of the test cases contain non-square grids

  • + 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.