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;
	}
Advertisements

728. Self Dividing Numbers

728. Self Dividing Numbers

class Solution {
	public static boolean isSelfDividing( int i){
		for ( int x = i ; x > 0 ; x /=10){
			int m = x % 10;
			if( m == 0 /*to avoid i%0*/|| i % m != 0) return false;
		}
		return true;
	}
    public List selfDividingNumbers(int left, int right) {
    	List out = new ArrayList();
    	
    	for ( int i = left ; i <= right ; i++ ){
    		if ( isSelfDividing(i)) out.add(i);
    	}
    	return out;
    }
}

Unique Morse Code Words

Unique Morse Code Words

class Solution {
    public int uniqueMorseRepresentations(String[] words) {
              String [] codes = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        HashSet output = new HashSet();

        for ( int i = 0 ; i &lt; words.length ; i++){
        	 StringBuilder transformation = new StringBuilder();
        	for ( int j = 0 ; j <span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>&lt; words[i].length() ; j++ ){
        		transformation.append(codes[words[i].charAt(j)-97]);
        	}
        	output.add(transformation.toString());
        }

    	return output.size();
    }
}