fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static List<Boolean> camelMatch(String[] queries, String pattern) {
  11. List<Boolean> result = new ArrayList<>();
  12.  
  13. char[] patternArr = pattern.toCharArray();
  14.  
  15. // Start for each string in the query array
  16. for (String query : queries) {
  17.  
  18. // Match it with the pattern
  19. boolean isMatch = match(query.toCharArray(), patternArr);
  20.  
  21. // Pass the result
  22. result.add(isMatch);
  23. }
  24.  
  25. return result;
  26. }
  27.  
  28. private static boolean match(char[] queryArr, char[] patternArr) {
  29.  
  30. int j = 0; // pattern pointer
  31.  
  32. // i is the query pointer
  33. for (int i = 0; i < queryArr.length; i++) {
  34.  
  35. // If char[i] == char[j], we need to advance both the pointers.
  36. if (j < patternArr.length && queryArr[i] == patternArr[j]) {
  37. j++;
  38. } else if (queryArr[i] >= 'A' && queryArr[i] <= 'Z') {
  39.  
  40. // If the query character is a uppercase letter, then return false.
  41. return false;
  42. }
  43. }
  44.  
  45. // Just verify that pattern pointer reaches its length limit
  46. return j == patternArr.length;
  47. }
  48.  
  49. public static void main (String[] args) throws java.lang.Exception
  50. {
  51. String[] queries = {"StudyAlgorithms","StudyAlgorithmsTest","StanAlluring","SimulationAlteration","StopStayAlive"};
  52. String pattern = "StAl";
  53.  
  54. List<Boolean> results = camelMatch(queries, pattern);
  55. for(Boolean result : results) {
  56. System.out.println(result);
  57. }
  58. }
  59. }
Success #stdin #stdout 0.04s 2184192KB
stdin
Standard input is empty
stdout
true
false
true
true
false