π3. Palindrome Sentence π§
The problem can be found at the following link: Problem Link
π‘ Problem Description:
Given a single string s, check if it is a palindrome sentence or not. A palindrome sentence is a sequence of characters that reads the same backward as forward after:
Converting all uppercase letters to lowercase.
Removing all non-alphanumeric characters.
π Example Walkthrough:
Input:
s = "Too hot to hoot"Output:
trueExplanation: If we remove all non-alphanumeric characters and convert all uppercase letters to lowercase, the string becomes βtoohottohootβ, which is a palindrome.
Input:
s = "Abc 012..## 10cbA"Output:
trueExplanation: After processing the string, it becomes "abc01210cba", which is a palindrome.
Input:
Output:
Explanation: The processed string becomes "abcdef01asdf", which is not a palindrome.
Constraints:
1 β€ s.size() β€ 10^6
π― My Approach:
Step-by-Step:
Pointer Initialization: Start by initializing two pointers,
iat the start of the string andjat the end of the string.Skip Non-Alphanumeric Characters: Move the pointers
iandjtowards each other. Ifs[i]is not alphanumeric, incrementi. Similarly, ifs[j]is not alphanumeric, decrementj.Check for Matching Characters: Once valid characters are found at both pointers, convert them to lowercase and compare:
If they are not equal, return
falseas it is not a palindrome.Otherwise, move both pointers towards the center (increment
iand decrementj).
End Condition: If the entire string is processed without mismatches, return
true.
π Time and Auxiliary Space Complexity:
Time Complexity: The algorithm performs a linear scan of the string with two pointers moving from the ends towards the center, which takes O(n) time, where
nis the length of the string.Auxiliary Space Complexity: The space complexity is O(1) as we are only using a few integer variables and no additional space proportional to the input size.
π Solution Code
Code (Cpp)
Code (Java)
Code (Python)
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated