public static boolean samePattern(String s1, String s2){ //base-case: s1 has reach the end if(s1.length() == 0){ if(s2.length() == 0 || (s2.charAt(0) == '*' && s2.length() == 1)) //check if s2 has also reached the end OR edge-case: joker in the end of s2 return true; //else return false; } //else: s1 had not reach the end if(s2.length() == 0) return false; //s2 has reached the end //encounters a joker if(s2.charAt(0) == '*' ){ if(samePattern(s1,s2.substring(1))) //backtrack return true; return samePattern(s1.substring(1), s2); //you have just activated the ability to GO BACK IN TIME. the wonders of recursion } if(s1.charAt(0) == s2.charAt(0)) return samePattern(s1.substring(1), s2.substring(1)); return false; }