I need to write a piece of code which accepts an input string parameter and determine if exactly 3 question marks exist between every pair of numbers that add up to 10. If so, return true, otherwise return false.Some examples test cases are below
"arrb6???4xxbl5???eee5" => true
"acc?7??sss?3rr1??????5" => true
"5??aaaaaaaaaaaaaaaaaaa?5?5" => false
"9???1???9???1???9" => true
"aa6?9" => false
I already tried to implement it in Java as below but the result is not as expected
public static String QuestionsMarks(String str) {
str = str.replaceAll("[a-z]+","");
Pattern pattern = Pattern.compile("([0-9])([?])([?])([0-9])");
Pattern pattern01 = Pattern.compile("([0-9])([?])([?])([0-9])");
Matcher matcher01 = pattern01.matcher(str);
Pattern pattern02 = Pattern.compile("([0-9])([0-9])");
Matcher matcher02 = pattern02.matcher(str);
Matcher matcher = pattern.matcher(str);
if (matcher01.find() || matcher02.find()) {
return "false";
} else if (matcher.find()) {
return "true";
}
return "false";
}