DEV Community

Discussion on: Daily Challenge #29 - Xs and Os

Collapse
 
mariocsee profile image
Mario See

A little Python here.

def count_xo(s):
  count_x, count_o = 0, 0
  for c in s:
    if c.lower() == "x":
      count_x += 1
    if c.lower() == "o":
      count_o += 1
  return count_x == count_o

This method uses a for loop to go through each character. It converts each character to lower case for a case insensitive comparison with "x" and "o" and increments the respective counters. The return line checks for count equality to return a boolean.

The time complexity in big O notation is O(n) or linear, where n is the size of input string s, and the space complexity is O(1) or constant since it just uses two variables to keep track of counts.

Collapse
 
colinb profile image
Colin Bounouar

Is there a reason why you prefer a for loop over a single call to lower on the str, and then returning lowered.count('x') == lowered.count('o') ?