Data Structures & AlgorithmsHardGATE CS · TCS NQT
What is the minimum number of single-character insertions, deletions and substitutions needed to turn "sunday" into "saturnday"?
- A. 3
- B. 6
- C. 4
- D. 9
Correct answer: A. 3
This is the edit (Levenshtein) distance: dp[i][j] = dp[i-1][j-1] if the characters match, otherwise 1 + min(insert, delete, substitute). Counting only insertions and deletions gives m + n − 2·LCS, which is larger whenever substitution would have helped.
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
- What is the minimum number of characters you must delete from "abcbdaxyzyx" so that the remaining string is a palindrome?
- How many palindromic substrings (counted by position, so repeated text at different positions counts separately) does "abacabad" have? A…
- What is the minimum number of cuts needed to split "abbacdcdc" into pieces that are each a palindrome?
- What is the length of the shortest string that has both "abcbdab" and "bdcaba" as subsequences?
- How many DIFFERENT non-empty subsequences does the string "abcabc" have? Two subsequences are different if their letter sequences differ.
- How many subsequences (chosen by index) of "aabbabcaccb" spell exactly the word abc?