-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d471917
commit f70ad14
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.gitproject; | ||
|
||
import java.util.*; | ||
|
||
public class Beecrowd1251_uri { | ||
public static void main(String[] args) { | ||
boolean line = false; | ||
try (Scanner s = new Scanner(System.in)) { | ||
while (s.hasNext()) { | ||
if (line) { | ||
System.out.println(); | ||
} | ||
line = true; | ||
String input = s.nextLine(); | ||
HashMap<String, Integer> map = new HashMap<>(); | ||
|
||
for (Character c : input.toCharArray()) { | ||
Integer num = 1; | ||
String ascii = String.valueOf(c); | ||
if (map.containsKey(ascii)) { | ||
num += map.get(ascii); | ||
} | ||
map.put(ascii, num); | ||
} | ||
Map<String, Integer> sorted = sortByValueThenKey(map); | ||
for (String r: sorted.keySet()) { | ||
System.out.println(((int) (r.charAt(0)) + " " + sorted.get(r))); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static HashMap<String, Integer> | ||
sortByValueThenKey(HashMap<String, Integer> hm) | ||
{ | ||
// Create a list from elements of HashMap | ||
List<Map.Entry<String, Integer> > list = new LinkedList<>(hm.entrySet()); | ||
|
||
// Sort the list using lambda expression | ||
list.sort((i1, i2) -> | ||
i1.getValue().equals(i2.getValue()) ? | ||
i2.getKey().compareTo(i1.getKey()) : i1.getValue().compareTo(i2.getValue())); | ||
|
||
// put data from sorted list to hashmap | ||
HashMap<String, Integer> temp = new LinkedHashMap<>(); | ||
for (Map.Entry<String, Integer> aa : list) { | ||
temp.put(aa.getKey(), aa.getValue()); | ||
} | ||
return temp; | ||
} | ||
} |