Skip to content

1023. Camelcase Matching

  • Time: $O(q \cdot |\texttt{pattern}|)$
  • Space: $O(q)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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();
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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();
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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]