S1Q2 · Case Insensitive Strict Prefix¶
⚡ Quick Reference
Function: is_case_insensitive_strict_prefix(a: str, b: str) -> bool
Core idea: lowercase both strings, check if one starts with the other, and ensure they are not equal.
def is_case_insensitive_strict_prefix(a: str, b: str) -> bool:
a, b = a.lower(), b.lower()
if a == b:
return False
return b.startswith(a) or a.startswith(b)
Key rules:
- Case insensitive → lowercase both first
- "Strict" means not equal - reject if a == b after lowercasing
- Either string can be the prefix of the other
Template Code¶
def is_case_insensitive_strict_prefix(a: str, b: str) -> bool:
'''
Return True if one string is a strict prefix of the other,
case insensitively. Returns False if strings are equal or
neither is a prefix of the other.
Examples:
>>> is_case_insensitive_strict_prefix("app", "apple")
True
>>> is_case_insensitive_strict_prefix("hello", "Hello")
False
>>> is_case_insensitive_strict_prefix("cat", "dog")
False
Args:
a (str): First string (alphabets only)
b (str): Second string (alphabets only)
Returns:
bool: True if one is a strict prefix of the other
'''
...
Problem Statement¶
Problem
Write a function is_case_insensitive_strict_prefix(a, b) that returns True if one string is a prefix of the other (case-insensitively), but they are not equal.
Examples:
a="app", b="apple"
True
a="App", b="APPLE"
True
a="hello", b="Hello"
False
a="cat", b="dog"
False
Tracing all examples¶
a |
b |
a.lower() |
b.lower() |
Equal? | Prefix? | Result |
|---|---|---|---|---|---|---|
"app" |
"apple" |
"app" |
"apple" |
❌ | "apple".startswith("app") ✅ |
True |
"App" |
"APPLE" |
"app" |
"apple" |
❌ | "apple".startswith("app") ✅ |
True |
"hello" |
"Hello" |
"hello" |
"hello" |
✅ | - | False |
"cat" |
"dog" |
"cat" |
"dog" |
❌ | neither starts with the other ❌ | False |
"pre" |
"prefix" |
"pre" |
"prefix" |
❌ | "prefix".startswith("pre") ✅ |
True |
Why check equality before prefix?
"hello" and "Hello" - after lowercasing both are "hello". "hello".startswith("hello") is True, but since they're equal it's not a strict prefix. Checking equality first and returning False handles this correctly.
Solution approaches¶
def is_case_insensitive_strict_prefix(a: str, b: str) -> bool:
a_lower = a.lower()
b_lower = b.lower()
# Not strict if they're equal
if a_lower == b_lower:
return False
# Check if either is a prefix of the other
a_is_prefix_of_b = b_lower.startswith(a_lower)
b_is_prefix_of_a = a_lower.startswith(b_lower)
return a_is_prefix_of_b or b_is_prefix_of_a
Key takeaways¶
Lowercase both before any comparison
Always normalise case upfront. Converting both to lowercase once at the start avoids mixing cased and uncased comparisons throughout the function.
"Strict" means not equal
A prefix is "strict" if it's shorter than the string it prefixes. Checking a != b ensures the two strings don't become identical after case normalisation.
Either can be the prefix
The problem says "one is a prefix of the other" - not specifically which one. Always check both directions: b.startswith(a) or a.startswith(b).