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...
(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...
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.
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.
Let u and v be vectors in R3. Consider the following statements.T or F (1) |u ...
Let u and v be vectors in R3. Consider the following statements.T or F (1) |u · v|  ≤  ||u|| + ||v|| (2) If  au + bv = cu + dv then  a  =  c  and  b  =  d. (3) ||u + v||2  =  ||u||2 + ||v||2 + 2(u · v) Let u, v, and w be vectors in R3. T or F. (1) u · v − ||u|| (2) (u · v) × w (3) || ( ||u|| projvu ...
2 Let u,v, and w be vectors, where u=(1,2,3,-1), v=(2,3,1,5) and w=(3,5,4,4). 2.1 Construct a basis...
2 Let u,v, and w be vectors, where u=(1,2,3,-1), v=(2,3,1,5) and w=(3,5,4,4). 2.1 Construct a basis for the vector space spanned by u, v and w. 2.2 Show that c=(1,3,2,1) is not in the vector space spanned by the above vectors u,v and w. 2.3 Show that d=(4,9,17,-11) is in the vector space spanned by the above vectors u,v and w, by expressing d as a linear combination of u,v and w.
Consider the group G = {1, −1, i, −i, j, −j, k, −k} under multiplication. Here...
Consider the group G = {1, −1, i, −i, j, −j, k, −k} under multiplication. Here i2= j2= k2= ijk = −1. determine which of the following sets is a subgroup of G. If a set is not a subgroup, give one reason why it is not. (a) {1, −1} (b) {i, −i, j, −j} (c) {1, −1, i, −i} (d) {1, i, −i, j}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT