- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a lowercase string s. Now consider an operation, where we can delete, insert or update any character in s. We have to count minimum number of operations required to make s = (t concatenate t) for any string t.

So, if the input is like s = "pqrxqsr", then the output will be 2, because, we can update the "x" with "p" and delete "s", then s is "pqrpqr", this is s = t concatenate t, for t = "pqr".

To solve this, we will follow these steps −

- Define a function edit_distance(). This will take s1, s2
- m := size of s1
- n := size of s2
- cur := a new list from range 0 to n
- for i in range 0 to m - 1, do
- prev := cur
- cur := a list with (i + 1) and then n number of 0s
- for j in range 0 to n - 1, do
- cur[j + 1] := prev[j] if s1[i] and s2[j] are same otherwise (minimum of cur[j], prev[j], prev[j + 1]) + 1

- return cur[n]
- From the main method, do the following −
- res := size of s
- for i in range 0 to size of s - 1, do
- res := minimum of edit_distance(substring of s from index 0 to i - 1, substring of s from index i to end) and res

- return res

Let us see the following implementation to get better understanding −

def solve(s): def edit_distance(s1, s2): m, n = len(s1), len(s2) cur = list(range(n + 1)) for i in range(m): prev, cur = cur, [i + 1] + [0] * n for j in range(n): cur[j + 1] = (prev[j] if s1[i] == s2[j] else min(cur[j], prev[j], prev[j + 1]) + 1) return cur[n] res = len(s) for i in range(len(s)): res = min(edit_distance(s[:i], s[i:]), res) return res s = "pqrxqsr" print(solve(s))

"pqrxqsr"

None

- Related Questions & Answers
- Program to check minimum number of characters needed to make string palindrome in Python
- Program to find minimum number of operations to make string sorted in Python
- Program to find number of operations needed to make pairs from first and last side are with same sum in Python
- Program to find minimum number of operations required to make one string substring of other in Python
- Minimum number of Appends needed to make a string palindrome in C++
- Program to find minimum number of subsequence whose concatenation is same as target in python
- Program to count minimum number of operations to flip columns to make target in Python
- Program to count number of operations required to all cells into same color in Python
- Program to count number of operations required to convert all values into same in Python?
- Program to count minimum number of operations required to make numbers non coprime in Python?
- Python program to count number of substring present in string
- Program to find number of coins needed to make the changes in Python
- Program to count number of unique palindromes we can make using string characters in Python
- Python Program to Count Number of Lowercase Characters in a String
- Count of operations to make a binary string “ab” free in C++

Advertisements