Check Whether One String Contains Another by Direct Comparison
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Implement direct substring containment by checking each possible starting position. Handle empty patterns and boundary cases, and explain the repeated-prefix inputs that produce quadratic work.
Constraints
- text and pattern are strings of characters; the source gives no length bound or encoding restriction.
- A match must use consecutive characters in the same order.
- Comparison is case-sensitive and does not normalize characters.
- The empty pattern returns true for every text.
Examples
Input: ('', '')
Expected Output: True
Explanation: The empty pattern occurs in the empty text.
Input: ('', 'a')
Expected Output: False
Explanation: A nonempty pattern cannot fit in empty text.
Hints
- Consider what the empty pattern means before checking possible positions.
- A longer pattern cannot fit into a shorter text.