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 contain 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
my:
public class OX {
public static boolean getXO (String str) {
// Good Luck!!
if (str.trim().isEmpty())
return true;
char chars[]=str.toLowerCase().toCharArray();
int numO=0;
int numX=0;
for (int i=0;i<chars.length;i++)
{
if (chars[i]=='x')
numX++;
if (chars[i]=='o')
numO++;
}
return numO==numX?true:false;
}
}
others:
public class XO {
public static boolean getXO (String str) {
str = str.toLowerCase();
return str.replace("o","").length() == str.replace("x","").length();
}
}
思维角度的不同啊