LeetCode Solutions

1023. Camelcase Matching

Time: $O(q \cdot |\texttt{pattern}|)$

Space: $O(q)$

			

class Solution {
 public:
  vector<bool> camelMatch(vector<string>& queries, string pattern) {
    vector<bool> ans;

    for (const string& q : queries)
      ans.push_back(isMatch(q, pattern));

    return ans;
  }

 private:
  bool isMatch(const string& q, const string& pattern) {
    int j = 0;

    for (int i = 0; i < q.length(); ++i)
      if (j < pattern.length() && q[i] == pattern[j])
        ++j;
      else if (isupper(q[i]))
        return false;

    return j == pattern.length();
  }
};
			

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 q, final String pattern) {
    int j = 0;

    for (int i = 0; i < q.length(); ++i)
      if (j < pattern.length() && q.charAt(i) == pattern.charAt(j))
        ++j;
      else if (Character.isUpperCase(q.charAt(i)))
        return false;

    return j == pattern.length();
  }
}
			

class Solution:
  def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
    def isMatch(q: str) -> bool:
      j = 0

      for i, c in enumerate(q):
        if j < len(pattern) and c == pattern[j]:
          j += 1
        elif c.isupper():
          return False

      return j == len(pattern)

    return [isMatch(q) for q in queries]