<?php
function punyaHuruf(string $kataPertama, string $kataKedua): bool
{
// Ubah kata jadi lowercase
foreach (splitString($kataPertama) as $huruf) {
if (strpos($kataKedua, $huruf) === false) { // Cek kondisi jika tidak ada huruf dalam kata kedua return false;
}
}
// Semua huruf ada pada kata kedua
return true;
}
// Helper function split string ke array
function splitString
(string
$str): array {
$hasil = [];
$masuk = []; // Array pengecekan duplikat
for ($i = 0; $i < strlen($str); $i++) { if (!isset($masuk[$str[$i]])) { // Jika belum ada dimasukan $hasil[] = $str[$i];
$masuk[$str[$i]] = true;
}
}
return $hasil;
}
// Eksekusi
// Penggunaan var_dump agar return value 'true' atau 'false'
var_dump(punyaHuruf
('cat', 'antarctica')); var_dump(punyaHuruf
('cat', 'australia')); var_dump(punyaHuruf
('cat', 'ANTARCTICA'));
PD9waHAKCmZ1bmN0aW9uIHB1bnlhSHVydWYoc3RyaW5nICRrYXRhUGVydGFtYSwgc3RyaW5nICRrYXRhS2VkdWEpOiBib29sCnsKICAgIC8vIFViYWgga2F0YSBqYWRpIGxvd2VyY2FzZQogICAgJGthdGFQZXJ0YW1hID0gc3RydG9sb3dlcigka2F0YVBlcnRhbWEpOwogICAgJGthdGFLZWR1YSA9IHN0cnRvbG93ZXIoJGthdGFLZWR1YSk7CgogICAgZm9yZWFjaCAoc3BsaXRTdHJpbmcoJGthdGFQZXJ0YW1hKSBhcyAkaHVydWYpIHsKICAgICAgICBpZiAoc3RycG9zKCRrYXRhS2VkdWEsICRodXJ1ZikgPT09IGZhbHNlKSB7IC8vIENlayBrb25kaXNpIGppa2EgdGlkYWsgYWRhIGh1cnVmIGRhbGFtIGthdGEga2VkdWEKICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgIH0KCiAgICAvLyBTZW11YSBodXJ1ZiBhZGEgcGFkYSBrYXRhIGtlZHVhCiAgICByZXR1cm4gdHJ1ZTsKfQoKLy8gSGVscGVyIGZ1bmN0aW9uIHNwbGl0IHN0cmluZyBrZSBhcnJheQ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