Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to count number of operations needed to make string as concatenation of same string twice in Python
Suppose we have a lowercase string s. We need to find the minimum number of operations (delete, insert, or update) required to make s equal to the concatenation of some string t with itself (s = t + t).
So, if the input is like s = "pqrxqsr", then the output will be 2. We can update the "x" with "p" and delete "s", making s = "pqrpqr", which is t + t where t = "pqr".
Algorithm
We solve this by trying all possible split points and using edit distance ?
- For each position i, split the string into two parts: s[:i] and s[i:]
- Calculate the edit distance between these two parts
- The minimum edit distance across all splits gives us the answer
Edit Distance Function
The edit distance (Levenshtein distance) calculates minimum operations to transform one string into another ?
def edit_distance(s1, s2):
m, n = len(s1), len(s2)
# Initialize DP array
cur = list(range(n + 1))
for i in range(m):
prev, cur = cur, [i + 1] + [0] * n
for j in range(n):
if s1[i] == s2[j]:
cur[j + 1] = prev[j] # No operation needed
else:
cur[j + 1] = min(cur[j], prev[j], prev[j + 1]) + 1
return cur[n]
# Test the function
print(edit_distance("pqr", "pqr")) # Same strings
print(edit_distance("pqr", "xqsr")) # Different strings
0 2
Complete Solution
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):
if s1[i] == s2[j]:
cur[j + 1] = prev[j]
else:
cur[j + 1] = min(cur[j], prev[j], prev[j + 1]) + 1
return cur[n]
res = len(s)
# Try all possible split points
for i in range(len(s) + 1):
left_part = s[:i]
right_part = s[i:]
res = min(edit_distance(left_part, right_part), res)
return res
# Test with the example
s = "pqrxqsr"
result = solve(s)
print(f"Minimum operations needed: {result}")
# Test with more examples
test_cases = ["abab", "aaaa", "abc", "abcabc"]
for test in test_cases:
print(f"String: '{test}' ? Operations: {solve(test)}")
Minimum operations needed: 2 String: 'abab' ? Operations: 0 String: 'aaaa' ? Operations: 0 String: 'abc' ? Operations: 1 String: 'abcabc' ? Operations: 0
How It Works
For the example "pqrxqsr" ?
- Split at position 3: "pqr" and "xqsr"
- Edit distance between "pqr" and "xqsr" is 2 (change x?p, remove s)
- This gives us "pqrpqr" = "pqr" + "pqr"
Time Complexity
The algorithm has O(n³) time complexity, where n is the length of the string. We try n+1 split points, and each edit distance calculation takes O(n²) time.
Conclusion
This solution finds the minimum edit distance across all possible ways to split the string into two equal parts. The edit distance represents the minimum operations needed to make the string a concatenation of identical substrings.
