/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public void charCount
(String sentence
) { // Convert the sentence to lower case.
String l
= sentence.
toLowerCase();
// Create a set of separators
Set
<Character
> separators
= new HashSet
<>(Arrays.
asList(' ',
'!',
'?',
';',
'.',
'/',
'~',
'(',
')',
'`',
'-',
'"'));
// Create a queue of characters in the sentence.
Queue<Character> q = new LinkedList<>();
// Create a map to keep track of the number of characters.
Map
<Character, Integer
> charMap
= new HashMap
<>();
// For each character in the string...
for (int i = 0; i < l.length(); i++) {
char c = l.charAt(i);
// If it is not a separator...
if (!separators.contains(c)) {
// And it is already in the map...
if (charMap.containsKey(c)) {
// Update the value in the map.
charMap.put(c, charMap.get(c) + 1);
} else {
// Otherwise, add character to the map and enqueue the newly encountered char.
charMap.put(c, 1);
q.add(c);
}
}
}
// For each character in the queue, print out its corresponding value in the map.
System.
out.
println(outChar
+ ": " + charMap.
get(outChar
)); }
}
public static void main
(String[] args
) { Ideone cc = new Ideone();
cc.charCount("Count the, characters.");
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKICAgIHB1YmxpYyB2b2lkIGNoYXJDb3VudChTdHJpbmcgc2VudGVuY2UpIHsKICAgICAgICAvLyBDb252ZXJ0IHRoZSBzZW50ZW5jZSB0byBsb3dlciBjYXNlLgogICAgICAgIFN0cmluZyBsID0gc2VudGVuY2UudG9Mb3dlckNhc2UoKTsKICAgICAgICAKICAgICAgICAvLyBDcmVhdGUgYSBzZXQgb2Ygc2VwYXJhdG9ycwogICAgICAgIFNldDxDaGFyYWN0ZXI+IHNlcGFyYXRvcnMgPSBuZXcgSGFzaFNldDw+KEFycmF5cy5hc0xpc3QoJyAnLCAnIScsICc/JywgJzsnLCAnLicsICcvJywgJ34nLCAnKCcsICcpJywgJ2AnLCAnLScsICciJykpOwogICAgICAgIAogICAgICAgIC8vIENyZWF0ZSBhIHF1ZXVlIG9mIGNoYXJhY3RlcnMgaW4gdGhlIHNlbnRlbmNlLgogICAgICAgIFF1ZXVlPENoYXJhY3Rlcj4gcSA9IG5ldyBMaW5rZWRMaXN0PD4oKTsKICAgICAgICAKICAgICAgICAvLyBDcmVhdGUgYSBtYXAgdG8ga2VlcCB0cmFjayBvZiB0aGUgbnVtYmVyIG9mIGNoYXJhY3RlcnMuCiAgICAgICAgTWFwPENoYXJhY3RlciwgSW50ZWdlcj4gY2hhck1hcCA9IG5ldyBIYXNoTWFwPD4oKTsKICAgICAgICAKICAgICAgICAvLyBGb3IgZWFjaCBjaGFyYWN0ZXIgaW4gdGhlIHN0cmluZy4uLgogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbC5sZW5ndGgoKTsgaSsrKSB7CiAgICAgICAgICAgIGNoYXIgYyA9IGwuY2hhckF0KGkpOwogICAgICAgICAgICAKICAgICAgICAgICAgLy8gSWYgaXQgaXMgbm90IGEgc2VwYXJhdG9yLi4uCiAgICAgICAgICAgIGlmICghc2VwYXJhdG9ycy5jb250YWlucyhjKSkgewogICAgICAgICAgICAgICAgLy8gQW5kIGl0IGlzIGFscmVhZHkgaW4gdGhlIG1hcC4uLgogICAgICAgICAgICAgICAgaWYgKGNoYXJNYXAuY29udGFpbnNLZXkoYykpIHsKICAgICAgICAgICAgICAgICAgICAvLyBVcGRhdGUgdGhlIHZhbHVlIGluIHRoZSBtYXAuCiAgICAgICAgICAgICAgICAgICAgY2hhck1hcC5wdXQoYywgY2hhck1hcC5nZXQoYykgKyAxKTsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgLy8gT3RoZXJ3aXNlLCBhZGQgY2hhcmFjdGVyIHRvIHRoZSBtYXAgYW5kIGVucXVldWUgdGhlIG5ld2x5IGVuY291bnRlcmVkIGNoYXIuCiAgICAgICAgICAgICAgICAgICAgY2hhck1hcC5wdXQoYywgMSk7CiAgICAgICAgICAgICAgICAgICAgcS5hZGQoYyk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgCiAgICAgICAgLy8gRm9yIGVhY2ggY2hhcmFjdGVyIGluIHRoZSBxdWV1ZSwgcHJpbnQgb3V0IGl0cyBjb3JyZXNwb25kaW5nIHZhbHVlIGluIHRoZSBtYXAuCiAgICAgICAgZm9yIChDaGFyYWN0ZXIgb3V0Q2hhciA6IHEpIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKG91dENoYXIgKyAiOiAiICsgY2hhck1hcC5nZXQob3V0Q2hhcikpOwogICAgICAgIH0KICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgSWRlb25lIGNjID0gbmV3IElkZW9uZSgpOwogICAgICAgIGNjLmNoYXJDb3VudCgiQ291bnQgdGhlLCBjaGFyYWN0ZXJzLiIpOwogICAgfQp9Cg==