A little Python here.
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.
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') ?
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.