forked from sowon-dev/AlgorithmStudy_Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridChallenge.java
More file actions
43 lines (37 loc) ยท 1.81 KB
/
GridChallenge.java
File metadata and controls
43 lines (37 loc) ยท 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package hackerrank;
import java.util.Arrays;
public class GridChallenge {
static String gridChallenge(String[] grid) {
// ๊ทธ๋ฆฌ๋์ ์ด์ด ์ฌ์ ์ ์ ๋ ฌ์ด๋ฉด YES๋ฅผ ์ถ๋ ฅํ๋ ๋ฌธ์
// ๋จ, ๊ทธ๋ฆฌ๋์ ํ์ ์์๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ค.
// 1. ํ์ ์ฌ์ ์ ์ ๋ ฌํ๊ธฐ
// String์ char๋ก ๋ณํํ์ฌ ์ฌ์ ์์ผ๋ก sortํ ํ ๋ค์ grid[i]๋ฒ์งธ์ ๋ฃ๋๋ค.
// System.out.println(Arrays.toString(grid));
for (int i = 0; i < grid.length; i++) {
char[] chars = grid[i].toCharArray();
Arrays.sort(chars);
grid[i] = String.valueOf(chars);
}
// System.out.println(Arrays.toString(grid));
// 2. ์ด์ด ์ฌ์ ์ ์ ๋ ฌ์ธ์ง ํ์ธํ๊ธฐ
// ์ง๊ธ ์ด๊ณผ ๋ค์ ์ด์ i๋ฒ์งธ๋ฅผ ๋น๊ตํ์ฌ ์ฌ์ ์์ด ์๋๋ฉด NO๋ฅผ ์ถ๋ ฅ
for (int i = 0; i < grid[0].length(); i++) {
for (int j = 0; j < grid.length - 1; j++) {
// System.out.println(grid[j].charAt(i)+", "+grid[j + 1].charAt(i));
if (grid[j].charAt(i) > grid[j + 1].charAt(i)) {
return "NO";
}
}
}
return "YES";
}
public static void main(String[] args) {
System.out.println(gridChallenge(new String[]{"abc", "ade", "efg"}) + ", ans:YES");
// System.out.println(gridChallenge(new String[]{"ebacd", "fghij", "olmkn", "trpqs", "xywuv"}) + ", ans:YES");
// System.out.println(gridChallenge(new String[]{"kc", "iu"}) + ", ans:YES");
System.out.println(gridChallenge(new String[]{"uxf", "vof", "hmp"}) + ", ans:NO");
// System.out.println(gridChallenge(new String[]{"ppp", "ypp", "wyw"}) + ", ans:YES");
// System.out.println(gridChallenge(new String[]{"eibjbwsp", "ptfxehaq", "jxipvfga", "rkefiyub", "nflvjznh"}) + ", ans:NO");
// System.out.println(gridChallenge(new String[]{"kalwfhfj", "lktajiaq", "srdgoros"}) + ", ans:YES");
}
}