Data Structures & AlgorithmsModerateTCS Digital · Infosys SP

Find the index of the first non-repeating character using a frequency table (−1 if none). What is printed?

Find the index of the first non-repeating character using a frequency table (−1 if none). What is printed?
from collections import Counter
for s in ['cbabcbb', 'ccbadad', 'cbdbddd']:
    c = Counter(s)
    print(next((i for i, ch in enumerate(s) if c[ch] == 1), -1), end=" ")
  1. A. 0 0 0
  2. B. 2 1 0
  3. C. 2 2 0
  4. D. 1 2 0

Correct answer: C. 2 2 0

Two passes: count every character, then return the first position whose count is 1. Output: 2 2 0.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic