1. Given two text strings A = a1a2...am of length m and B = b1b2...bn of length n, you want to convert A into B with a minimum number of total operations of the following types: • delete a character from A, • insert a character into A, or • change some character in A into a new character. Examples: (1) A = "geek", B = "gesek". Optimal result: 1 - by inserting a 's'. (2) A = "Sunday", B = "Saturday". Optimal result: 3. Last three and first characters are same. So we just need to convert "un" into "atur": replace 'n' with 'r', insert 'a' and 't'. Design a dynamic programming algorithm. Note that you should first derive the recurrence formula, then write the iterative algorithm to find the minimal number of total operations for conversions, and write the algorithm to display the detailed operations (i.e. delete, insert, or replace).
Added by Jerry C.
Close
Step 1
First, let's define a 2D array dp[m+1][n+1], where dp[i][j] represents the minimum number of operations required to convert the first i characters of string A to the first j characters of string B. Now, let's derive the recurrence formula: Show more…
Show all steps
Your feedback will help us improve your experience
Akash M and 81 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Recursively Defined Sequence #1: Bit Strings Define the sequence Bn = Number of different n-bit binary strings that contain at least one bit, but that do not contain the substring 111. For example, B3 = 7 because the following seven bit strings of length 3 do not contain 111: 000, 001, 010, 011, 100, 101, 110. Give a recursive definition for Bn. Your definition must include: - All the initial conditions of the sequence - A recurrence relation for Bn as a function of some of the elements of the sequence that precede it - The values of n for which this recurrence relation applies - An explanation of how this recurrence relation was derived. Note that the explanation is the most important part of this question. The way to approach this question (and the next one) is very much like we approached the Towers of Hanoi: we figured out a recursive algorithm to move the discs, then derived the recurrence relation for the cost, based on the algorithm. Here, you need to figure out a recursive algorithm to build longer valid strings from shorter valid strings. Once you have that algorithm, then you can derive the entire recursive definition of Bn quite easily.
Sri K.
For a string word of length n consisting of n lowercase English characters, it can be transformed as follows: For each character of the string, If the character is 'z', it is replaced by 'ab'. Otherwise, it is replaced by its next higher character. For example, 'a' is replaced by 'b', 'b' by 'c', and 'y' by 'z'. Thus, if the initial string is "azbk", it becomes "babcl" after 1 transformation. As a fun trivia, XYZ-Company publishes a puzzle for all its employees each week. The goal for this week is to find the length of the resultant string after exactly t transformations are performed as described above. Since the answer can be large, find the answer taking modulo (10^9 + 7). Input: String: word, Number: t Output: String Example: Consider word = "abczy" and the number of transformations t = 2. In the 1st transformation, the characters are transformed as follows: a -> b b -> c c -> d z -> ab y -> z So, after the 1st transformation, word = "bcdabz" Constraints: 1 ≤ |word| ≤ 10^5 1 ≤ t ≤ 10^5 The string word consists of lowercase English characters only.
Akash M.
Unsure how to approach this problem. Algorithm to determine whether an input character string is of the form a D b D c D ... D z. Where each string a, b, ...z is of the form of the string defined in problem 3. (Thus a string is in the proper form if it consists of any number of such strings from problem 3, separated by the character 'D', e.g. ABBCBBADACADBABCBABDAABACABAA.) At each point, you may read only the next character in the string, i.e. you must process the string on a left-to-right basis. You may not use string functions.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
Watch the video solution with this free unlock.
EMAIL
PASSWORD