문방구앞오락기 2017. 7. 21. 16:12

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
46
47
48
49
50
51
52
53
54
55
package Suss;
//Check to see if a string has the same amount of 'x's and 'o's. 
//The method must return a boolean and be case insensitive. 
//The string can contains any char.
//
//Examples input/output:
//
//XO("ooxx") => true
//XO("xooxx") => false
//XO("ooxXm") => true
//XO("zpzpzpp") => true // when no 'x' and 'o' is present should return true
////XO("zzoo") => false
 
public class CodeWar_3 {
 
     public static boolean getXO(String str) {
    
        str = str.toLowerCase();
        int o_c = 0;
        int x_c = 0;
        boolean out = false;
             
        for(int i =0 ;i<str.length() ; i++){
            if(String.valueOf(str.charAt(i)).equals("o")) o_c++;
            if(String.valueOf(str.charAt(i)).equals("x")) x_c++;
            
        }
            if( (o_c ==x_c) || (o_c+x_c == 0)) out=true;
        
         System.out.println(out);
         return out;
    }
     
    
     public static boolean best_getXO (String str) {
            str = str.toLowerCase();
            return str.replace("o","").length() == str.replace("x","").length();
            
          }
    
    
    public static void main(String[] args) {
        
        CodeWar_3 c = new CodeWar_3();
        
        c.getXO("ooxx");
        c.getXO("xooxx");
        c.getXO("ooxXm");
        c.getXO("zpzpzpp");
        c.getXO("zzoo");
        
        
    }
}