Given a square grid of characters in the range ascii[a-z], rearrange elements of each row alphabetically, ascending. Determine if the columns are also in ascending alphabetical order, top to bottom. Return YES if they are or NO if they are not.Example:grid = [‘abc’, ‘ade’, ‘efg’]
Question
Given a square grid of characters in the range ascii[a-z], rearrange elements of each row alphabetically, ascending. Determine if the columns are also in ascending alphabetical order, top to bottom. Return YES if they are or NO if they are not.Example:grid = [‘abc’, ‘ade’, ‘efg’]
Solution
Sure, here is a step by step solution in Python:
- First, we need to sort each row in the grid. We can do this by iterating over each row and using the sorted() function in Python, which returns a sorted list of the elements in the input iterable.
for i in range(len(grid)):
grid[i] = ''.join(sorted(grid[i]))
- Next, we need to check if the columns are in ascending order. We can do this by iterating over each column and checking if the characters in the column are in ascending order.
for i in range(len(grid[0])):
for j in range(len(grid) - 1):
if grid[j][i] > grid[j + 1][i]:
return 'NO'
- If we have iterated over all the columns and have not returned 'NO', then all the columns are in ascending order, so we return 'YES'.
return 'YES'
Here is the complete function:
def grid_sorted(grid):
# Sort each row
for i in range(len(grid)):
grid[i] = ''.join(sorted(grid[i]))
# Check if columns are in ascending order
for i in range(len(grid[0])):
for j in range(len(grid) - 1):
if grid[j][i] > grid[j + 1][i]:
return 'NO'
return 'YES'
You can call this function with your grid as the argument:
grid = ['abc', 'ade', 'efg']
print(grid_sorted(grid))
This will print 'YES' if the columns in the grid are in ascending order after sorting the rows, and 'NO' otherwise.
Similar Questions
Question 10A data professional wants the data in column A to be in alphabetical order from A to Z. What spreadsheet feature would enable them to accomplish this task?(n/a)ABC1DecianiLin332RobertsJeremy283StorelliHyun644PiotrowskiSook845MondekPriscilla426LamApril961 pointSort rangeFormattingChart editorDragging
Given an m x n grid of characters board and a string word, return true if word exists in the grid.The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
Question 8Which function sorts a spreadsheet range between cells K1 and L80 in ascending order by the first column, Column K?1 point=SORT(K1:L80, A, TRUE)=SORT(K1:L80, A, FALSE)=SORT(K1:L80, 1, FALSE)=SORT(K1:L80, 1, TRUE)
Write a program to check whether a given character is Alphabet or not using if else statement Note: Check for both upper and lower case characters
Given a 2D character matrix grid, where grid[i][j] is either 'X', 'Y', or '.', return the number of submatrices that contains:grid[0][0]an equal frequency of 'X' and 'Y'.at least one 'X'. Example 1:Input: grid = [["X","Y","."],["Y",".","."]]Output: 3Explanation:Example 2:Input: grid = [["X","X"],["X","Y"]]Output: 0Explanation:No submatrix has an equal frequency of 'X' and 'Y'.Example 3:Input: grid = [[".","."],[".","."]]Output: 0Explanation:No submatrix has at least one 'X'. Constraints:1 <= grid.length, grid[i].length <= 1000grid[i][j] is either 'X', 'Y', or '.'.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.