Algorithm


Problem Name: 1023. 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 pattern so that it equals the query. You may insert each character at any position and you may not insert any characters.

 

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".

 

Constraints:

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

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public List camelMatch(String[] queries, String pattern) {
    List list = new ArrayList<>();
    for (String query : queries) {
      list.add(isMatch(query, pattern));
    }
    return list;
  }
  
  private boolean isMatch(String query, String pattern) {
    int idxQuery = 0;
    int idxPattern = 0;
    int lenQuery = query.length();
    int lenPattern = pattern.length();
    while (idxQuery  <  lenQuery && idxPattern < lenPattern) {
      char queryC = query.charAt(idxQuery);
      char patternC = pattern.charAt(idxPattern);
      if (queryC == patternC) {
        idxQuery++;
        idxPattern++;
      }
      else if (Character.isUpperCase(queryC)) {
        return false;
      }
      else {
        idxQuery++;
      }
    }
    while (idxQuery  <  lenQuery && Character.isLowerCase(query.charAt(idxQuery))) {
      idxQuery++;
    }
    return idxPattern == lenPattern && idxQuery == lenQuery;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"

Output

x
+
cmd
[true,false,true,true,false]

#2 Code Example with Javascript Programming

Code - Javascript Programming


const camelMatch = function(queries, pattern) {
    const res = []

    queries.forEach(el => {
      let tmp = chk(el, pattern)
      if(tmp) res.push(true)
      else res.push(false)
    })
    
    return res
};

function chk(str, p) {
  let pIdx = 0
  let sIdx = 0
  const sLen = str.length
  const pLen = p.length
  const Acode = ('A').charCodeAt(0)
  const Zcode = ('Z').charCodeAt(0)
  let pEnd = false

  for(let i = 0; i < pLen; i++) {
    let target = p.charAt(i)
    
    while(sIdx  <  sLen && !pEnd) {
      if(str.charCodeAt(sIdx> >= Acode && str.charCodeAt(sIdx) <= Zcode && str.charAt(sIdx) !== target) return false
      if(str.charAt(sIdx) === target) {
        if(i !== pLen - 1) {
          sIdx++
        } else {
          pEnd = true
        }
        break
      } else {
        sIdx++        
      }
    }
    if(sIdx >= sLen) return false
  }

  for(let i = sIdx + 1; pEnd && i < sLen; i++) {
    if(str.charCodeAt(i> >= Acode && str.charCodeAt(i) <= Zcode) return false
  }
  return true
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"

Output

x
+
cmd
[true,false,true,true,false]

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
        res = []
        for w in queries:
            j = 0
            for c in w:
                if j < len(pattern) and c == pattern[j]:
                    j += 1
                elif c.isupper():
                    j = len(pattern) + 1
            res.append(j == len(pattern))
        return res
Copy The Code & Try With Live Editor

Input

x
+
cmd
queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa"

Output

x
+
cmd
[true,false,true,false,false]
Advertisements

Demonstration


Previous
#1022 Leetcode Sum of Root To Leaf Binary Numbers Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#1024 Leetcode Video Stitching Solution in C, C++, Java, JavaScript, Python, C# Leetcode