811. Subdomain Visit Count

811. Subdomain Visit Count

import java.util.Hashtable;
public static List subdomainVisits(String[] cpdomains) {
		List out = new ArrayList();

		Hashtable totals = new Hashtable();

		for (int i = 0; i < cpdomains.length; i++) {
			String[] cpDomainsSplit = cpdomains[i].split(" ");
			String visits = cpDomainsSplit[0];
			String cpdomain = cpDomainsSplit[1];

			String[] parts = cpdomain.split("\\.");

			
			for (int x = 0; x = 0; j--) {
				String currentPart = "";
				for (int k = j; k < parts.length; k++) {
					currentPart += (parts[k]);
				}
				if (totals.get(currentPart) != null) {
					int exist = totals.get(currentPart);
					totals.replace(currentPart, totals.get(currentPart) + new Integer(visits));
				} else
					totals.put(currentPart, new Integer(visits));
			}

		}
		for (String key : totals.keySet()) {
			out.add(totals.get(key) + " " + key);
		}
		return out;
	}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s