You are viewing a single comment's thread. Return to all comments →
I did mine with a hashMap, no trees, passes all the test cases, Java 8. Someone tell me if the memory is a problem?
public class Solution { public static final String ADD = "add"; public static final String FIND = "find"; private static HashMap<String, Integer> contactList = new HashMap<String, Integer>(); public static void main(String[] args) { Scanner in = new Scanner(System.in); Integer n = Integer.parseInt(in.nextLine()); for(int a0 = 0; a0 < n; a0++){ String[] opData = in.nextLine().split("\\s+"); if (opData[0].equalsIgnoreCase(ADD)){ addSubstringsToContacts(opData[1]); } if (opData[0].equalsIgnoreCase(FIND)){ System.out.println(numOfPartials(opData[1])); } } } private static int numOfPartials(String partialStringToFind) { int result = 0; if (contactList.containsKey(partialStringToFind)) { result = contactList.get(partialStringToFind); } return result; } private static void addSubstringsToContacts(String contactName) { String partialValue; for (int j = 1; j <= contactName.length(); j++){ partialValue = contactName.substring(0, j); if (contactList.containsKey(partialValue)) { contactList.put(partialValue, contactList.get(partialValue) + 1); } else { contactList.put(partialValue, 1); } } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Tries: Contacts
You are viewing a single comment's thread. Return to all comments →
I did mine with a hashMap, no trees, passes all the test cases, Java 8. Someone tell me if the memory is a problem?