LeetCode 1023: Camelcase Matching Solution
Master LeetCode problem 1023 (Camelcase Matching), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1023. Camelcase Matching
Problem Explanation
Explanation
To solve this problem:
- Iterate through each query in the queries array.
- Initialize two pointers, one for the query string and one for the pattern string.
- Check if the characters match between the query and pattern:
- If they match, move both pointers to the next character.
- If they don't match:
- If the character in the query is uppercase, return false.
- If the character in the query is lowercase, move only the query pointer.
- Check if the pattern pointer reaches the end of the pattern string. If not, return false.
- If all characters in the query are matched with the pattern, return true.
Time complexity: O(n * m) where n is the number of queries and m is the maximum length of a query string. Space complexity: O(1) as we are using constant extra space.
Solution Code
class Solution {
public List<Boolean> camelMatch(String[] queries, String pattern) {
List<Boolean> result = new ArrayList<>();
for (String query : queries) {
result.add(isMatch(query, pattern));
}
return result;
}
private boolean isMatch(String query, String pattern) {
int i = 0, j = 0;
while (i < query.length() && j < pattern.length()) {
if (query.charAt(i) == pattern.charAt(j)) {
i++;
j++;
} else {
if (Character.isUpperCase(query.charAt(i))) {
return false;
}
i++;
}
}
while (i < query.length()) {
if (Character.isUpperCase(query.charAt(i))) {
return false;
}
i++;
}
return j == pattern.length();
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1023 (Camelcase Matching)?
This page provides optimized solutions for LeetCode problem 1023 (Camelcase Matching) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1023 (Camelcase Matching)?
The time complexity for LeetCode 1023 (Camelcase Matching) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1023 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1023 in Java, C++, or Python.