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 given string can be split into four distinct strings in Python
Given a string, we need to check whether it can be split into four non-empty and distinct substrings. This problem involves finding all possible ways to partition the string into exactly four parts and verifying that all parts are unique.
For example, if the input string is "helloworld", one valid split would be ["hel", "lo", "wor", "ld"] since all four substrings are distinct.
Algorithm Approach
The solution uses a brute force approach with three nested loops to generate all possible splits ?
- If the string length is ? 10, return True (optimization: with 10+ characters, we can always find 4 distinct substrings)
- Use three loop variables (i, j, k) to create four split points
- Extract four substrings and check if they are all distinct
- Return True if any valid split is found, otherwise False
Implementation
def solve(s):
if len(s) >= 10:
return True
for i in range(1, len(s)):
for j in range(i + 1, len(s)):
for k in range(j + 1, len(s)):
sub1 = s[0:i]
sub2 = s[i:j]
sub3 = s[j:k]
sub4 = s[k:]
# Check if all four substrings are distinct
substrings = {sub1, sub2, sub3, sub4}
if len(substrings) == 4:
return True
return False
# Test the function
s = "helloworld"
print(solve(s))
True
How It Works
The algorithm creates three split points at positions i, j, and k, dividing the string into four parts ?
- sub1: s[0:i] - from start to position i
- sub2: s[i:j] - from position i to j
- sub3: s[j:k] - from position j to k
- sub4: s[k:] - from position k to end
Using a set to check distinctness is more efficient than multiple inequality comparisons.
Example with Different Input
def solve(s):
if len(s) >= 10:
return True
for i in range(1, len(s)):
for j in range(i + 1, len(s)):
for k in range(j + 1, len(s)):
sub1 = s[0:i]
sub2 = s[i:j]
sub3 = s[j:k]
sub4 = s[k:]
substrings = {sub1, sub2, sub3, sub4}
if len(substrings) == 4:
print(f"Valid split: {[sub1, sub2, sub3, sub4]}")
return True
return False
# Test with different strings
test_cases = ["helloworld", "abcd", "aaaa", "python"]
for test in test_cases:
result = solve(test)
print(f"'{test}' can be split: {result}")
Valid split: ['hel', 'lo', 'wor', 'ld'] 'helloworld' can be split: True Valid split: ['a', 'b', 'c', 'd'] 'abcd' can be split: True 'aaaa' can be split: False Valid split: ['p', 'y', 't', 'hon'] 'python' can be split: True
Time Complexity
The time complexity is O(n³) where n is the length of the string, due to the three nested loops. The space complexity is O(1) as we only store four substrings at a time.
Conclusion
This solution efficiently checks all possible ways to split a string into four distinct parts using nested loops. The optimization for strings with 10+ characters significantly improves performance for longer inputs.
