Friday, December 1, 2023

Substring in another string (Lesson in Algorithms)

Substring in another string

Problem

Detect if a shorter string is part of a longer string.

Shorter string is the substring.

Longer string is the superstring.

Example

Shorter string is ‘eng’.

Longer string is ‘opengenus’.

‘eng’ is a substring in ‘opengenus’.

‘123’ is not a substring in ‘opengenus’.

Code in Python

# s1 is the superstring
# s2 is the substring

def solution(s1, s2):
    for i in range(len(s1)-len(s2)+1):
        if s1[i:i+len(s2)] == s2:
            return 'Match Found'  

Testing

s1 = 'opengenus'

# Test case: no match found

s2 = '123'

print(s2, solution(s1, s2))

# Test case: single character

s2 = 'p'

print(s2, solution(s1, s2))

# Test case: match substring at the beginning (at index 0)

s2 = 'ope'

print(s2, solution(s1, s2))

# Test case: match substring somewhere in the middle

s2 = 'eng'

print(s2, solution(s1, s2))

# Test case: match substring at the end

s2 = 'nus'

print(s2, solution(s1, s2))

# Test case: match both the strings completely

s2 = 'opengenus'

print(s2, solution(s1, s2))

Output

123 None
p Match Found
ope Match Found
eng Match Found
nus Match Found
opengenus Match Found

No comments:

Post a Comment