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=" ")- A. 0 0 0
- B. 2 1 0
- C. 2 2 0
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A string of length n is searched for a pattern of length m. What are the worst-case times of the naive algorithm and of KMP?
- How many inversions (pairs i < j with a[i] > a[j]) does [35, 33, 31, 3, 20, 18, 13, 4] have? (This is also the number of swaps bubble sort…
- The Dutch national flag algorithm (pointers low, mid, high; 0 → swap with low and advance both; 1 → advance mid; 2 → swap with high and…
- A mountain is a contiguous subarray of length ≥ 3 that strictly increases to a single peak and then strictly decreases. What is the length…
- For each day, a monotonic stack finds how many days you must wait for a strictly warmer temperature (0 if never). What is printed? python…
- Count palindromic substrings by expanding around all 2n − 1 centres, and also count DISTINCT palindromic substrings. What is printed?…