# Ex Oh Coderbyte Solution

0

Have the function ExOh(str) take the str parameter being passed and return the string true if there is an equal number of x's and o's, otherwise return the string false.Only these two letters will be entered in the string, no punctuation or numbers.

For example: if str is "xooxxxxooxo" then the output should return false because there are 6 x's and 5 o's.

## Ex Oh Coderbyte Solution In Java

import java.util.Scanner;

public class ExOh {
String ExOh(String str) {
int xCount = 0, oCount = 0;

for (char c : str.toCharArray()) {
if (c == 'x' || c == 'X') {
xCount++;
} else if (c == 'o' || c == 'O') {
oCount++;
}
}

return xCount == oCount ? "true" : "false";

}

public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
ExOh c = new ExOh();
System.out.print(c.ExOh(s.nextLine()));
}
}

## Ex Oh Coderbyte Solution In Python

def ExOh(str):
xs = 0
os = 0
for i in str:
if i == "x":
xs += 1
elif i == "o":
os += 1
return xs == os

print ExOh(raw_input())

## Ex Oh Coderbyte Solution In JavaScript

Using the JavaScript language, have the function ExOh(str) take the str parameter being passed and return the string true if there is an equal number of x's and o's, otherwise return the string false. Only these two letters will be entered in the string, no punctuation or numbers. For example: if str is "xooxxxxooxo" then the output should return false because there are 6 x's and 5 o's.

function ExOh(str) {

if (str.length % 2 === 1) {
return false;
}
else {
var tot = 0;
for (var i = 0; i < str.length; i++) {
if (str[i] === "x") {
tot++;
}
}
if (tot === (str.length / 2)) {
return true;
}
else {
return false;
}

}
}

Explanation

My solution accounts for the one outlier which is when the length of the string is not even. Since the count of Xs and Os have to be equal then the only way for this to happen is if the length of the string is an even number. I am going to loop through the string and count every X. Then I am going to compare this count to half the length of the string. If they are equal then you have equal number of Xs and Os.

Disclaimer: The above Problem is generated by Coderbyte but the Solution is provided by ShouterFolk.