import Foundation
func matchesPattern(_ pattern: String, _ sentence: String) -> Bool {
let words = sentence
.split(whereSeparator: { $0.isWhitespace })
.map(String.init)
// Length mismatch → automatic failure
guard pattern.count == words.count else {
return false
}
// Compare each pattern character to the first letter of each word
for (p, w) in zip(pattern.lowercased(), words) {
guard let first = w.first?.lowercased().first else {
return false
}
if p != first {
return false
}
}
return true
}
let pattern = "jpcrg"
let sentence = "java python c rust go"
if matchesPattern(pattern, sentence) {
print("Pattern matches!")
} else {
print("Pattern does NOT match.")
}
/*
run:
Pattern matches!
*/