package bioinformatics_textbook_track;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;

public class BA1B {

	public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
		// TODO Auto-generated method stub
		String input = "";
		try {
			input = new String(
					Files.readAllBytes(Paths
							.get("src/bioinformatics_textbook_track/rosalind_1a.txt")));
		} catch (IOException e) {

			e.printStackTrace();
		}
		String[] lines = input.split("\n");
		PrintWriter writer = new PrintWriter(
				"src/bioinformatics_textbook_track/output.txt", "UTF-8");
		writer.print(getMCKmer(lines[0], Integer.parseInt(lines[1])));
		writer.close();
	}

	private static String getMCKmer(String input, int k) {
		HashMap<String, Integer> countOfKmers = new HashMap<String, Integer>();
		for (int i = 0; i < input.length() - k + 1; i++)
		{
			if(countOfKmers.containsKey(input.substring(i, i + k)))
			{
				countOfKmers.replace(input.substring(i, i + k), countOfKmers.get(input.substring(i, i + k)) + 1);
			}
			else
			{
				countOfKmers.put(input.substring(i, i + k), 1);
			}
		}
		int max = 0;
		for(int value : countOfKmers.values())
		{
			if(value > max)
			{
				max = value;
			}
		}
		
		String output = "";
		for(String key : countOfKmers.keySet())
		{
			if(countOfKmers.get(key) == max)
			{
				output += key + " ";
			}
		}
		
		return output;
	}

}
