Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Mixing {
- public static String mix(String s1, String s2) {
- // your code
- /*String s1A = s1.replaceAll("[^a-z]", "");
- String s2A = s2.replaceAll("[^a-z]", "");*/
- int[][] count = new int[2][26];
- ArrayList<String> list = new ArrayList<>();
- int index = 0;
- for (char c = 'a'; c <= 'z'; c++){
- final char C = c;
- count[0][index] = (int) s1.chars().filter(i -> i == C).count();
- count[1][index] = (int) s2.chars().filter(i -> i == C).count();
- index++;
- }
- for (int i = 0; i < 26; i++){
- if (count[0][i] > 1 || count[1][i] > 1){
- int j = i+97;
- char c = (char)j;
- String s = count[0][i] > count[1][i] ? ("1:"+String.join("", Collections.nCopies(count[0][i], String.valueOf(c)))) :
- count[1][i] > count[0][i] ? ("2:"+String.join("", Collections.nCopies(count[1][i], String.valueOf(c)))) :
- ("=:"+String.join("", Collections.nCopies(count[1][i], String.valueOf(c))));
- list.add(s);
- }
- }
- //Arrays.sort(rank, Comparator.comparingInt(String::length));
- Collections.sort(list, new LengthFirstComparator());
- StringBuffer sb = new StringBuffer("");
- for (String s : list){
- if (!s.equals("")){
- sb.append(s);
- sb.append("/");
- }
- }
- if (sb.length()>0){
- sb.deleteCharAt(sb.length()-1);
- }
- return sb.toString();
- }
- static class LengthFirstComparator implements Comparator<String> {
- @Override
- public int compare(String o1, String o2) {
- if (o1.length()!=o2.length()) {
- return o2.length()-o1.length(); //overflow impossible since lengths are non-negative
- }
- return o1.compareTo(o2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement