0
0
DSA Pythonprogramming~20 mins

String Pattern Matching Naive in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Naive Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Naive Pattern Matching Positions
What is the output of the following code that finds all starting indices where the pattern occurs in the text using naive pattern matching?
DSA Python
def naive_pattern_search(text, pattern):
    positions = []
    n = len(text)
    m = len(pattern)
    for i in range(n - m + 1):
        match = True
        for j in range(m):
            if text[i + j] != pattern[j]:
                match = False
                break
        if match:
            positions.append(i)
    return positions

text = "abracadabra"
pattern = "abra"
print(naive_pattern_search(text, pattern))
A[7]
B[0, 3, 7]
C[3, 7]
D[0, 7]
Attempts:
2 left
💡 Hint
Check where the pattern "abra" appears exactly in the text "abracadabra".
Predict Output
intermediate
2:00remaining
Output when pattern not found
What is the output of this naive pattern matching code when the pattern does not exist in the text?
DSA Python
def naive_pattern_search(text, pattern):
    positions = []
    n = len(text)
    m = len(pattern)
    for i in range(n - m + 1):
        match = True
        for j in range(m):
            if text[i + j] != pattern[j]:
                match = False
                break
        if match:
            positions.append(i)
    return positions

text = "hello world"
pattern = "bye"
print(naive_pattern_search(text, pattern))
A[]
B[0, 6]
C[6]
D[0]
Attempts:
2 left
💡 Hint
If the pattern is not found, the list of positions should be empty.
🔧 Debug
advanced
2:00remaining
Identify the error in naive pattern matching code
What error does this code raise when run?
DSA Python
def naive_pattern_search(text, pattern):
    positions = []
    n = len(text)
    m = len(pattern)
    for i in range(n - m + 2):
        match = True
        for j in range(m):
            if text[i + j] != pattern[j]:
                match = False
                break
        if match:
            positions.append(i)
    return positions

text = "abcabcabc"
pattern = "abc"
print(naive_pattern_search(text, pattern))
A[0, 3, 6]
BIndexError
C[]
DSyntaxError
Attempts:
2 left
💡 Hint
Check the loop range for possible off-by-one errors.
🧠 Conceptual
advanced
2:00remaining
Time complexity of naive pattern matching
What is the worst-case time complexity of the naive pattern matching algorithm for a text of length n and pattern of length m?
AO(n * m)
BO(m^2)
CO(n^2)
DO(n + m)
Attempts:
2 left
💡 Hint
Consider how many comparisons are done in the worst case.
🚀 Application
expert
2:00remaining
Number of matches found by naive pattern matching
Given the text "aaaaaa" and pattern "aaa", how many matches will the naive pattern matching algorithm find?
A2
B3
C4
D5
Attempts:
2 left
💡 Hint
Count all overlapping occurrences of the pattern in the text.