Task: Most frequent k chars distance


C++: #include <string>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
#include <utility>
#include <sstream>

std::string mostFreqKHashing ( const std::string & input , int k ) {
   std::ostringstream oss ;
   std::map<char, int> frequencies ;
   for ( char c : input ) {
      frequencies[ c ] = std::count ( input.begin( ) , input.end( ) , c ) ;
   }
   std::vector<std::pair<char , int>> letters ( frequencies.begin( ) , frequencies.end( ) ) ;
   std::sort ( letters.begin( ) , letters.end( ) , [input] ( std::pair<char, int> a ,
	         std::pair<char, int> b ) { char fc = std::get<0>( a ) ; char fs = std::get<0>( b ) ; 
	         int o = std::get<1>( a ) ; int p = std::get<1>( b ) ; if ( o != p ) { return o > p ; }
	         else { return input.find_first_of( fc ) < input.find_first_of ( fs ) ; } } ) ;
   for ( int i = 0 ; i < letters.size( ) ; i++ ) {
      oss << std::get<0>( letters[ i ] ) ;
      oss << std::get<1>( letters[ i ] ) ;
   }
   std::string output ( oss.str( ).substr( 0 , 2 * k ) ) ;
   if ( letters.size( ) >= k ) {
      return output ;
   }
   else {
      return output.append( "NULL0" ) ;
   }
}

int mostFreqKSimilarity ( const std::string & first , const std::string & second ) {
   int i = 0 ;
   while ( i < first.length( ) - 1  ) {
      auto found = second.find_first_of( first.substr( i , 2 ) ) ;
      if ( found != std::string::npos ) 
	 return std::stoi ( first.substr( i , 2 )) ;
      else 
	 i += 2 ;
   }
   return 0 ;
}

int mostFreqKSDF ( const std::string & firstSeq , const std::string & secondSeq , int num ) {
   return mostFreqKSimilarity ( mostFreqKHashing( firstSeq , num ) , mostFreqKHashing( secondSeq , num ) ) ;
}

int main( ) {
   std::string s1("LCLYTHIGRNIYYGSYLYSETWNTGIMLLLITMATAFMGYVLPWGQMSFWGATVITNLFSAIPYIGTNLV" ) ;
   std::string s2( "EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG" ) ;
   std::cout << "MostFreqKHashing( s1 , 2 ) = " << mostFreqKHashing( s1 , 2 ) << '\n' ;
   std::cout << "MostFreqKHashing( s2 , 2 ) = " << mostFreqKHashing( s2 , 2 ) << '\n' ;
   return 0 ;
}




Java: import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class SDF {

    
    public static HashMap<Character, Integer> countElementOcurrences(char[] array) {

        HashMap<Character, Integer> countMap = new HashMap<Character, Integer>();

        for (char element : array) {
            Integer count = countMap.get(element);
            count = (count == null) ? 1 : count + 1;
            countMap.put(element, count);
        }

        return countMap;
    }
    
    
    private static <K, V extends Comparable<? super V>>
            HashMap<K, V> descendingSortByValues(HashMap<K, V> map) { 
	List<Map.Entry<K, V>> list = new ArrayList<Map.Entry<K, V>>(map.entrySet());
	
	Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
		public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
		    return o2.getValue().compareTo(o1.getValue());
		}
	    });

	
	
	HashMap<K, V> sortedHashMap = new LinkedHashMap<K, V>();
	for (Map.Entry<K, V> entry : list) {
	    sortedHashMap.put(entry.getKey(), entry.getValue());
	} 
	return sortedHashMap;
    }
    
    public static String mostOcurrencesElement(char[] array, int k) {
        HashMap<Character, Integer> countMap = countElementOcurrences(array);
        System.out.println(countMap);
        Map<Character, Integer> map = descendingSortByValues(countMap); 
        System.out.println(map);
        int i = 0;
        String output = "";
        for (Map.Entry<Character, Integer> pairs : map.entrySet()) {
	    if (i++ >= k)
		break;
            output += "" + pairs.getKey() + pairs.getValue();
        }
        return output;
    }
    
    public static int getDiff(String str1, String str2, int limit) {
        int similarity = 0;
	int k = 0;
	for (int i = 0; i < str1.length() ; i = k) {
	    k ++;
	    if (Character.isLetter(str1.charAt(i))) {
		int pos = str2.indexOf(str1.charAt(i));
				
		if (pos >= 0) {	
		    String digitStr1 = "";
		    while ( k < str1.length() && !Character.isLetter(str1.charAt(k))) {
			digitStr1 += str1.charAt(k);
			k++;
		    }
					
		    int k2 = pos+1;
		    String digitStr2 = "";
		    while (k2 < str2.length() && !Character.isLetter(str2.charAt(k2)) ) {
			digitStr2 += str2.charAt(k2);
			k2++;
		    }
					
		    similarity += Integer.parseInt(digitStr2)
			+ Integer.parseInt(digitStr1);
					
		} 
	    }
	}
	return Math.abs(limit - similarity);
    }
    
    public static int SDFfunc(String str1, String str2, int limit) {
        return getDiff(mostOcurrencesElement(str1.toCharArray(), 2), mostOcurrencesElement(str2.toCharArray(), 2), limit);
    }

    public static void main(String[] args) {
        String input1 = "LCLYTHIGRNIYYGSYLYSETWNTGIMLLLITMATAFMGYVLPWGQMSFWGATVITNLFSAIPYIGTNLV";
        String input2 = "EWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTIKDFLG";
        System.out.println(SDF.SDFfunc(input1,input2,100));

    }

}