Java Doubt- runtime too long

hi,
Im a new user… i have this code: im taking the input as pelpevettrrrlrplvlgqtlrqvvhrdpgrrlevpelkmdtkkvr from an external txt file but it is taking ages to run the code and give the result… in most cases it terminates… its taking more than 40 minutes if i keep the word length of input to 20 letters… need help to rectify it… plz help.

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.util.Collection;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Map.Entry;


/**
 * Find the Frequency of Codon and Print the combinations of CODONS for the given Word
 * @author
 */
public class CountCodon {
	private static int count;

	private static String file = "C:\\input.txt";

	public static void main(String[] args) throws Exception {

		BufferedReader br = new BufferedReader(new FileReader(new File(file)));
		String[] words = br.readLine().trim().split(" ");

		Map<Character,String[]> hash=init();
		for(String word : words)
		{
			count=0;
			word=word.trim().toUpperCase();
			combinations(hash, word , word.length(),0 ,"");
			System.out.println("Total Combinations :"+count);
			Collection<String[]> codons = hash.values();

			Iterator it = codons.iterator();

			while(it.hasNext())
			{
				String[] codes = (String[]) it.next();
				for(String code : codes )
				{
					int freq = getFrequencyCount(code, word,hash);
					System.out.println("CODON : "+code + " ---> " + "FREQ : " +freq);
				}

			}



		}
	private static void solve(String word, String codon) {
		Map<Character,String[]> hash=init();

		combinations(hash, word , word.length(),0 ,"");
		System.out.println("Total Combinations :"+count);

		int freq = getFrequencyCount(codon, word,hash);
		System.out.println("CODON : "+codon + " ---> " + "FREQ : " +freq);
		System.out.println("######################################");
	}*/


	/**
	 * Returns the frequency count of the CODON
	 * @param codon
	 * @param word
	 * @param hash
	 * @return
	 */
	private static int getFrequencyCount(String codon ,String word, Map<Character, String[]> hash) {

		int currCount = 1;

		int len=word.length();
		//count the total combinations
		for(int i=0;i<len;i++)
		{
			if(hash.get(word.charAt(i)) != null)
			currCount = currCount * hash.get(word.charAt(i)).length ;
		}

		//System.out.println("Total Combinations :"+currCount);

		//Iterate over map and fetch, the character and size of the codons corresponding to that character
		char c = 0;
		int size=0;
		for(Entry<Character, String[]> entry : hash.entrySet())
		{
			String[] vals =entry.getValue();
			for(String v:vals)
			{
				if(v.equalsIgnoreCase(codon)) //if codon is found, get the character
				{
					c=entry.getKey();
					size=vals.length;
					break;
				}
			}
		}

		//If no charater is found corresponding to the CODON
		if(c==0)
			return 0;

		//No. of times the character appears in the word, tricky
		int charCount = word.split(c+"").length-1;

		return charCount * (currCount/size);
	}

	/**
	 * Prints All combinations of the codons
	 * @param hash: storage unit of character and corresponding codons
	 * @param word: the given word , eg:
	 * @param len: length or 'word'
	 * @param i: currrent character in 'word'
	 * @param s: combination formed in intermediate steps
	 */
	private static void combinations(Map<Character, String[]> hash,
			String word, int len , int i,String s) {

		//if currCharacter reaches end of word, print
		if(i>=len)
		{
			System.out.println(s);
			count++;
			return;
		}

		//current Character
		char c = word.charAt(i);
		//Codons corresponding to the character
		String[] codons = hash.get(c);

		String temp = s;
		if(codons!=null)
		{
			for(int j=0;j<codons.length;j++)
			{
				s=temp; //initialize s , with the value held in temp everytime s gets modified
				s = s+":"+codons[j];
				combinations(hash,word,len,i+1,s);
			}
		}
	}

	/**
	 * Initialzing Map
	 * @return: returns map of character and its corresponding Codons
	 */
	private static Map<Character,String[]> init() {
		Map<Character,String[]> hash = new HashMap<Character,String[]>();
		hash.put('F',new String[]{"UUU","UUC"});
		hash.put('I',new String[]{"AUU","AUC","AUA"});
		hash.put('P',new String[]{"CCU","CCC","CCA","CCG"});
		hash.put('S',new String[]{"UCU","UCC","UCA","UCG","AGU","AGC"});
		hash.put('T',new String[]{"ACU","ACC","ACA","ACG"});
		hash.put('A',new String[]{"GCU","GCC","GCA","GCG"});
		hash.put('E',new String[]{"CAA","CAG"});
		hash.put('L',new String[]{"UUA","UUG","CUU","CUC","CUA","CUG"});
		hash.put('V',new String[]{"GUU","GUC","GUA","GUG"});
		hash.put('T',new String[]{"UAU","UAC"});
		hash.put('R',new String[]{"CGU","CGC","CGA","CGG","AGA","AGG"});
        hash.put('K',new String[]{"AAA","AAG"});
        hash.put('Y',new String[]{"UAU","UAC"});
        hash.put('H',new String[]{"CAU","CAC"});
        hash.put('N',new String[]{"AAU","AAC"});
        hash.put('D',new String[]{"GAU","GAC"});
        hash.put('C',new String[]{"UGU","UGC"});
        hash.put('W',new String[]{"UGG"});
        hash.put('G',new String[]{"GGU","GGC","GGA","GGG"});
        hash.put('M',new String[]{"AUG"});
		return hash;
	}

}

Can you:

  1. provide the compileable code? The one from question is not it seems - NyVpRL - Online IDE & Debugging Tool - Ideone.com
  2. describe what are you doing? combinations() seems suspicious to me…

This part of the code definitely has some problem

String[] words = br.readLine().trim().split(" ");

The whole of your String is getting stored in words[0] and i think this isn’t you want.
So you are unable to manipulate the data.

Hope it helps!

Plz help me out

im taking a protein sequence and decoding it into codons… n outputting the frequency as well as the combinations of codons.
its taking too much time to run the code …

yup… if u can provide that takes less time …il be greatfull

thanks for the reply…but it is supposed to get the output as codons which is a triplet of letters…so it gives a sequence of words as output

As i can understand,you are replacing all the letters with the values in hash. But you are not able to get individual values!

im getting the output as desired… the code running time is too long