1023. Camelcase Matching LeetCode Solution

In this guide, you will get 1023. Camelcase Matching LeetCode Solution with the best time and space complexity. The solution to Camelcase Matching problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Camelcase Matching solution in C++
  4. Camelcase Matching solution in Java
  5. Camelcase Matching solution in Python
  6. Additional Resources
1023. Camelcase Matching LeetCode Solution image

Problem Statement of Camelcase Matching

Given an array of strings queries and a string pattern, return a boolean array answer where answer[i] is true if queries[i] matches pattern, and false otherwise.
A query word queries[i] matches pattern if you can insert lowercase English letters into the pattern so that it equals the query. You may insert a character at any position in pattern or you may choose not to insert any characters at all.

Example 1:

Input: queries = [“FooBar”,”FooBarTest”,”FootBall”,”FrameBuffer”,”ForceFeedBack”], pattern = “FB”
Output: [true,false,true,true,false]
Explanation: “FooBar” can be generated like this “F” + “oo” + “B” + “ar”.
“FootBall” can be generated like this “F” + “oot” + “B” + “all”.
“FrameBuffer” can be generated like this “F” + “rame” + “B” + “uffer”.

Example 2:

Input: queries = [“FooBar”,”FooBarTest”,”FootBall”,”FrameBuffer”,”ForceFeedBack”], pattern = “FoBa”
Output: [true,false,true,false,false]
Explanation: “FooBar” can be generated like this “Fo” + “o” + “Ba” + “r”.
“FootBall” can be generated like this “Fo” + “ot” + “Ba” + “ll”.

Example 3:

Input: queries = [“FooBar”,”FooBarTest”,”FootBall”,”FrameBuffer”,”ForceFeedBack”], pattern = “FoBaT”
Output: [false,true,false,false,false]
Explanation: “FooBarTest” can be generated like this “Fo” + “o” + “Ba” + “r” + “T” + “est”.

See also  2543. Check if Point Is Reachable LeetCode Solution

Constraints:

1 <= pattern.length, queries.length <= 100
1 <= queries[i].length <= 100
queries[i] and pattern consist of English letters.

Complexity Analysis

  • Time Complexity: O(q \cdot |\texttt{pattern}|)
  • Space Complexity: O(q)

1023. Camelcase Matching LeetCode Solution in C++

class Solution {
 public:
  vector<bool> camelMatch(vector<string>& queries, string pattern) {
    vector<bool> ans;
    for (const string& query : queries)
      ans.push_back(isMatch(query, pattern));
    return ans;
  }

 private:
  bool isMatch(const string& query, const string& pattern) {
    int j = 0;
    for (const char c : query)
      if (j < pattern.length() && c == pattern[j])
        ++j;
      else if (isupper(c))
        return false;
    return j == pattern.length();
  }
};
/* code provided by PROGIEZ */

1023. Camelcase Matching LeetCode Solution in Java

class Solution {
  public List<Boolean> camelMatch(String[] queries, String pattern) {
    List<Boolean> ans = new ArrayList<>();
    for (final String q : queries)
      ans.add(isMatch(q, pattern));
    return ans;
  }

  private boolean isMatch(final String query, final String pattern) {
    int j = 0;
    for (final char c : query.toCharArray())
      if (j < pattern.length() && c == pattern.charAt(j))
        ++j;
      else if (Character.isUpperCase(c))
        return false;
    return j == pattern.length();
  }
}
// code provided by PROGIEZ

1023. Camelcase Matching LeetCode Solution in Python

class Solution:
  def camelMatch(self, queries: list[str], pattern: str) -> list[bool]:
    def isMatch(query: str) -> bool:
      j = 0
      for c in query:
        if j < len(pattern) and c == pattern[j]:
          j += 1
        elif c.isupper():
          return False
      return j == len(pattern)

    return [isMatch(query) for query in queries]
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.