August 2003
Intermediate to advanced
672 pages
16h 23m
English
| 11.1 |
import java.util.*; public class UniqueCharacterCounter { /** * A cache, mapping strings to count results. The count values are * stored as Integer objects within the map. */ static Map globalCache = new HashMap(); public static int countUniqueCharacters(String aString) { Object cachedResult = globalCache.get(aString); if (cachedResult != null) return ((Integer) cachedResult).intValue(); // Result was not in the cache, calculate it. int length = aString.length(); Set occurred = new TreeSet(); Set duplicates = new TreeSet(); // Identify occurrences and duplicates for each character in string: for (int i=0; i<length;i++) { Character character = new Character(aString.charAt(i)); if (duplicates.contains(character)) continue; ... |
Read now
Unlock full access