String[] countWords(String sentence, int returnWordCount) {
		
		// check input parameter
		if (sentence == null || sentence.isEmpty()) {
			System.out.println("Sentence not defined");
			return null;
		}
		
		// check input parameter
		if (returnWordCount <= 0) {
			System.out.println("Return Words must be greater than zero");
			return null;
		}

		// store count for each word discovered, indexed by word
		HashMap<String, Integer> wordFreq = new HashMap<>();
		
		// remove all characters that are not spaces, letters, or digits
		final String regex = "[^a-zA-Z\\d\\ ]";
		sentence = sentence.replaceAll(regex, "");
		
		// parse string by space
		StringTokenizer token = new StringTokenizer(sentence); 
		
		// count the word frequency in the sentence.  This operation is O(n)
		while (token.hasMoreElements()) {
			String word = (String) token.nextElement();
			
			if (wordFreq.containsKey(word)) {
				// existing word found. Increment word count
				wordFreq.put(word,  wordFreq.get(word) + 1);
			} else {
				// new word found, initial count is 1
				wordFreq.put(word, 1);
			}
		}
		
		// convert map to a list
		ArrayList<Map.Entry<String, Integer>> wordFreqList = new ArrayList<>(wordFreq.entrySet());
		
		// sort the list and reverse it so that the greatest counts are in the front
		MapComparator myComparator = new MapComparator();
		Collections.sort(wordFreqList, myComparator);
		Collections.reverse(wordFreqList);

		// adjust return size so that our return list is no bigger than the number of words
		if (returnWordCount > wordFreqList.size()) {
			returnWordCount = wordFreqList.size();
		}
		
		// copy results into return list
		int index = 0;
		String [] returnList = new String[returnWordCount];
		
		for (Map.Entry<String, Integer> x : wordFreqList) {
			
			returnList[index++] = x.getKey();
			
			// break if we reach end of list
			if (index >= returnWordCount) {
				break;
			}
		}
		
		return returnList;
	}