Question

In: Computer Science

When transforming word u[1 : i] to v[1 : j], in addition to adding, removing, or...

When transforming word u[1 : i] to v[1 : j], in addition to adding, removing, or changing a letter, suppose we also allow for the swapping of adjacent letters. For example, transforming u = tera to v = tear requires a single edit that swaps the ‘r’ with the ‘a’. Provide the recurrence for d(i, j) that occurs when one performs this edit.

Solutions

Expert Solution

Given two strings s1 and s2, the task is to find the minimum number of steps required to convert s1 into s2. The only operation allowed is to swap adjacent elements in the first string. Every swap is counted as a single step.

Examples:

Input: s1 = “abcd”, s2 = “cdab”
Output: 4
Swap 2nd and 3rd element, abcd => acbd
Swap 1st and 2nd element, acbd => cabd
Swap 3rd and 4th element, cabd => cadb
Swap 2nd and 3rd element, cadb => cdab
Minimum 4 swaps are required.

Input: s1 = “abcfdegji”, s2 = “fjiacbdge”
Output:17
Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Use two pointers i and j for first and second strings respectively. Initialise i and j to 0.
Iterate over the first string and find the position j such that s1[j] = s2[i] by incrementing the value to j. Keep on swapping the adjacent elements j and j – 1 and decrement j until it is greater than i.
Now the ith element of the first string is equal to the second string, hence increment the value of i.
This technique will give the minimum number of steps as there are zero unnecessary swaps.


Related Solutions

Suppose u, and v are vectors in R m, such that ∥u∥ = 1, ∥v∥ =...
Suppose u, and v are vectors in R m, such that ∥u∥ = 1, ∥v∥ = 4, ∥u + v∥ = 5. Find the inner product 〈u, v〉. Suppose {a1, · · · ak} are orthonormal vectors in R m. Show that {a1, · · · ak} is a linearly independent set.
(1) Suppose that V is a vector space and that S = {u,v} is a set...
(1) Suppose that V is a vector space and that S = {u,v} is a set of two vectors in V. Let w=u+v, let x=u+2v, and letT ={w,x} (so thatT is another set of two vectors in V ). (a) Show that if S is linearly independent in V then T is also independent. (Hint: suppose that there is a linear combination of elements of T that is equal to 0. Then ....). (b) Show that if S generates V...
3. Let S3 act on the set A={(i,j) : 1≤i,j≤3} by σ((i, j)) = (σ(i), σ(j))....
3. Let S3 act on the set A={(i,j) : 1≤i,j≤3} by σ((i, j)) = (σ(i), σ(j)). (a) Describe the orbits of this action. (b) Show this is a faithful action, i.e. that the permutation represen- tation φ:S3 →SA =S9 (c) For each σ ∈ S3, find the cycle decomposition of φ(σ) in S9.
(2) Jurian has preferences u J (x J 1 ; x J 2 ) = (x...
(2) Jurian has preferences u J (x J 1 ; x J 2 ) = (x J 1 ) 4 (x J 2 ) 2 . (a) Find his uncompensated demand for the two goods as a function of prices and her wealth w. (b) Find his compensated demand as a function of prices and her utility u. (c) Now suppose that Jurian interacts with Katinka in an exchange economy, where Katinka’s preferences are u K(x K 1 ; x...
def longest(string): start=0;end=1;i=0; while i<len(string): j=i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: #update if current...
def longest(string): start=0;end=1;i=0; while i<len(string): j=i+1 while j<len(string) and string[j]>string[j-1]: j+=1 if end-start<j-i: #update if current string has greater length than #max start and end end=j start=i i=j; avg=0 for i in string[start:end]: avg+=int(i) print('The longest string in ascending order is',string[start:end]) print('Teh average is',avg/(end-start)) s=input('Enter a string') longest(s) i need a definition and explanation of this code that how it works?
Let U, V be iid Unif(0, 1) random variables, and set M = max(U,V) and N...
Let U, V be iid Unif(0, 1) random variables, and set M = max(U,V) and N = min (U,V) (a) Find the conditional density of N given M = a for any value of a ∈ (0, 1). (b) Find Cov(M, N).
For the following exercises, calculate u ⋅ v. Given v = 〈−3, 4〉 draw v, 2v, and 1/2 v.
For the following exercises, calculate u ⋅ v.Given v = 〈−3, 4⟩ draw v, 2v, and 1/2 v.
If z=x2y- 9xy2 and x=u2+euw, y=v2+2uv, when u=1, v=-1, w=0, then Zu=
If z=x2y- 9xy2 and x=u2+euw, y=v2+2uv, when u=1, v=-1, w=0, then Zu=
Find a unit vector that is orthogonal to both u = i − 4j + k and v = 2i + 3j.
Find a unit vector that is orthogonal to both u = i − 4j + k and v = 2i + 3j.  
Question 1 a) Prove that if u and v are distinct vertices of a graph G,...
Question 1 a) Prove that if u and v are distinct vertices of a graph G, there exists a walk from u to v if and only if there exists a path (a walk with distinct vertices) from u to v. b) Prove that a graph is bipartite if and only if it contains no cycles of odd length. Please write legibly with step by step details. Many thanks!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT