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
Check if a string can be repeated to make another string in Python
When working with strings in Python, we sometimes need to check if one string can be repeated multiple times to form another string. This is useful in pattern matching and string manipulation tasks.
Given two strings s and t, we need to find how many times string s must be concatenated to generate string t. If it's impossible to generate t using s, we return -1.
Algorithm
To solve this problem, we follow these steps ?
- Check if the length of
tis divisible by the length ofs - If not divisible, return -1 (impossible to form
tfroms) - Calculate how many times
sshould be repeated - Create the repeated string and compare with
t - Return the count if they match, otherwise return -1
Example
Let's implement this solution with a complete example ?
def solve(s, t):
# Check if t can be formed by repeating s
if len(t) % len(s) != 0:
return -1
# Calculate number of repetitions needed
cnt = len(t) // len(s)
# Create repeated string
repeated_s = s * cnt
# Check if the repeated string matches t
if repeated_s == t:
return cnt
return -1
# Test the function
s = "tom"
t = "tomtomtom"
result = solve(s, t)
print(f"String '{s}' repeated {result} times makes '{t}'")
# Test with another example
s2 = "abc"
t2 = "abcabcabc"
result2 = solve(s2, t2)
print(f"String '{s2}' repeated {result2} times makes '{t2}'")
# Test impossible case
s3 = "ab"
t3 = "aba"
result3 = solve(s3, t3)
print(f"Result for '{s3}' and '{t3}': {result3}")
String 'tom' repeated 3 times makes 'tomtomtom' String 'abc' repeated 3 times makes 'abcabcabc' Result for 'ab' and 'aba': -1
How It Works
The solution works by first checking if the target string length is divisible by the source string length. If not, it's mathematically impossible for the source string to form the target string through repetition.
When the lengths are compatible, we calculate the required number of repetitions and create the repeated string using Python's string multiplication operator (*). Finally, we compare the result with the target string.
Edge Cases
def solve(s, t):
if len(t) % len(s) != 0:
return -1
cnt = len(t) // len(s)
repeated_s = s * cnt
if repeated_s == t:
return cnt
return -1
# Edge case: Empty strings
print("Empty target:", solve("a", ""))
# Edge case: Same strings
print("Same strings:", solve("hello", "hello"))
# Edge case: Single character
print("Single character:", solve("x", "xxxx"))
# Edge case: No match
print("No match:", solve("abc", "abcabcab"))
Empty target: 0 Same strings: 1 Single character: 4 No match: -1
Conclusion
This approach efficiently determines if one string can be repeated to form another by checking length divisibility and string equality. The time complexity is O(n) where n is the length of the target string.
