function matchesPattern(string $pattern, string $sentence): bool
{
// Split sentence into words (handles multiple spaces automatically)
$words = preg_split('/\s+/', trim($sentence));
// Length mismatch → automatic failure
if (strlen($pattern) !== count($words)) {
return false;
}
// Compare each pattern character to the first letter of each word
for ($i = 0; $i < strlen($pattern); $i++) {
$p = strtolower($pattern[$i]);
$w = strtolower($words[$i][0]);
if ($p !== $w) {
return false;
}
}
return true;
}
$pattern = "jpcrg";
$sentence = "java python c rust go";
if (matchesPattern($pattern, $sentence)) {
echo "Pattern matches!\n";
} else {
echo "Pattern does NOT match.\n";
}
/*
run:
Pattern matches!
*/