ProgrammingVery hardTCS NQT · Accenture
The string "aabbc" is examined by the procedure below, which counts the substrings (contiguous pieces, counted by position) that contain exactly 2 distinct characters. What is printed?
The string "aabbc" is examined by the procedure below, which counts the substrings (contiguous pieces, counted by position) that contain exactly 2 distinct characters. What is printed?
count = 0
for i = 0 to n - 1
for j = i to n - 1
if the substring s[i..j] has exactly 2 distinct characters then count = count + 1
end for
end for
Print count- A. 8
- B. 6
- C. 7
- D. 2
Correct answer: B. 6
The substrings with exactly two distinct letters are aab, aabb, ab, abb, bbc and bc, which is 6. Counting substrings with at least two distinct letters gives 8, counting the single-letter substrings gives 7, and counting substrings with three distinct letters gives 2.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The procedure below counts the positions i in "abbab" where the two-letter window s[i]s[i+1] is "ab" or "ba". What is printed? count = 0…
- The procedure below counts the positions i in "mississippi" where the two letters s[i]s[i+1] are both "s" (overlaps allowed, though "ss"…
- The palindromic substrings of length exactly 3 in the string "abacaba" are counted by the procedure below, which checks each window of…
- The number of distinct substrings (non-empty, counted once per distinct text) of the string "aab" is computed by the procedure below using…
- Two positions in the word "hello" are chosen (the letters at the two positions form a pair, and positions are distinct). The procedure…
- The procedure below counts the cyclic rotations of "abab" that are equal to "abab" itself, checking the rotations obtained by moving k…