-
Notifications
You must be signed in to change notification settings - Fork 13
/
organic-compounds.java
28 lines (25 loc) · 1.16 KB
/
organic-compounds.java
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
import java.util.*;
import java.io.*;
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
String c[] = new String[N]; in.nextLine();
for (int i = 0; i < N; i++) { c[i] = in.nextLine().toUpperCase(); }
for (int i = 0; i < N; i++) {
if (c[i].indexOf('C')==-1) { continue; }
for (int j = 0; j < c[i].length()-2; j++) {
if (c[i].substring(j,j+2).equals("CH")) {
int b = Integer.parseInt(""+c[i].charAt(j+2));
try { b += Integer.parseInt(""+c[i-1].charAt(j+1)); } catch (Exception e) {}
try { b += Integer.parseInt(""+c[i+1].charAt(j+1)); } catch (Exception e) {}
try { b += Integer.parseInt(""+c[i].charAt(j-2) ); } catch (Exception e) {}
try { b += Integer.parseInt(""+c[i].charAt(j+4) ); } catch (Exception e) {}
if (b!=4) { System.out.println("INVALID"); return; }
j+=5;
}
}
}
System.out.print("VALID");
}
}