Heterogram คือคำ/ประโยคที่ตัวอักษรแต่ละตัวไม่ซ้ำกัน (ต่างจาก pangram ที่ต้องมีทุกตัวอักษร)
ตัวอย่าง
"abc def ghi" -> Heterogram (ไม่มีตัวซ้ำ) "abc bcd dfh" -> Not Heterogram (b,c,d ซ้ำ)
อัลกอริทึม
- แยกตัวอักษรทั้งหมด (เว้น space)
- แปลงเป็น set (unique)
- ถ้าความยาว set == จำนวนตัวอักษร -> Heterogram
โค้ดตัวอย่าง
def stringheterogram(s, n):
hash = [0] * 26
for i in range(n):
if s[i] != ' ':
idx = ord(s[i]) - ord('a')
if hash[idx] == 0:
hash[idx] = 1
else:
return False
return True
s = input("Enter the String: ")
n = len(s)
print(s, "This string is Heterogram" if stringheterogram(s, n) else "This string is not Heterogram")
ผลลัพธ์
Enter the String: asd fgh jkl asd fgh jkl This string is Heterogram Enter the String: asdf asryy asdf asryy This string is not Heterogram