Similar Problems

Similar Problems not available

Market Analysis I - Leetcode Solution

Companies:

LeetCode:  Market Analysis I Leetcode Solution

Difficulty: Medium

Topics: database  

Problem Statement:

You are given a string s consisting of lowercase English letters, digits, and underscores '_'.

You want to determine whether s is a palindrome considering only alphanumeric characters and ignoring cases.

To achieve this, you can convert s to lowercase, remove all non-alphanumeric characters, and then check if it is a palindrome.

The string s is considered palindrome if it reads the same backward as forwards.

Return true if s is a palindrome, otherwise, return false.

Constraints:

1 <= s.length <= 2 * 10^5 s consists only of printable ASCII characters.

Example 1:

Input: s = "A man, a plan, a canal: Panama" Output: true Explanation: "amanaplanacanalpanama" is a palindrome.

Example 2:

Input: s = "race a car" Output: false Explanation: "raceacar" is not a palindrome.

Solution:

To solve this problem, we need to remove all non-alphanumeric characters and convert the remaining characters to lowercase. After this, we can check if the resulting string is a palindrome or not.

We can use two pointers, one starting from the beginning of the string and the other starting from the end of the string. We can compare the characters at these pointers and move them towards each other until they meet or cross each other.

We can use two loops for this. The outer loop iterates over the characters of the string s and the inner loop checks if each character is alphanumeric or not. If it is alphanumeric, we add it to a new string called clean_str after converting it to lowercase.

Then, we use the two pointers to compare the characters in clean_str. If they are equal, we continue to compare the next pair of characters, otherwise, we return false as the string is not a palindrome.

If we are able to compare all the characters and none of them mismatches, we return true as the string is a palindrome.

Let's see the implementation of the above approach in Python:

def isPalindrome(s: str) -> bool:

clean_str = ""

# Removing non-alphanumeric characters and converting to lowercase
for c in s:
    if c.isalnum():
        clean_str += c.lower()

# Comparing characters using two pointers
left, right = 0, len(clean_str)-1
while left < right:
    if clean_str[left] != clean_str[right]:
        return False
    left += 1
    right -= 1

return True

Time Complexity:

The time complexity of this approach is O(n), where n is the length of the input string s. The outer loop iterates over each character of the string once, hence O(n) time complexity. The inner loop checks if each character is alphanumeric or not, which takes O(1) time. The two pointer method also takes O(n) time in the worst case, where the string is not a palindrome and we need to compare all the characters.

Space Complexity:

The space complexity of this approach is O(n), where n is the length of the input string s. The clean_str string can have a maximum length of n, hence O(n) space complexity.

Conclusion:

In this problem, we learned how to remove non-alphanumeric characters and convert the remaining characters to lowercase. We also used the two pointer method to check if the resulting string is a palindrome or not. This is a very useful technique in solving string-related problems.

Market Analysis I Solution Code

1