Grid Challenge Discussions | Algorithms | HackerRank

Grid Challenge

  • + 0 comments

    My solution, it might complex

    def gridChallenge(grid):
        # Sort each row
        for idx in range(len(grid)):
            strList = list(grid[idx])
            strList.sort()
            grid[idx] = "".join(strList)
        # Join all grid list to a single string
        strJoin = "".join(grid)
        gLength = len(grid)
    		# Create the column list based on grid length
        reverseGrid = [0] * len(grid)
        for i in range(len(strJoin)):
    		    # get the ascii code
            curr = ord(strJoin[i])
    				# Get the saved grid ascii code based on index % length
            prev = reverseGrid[i % gLength]
            diff = curr - prev
    				# if the diff < 0, means current ascii value less than previous, which means it's not in order
            if diff < 0:
                return "NO"
    
            reverseGrid[i % gLength] = curr
        return "YES"