2023. 1. 29. 19:56ㆍJava/coding test
Grid Challenge
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
The grid is illustrated below.
a b c a d e e f g
The rows are already in alphabetical order. The columns a a e, b d f and c e g are also in alphabetical order, so the answer would be YES. Only elements within the same row can be rearranged. They cannot be moved to a different row.
Function Description
Complete the gridChallenge function in the editor below.
gridChallenge has the following parameter(s):
- string grid[n]: an array of strings
Returns
- string: either YES or NO
Input Format
The first line contains , the number of testcases.
Each of the next sets of lines are described as follows:
- The first line contains , the number of rows and columns in the grid.
- The next lines contains a string of length
Constraints
Each string consists of lowercase letters in the range ascii[a-z]
Output Format
For each test case, on a separate line print YES if it is possible to rearrange the grid alphabetically ascending in both its rows and columns, or NO otherwise.
Sample Input
STDIN Function ----- -------- 1 t = 1 5 n = 5 ebacd grid = ['ebacd', 'fghij', 'olmkn', 'trpqs', 'xywuv'] fghij olmkn trpqs xywuv
Sample Output
YES
Explanation
The 5x5 grid in the 1 test case can be reordered to
abcde fghij klmno pqrst uvwxy
This fulfills the condition since the rows 1, 2, ..., 5 and the columns 1, 2, ..., 5 are all alphabetically sorted.
static String gridChallenge(List<String> grid) { String prevStr = sort(grid.get(0)); for (int i = 1; i < grid.size(); i++) { String sortedStr = sort(grid.get(i)); for (int j = 0; j < sortedStr.length(); j++) { if(prevStr.charAt(j) > sortedStr.charAt(j)) { return "NO"; } } } return "YES"; } static String sort(String s) { char[] arr = s.toCharArray(); Arrays.sort(arr); return new String(arr); }
'Java > coding test' 카테고리의 다른 글
[Codility - Java] 2. Array - 1. CyclicRotation (1) | 2023.01.31 |
---|---|
[Codility - Java] 1. Iterations - 1. Binary Gap (0) | 2023.01.31 |
[HackerRank - Java] Day 6 - 1. Simple Text Editor (0) | 2023.01.29 |
[HackerRank - Java] Day 4 - 2. Recursive Digit Sum (0) | 2023.01.28 |
[HackerRank - Java] Day 3 - 2. Tower Breakers (0) | 2023.01.28 |