public class PatternMatch {
public static boolean matchesPattern(String pattern, String sentence) {
String[] words = sentence.split("\\s+");
// Length mismatch → automatic failure
if (pattern.length() != words.length) {
return false;
}
// Compare each pattern character to the first letter of each word
for (int i = 0; i < pattern.length(); i++) {
char p = Character.toLowerCase(pattern.charAt(i));
char w = Character.toLowerCase(words[i].charAt(0));
if (p != w) {
return false;
}
}
return true;
}
public static void main(String[] args) {
String pattern = "jpcrg";
String sentence = "java python c rust go";
if (matchesPattern(pattern, sentence)) {
System.out.println("Pattern matches!");
} else {
System.out.println("Pattern does NOT match.");
}
}
}
/*
run:
Pattern matches!
*/