COUNTAB - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: pols_agyi_pols
Tester: kingmessi
Editorialist: iceknight1093

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

Given a string S consisting of only the characters a and b, print the counts of each of a and b.

EXPLANATION:

Do what is asked for: run a loop and store the counts of 'a' and 'b' separately, and print them in the end.

Alternately, you can use your language’s inbuilt counting function - which most modern languages such as C++, Java, Python all have.

TIME COMPLEXITY:

\mathcal{O}(N) per testcase.

CODE:

Editorialist's code (PyPy3)
for _ in range(int(input())):
    n = int(input())
    s = input()
    print(s.count('a'), s.count('b'))