samedi 25 avril 2015

Make JAVA code efficient


I am trying to solve a problem and I am getting time limit errors, can someone help improving my code? Thanks.

Link for the problem: http://ift.tt/1vdOQVb My code:

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
        String aux;
        String input;
        int contador;
        while((input = in.readLine()) != null) {
            StringTokenizer tokens = new StringTokenizer(input);
            contador = 0;
            for (int i = 0; i < tokens.countTokens(); i++) {
                aux = tokens.nextToken();
                for (int j = 0; j < aux.length() - 1; j++) {
                    if (Character.isLetter(aux.charAt(j)) && Character.isLetter(aux.charAt(j + 1))) {
                        contador++;
                    }
                }
            }
            out.write(contador);
        }
        out.flush();
        in.close();
        out.close();
    }
}


Aucun commentaire:

Enregistrer un commentaire