# Check if the chain has the same amount

### José Oscátegui github logo Jun 15 '18・1 min read

Check if a string has the same amount of 'x' and 'o'. The method must return a Boolean value and must not be case-sensitive. The string can contain any char.

DISCUSS (12)
Discussion In Haskell, with what I thought was kind of a neat method:

``````import Data.Char

val 'x' = 1
val 'o' = -1
val other = 0

check str
| (sum \$ map (val . toLower) str) == 0 = True
| otherwise = False
``````

As a javascript one-liner:

``````string.toLowerCase().split('').map(a => a == 'x' ? 1 : a == 'o' ? -1 : 0).reduce((acc, a) => acc+a) == 0 ? true : false
``````

Ruby Solution!

``````def character_counts(str)
str.chars.each_with_object(Hash.new(0)) { |char, hash| hash[char] += 1 }
end

def check(str)
str.downcase!
char_count = character_counts(str)
char_count['o'] == char_count['x']
end

``````
``````irb(main):003:0> check('xoxo')
=> true
irb(main):004:0> check('xaabboccddxeeffo')
=> true
irb(main):005:0> check('x')
=> false
irb(main):006:0> check('ooooox')
=> false
``````

A simple solution with Rust.

``````fn main() {
println!(
"{} the same amount",
if has_same_amount("\$XxkkLLOoox-", "x", "o") {
"Has"
} else {
"Not has"
}
)
}

fn has_same_amount(str: &str, ch1: &str, ch2: &str) -> bool {
let str = str.to_lowercase();

str.matches(ch1).count() == str.matches(ch2).count()
}
``````
``````Has the same amount
``````

Time for a Go kata you say?

``````
func IsBalanced(dic map[rune]int, s string) bool {
b := 0
for _, r := range strings.ToLower(s) {
i, ok := dic[r]
if ok == false {
continue
}
b += i
}
return b == 0
}

func main() {
//because the world is bigger than Latin
var step = map[rune]int{'x': 1, '🗴': 1, 'o': -1, 'ອ': -1}
table := map[string]bool{
"":true, //0x == 0o
"x0x0": true,
"ອXອXອ":  false,
"H🗴H🗴H(╯° °）╯︵ ┻━┻)HoHo":true,
}

for input, exp := range table {
fmt.Printf("'%s', exp: '%v', got: '%v'\n", input, exp, IsBalanced(step, input))
}

}

``````

play.golang.org/p/n8q9BYrsW4a

Classic DEV Post from Jul 26 '19

## 🎩 JavaScript Enhanced Scss mixins! 🎩 concepts explained  