my approch is
- find H and if there is H find right and find up
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 44 45 | package srm643; public class TheKingsArmyDiv2 { public static void main(String args[]){ String[] temp = {"SSSSS", "SSHHS", "SSSSS"}; TheKingsArmyDiv2 army = new TheKingsArmyDiv2(); System.out.println(army.getNumber(temp)); } public int getNumber(String[] state){ int len = state.length; int count =0; for(int i=0; i<len; i++){ char [] emotions = state[i].toCharArray(); int elen = emotions.length; for(int j=0; j<elen; j++){ if(emotions[j] =='H'){ count++; if(j+ 1 < elen && emotions[j+1] =='H'){ return 0; } if(i+1 < len && state[i+1].charAt(j) =='H'){ return 0; } } } } if(count > 0){ return 1; }else{ return 2; } } } |