for (String s : phone_book) { if (hashMap.get(s) != null) { returnfalse; }
for (int i = 0; i < s.length(); i++) { hashMap.put(s.substring(0, i), 0); } }
returntrue; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import java.util.HashMap;
classSolution{ publicbooleansolution(String[] phone_book){ HashMap<String, Integer> hashMap = new HashMap<>(); for (String s : phone_book) hashMap.put(s, 0); for (String s : phone_book) { for (int i = 1; i < s.length(); i++) { if (hashMap.containsKey(s.substring(0, i))) returnfalse; } }
returntrue; } }
kotlin
1 2 3 4 5 6 7 8 9 10 11
funsolution(phone_book: Array<String>): Boolean { val hashMap = HashMap<String, Int>() phone_book.forEach { hashMap[it] = 0 } phone_book.forEach { for (i in1 until it.length) { if (hashMap.containsKey(it.substring(0, i))) returnfalse } }