1 / 2
May 2011

This is my code for problem NHAY spoj.pl/problems/NHAY/

import java.util.Scanner;
class Ne{
	private int dem=0;
	Ne(String c1,String c2){
		 if (c1.length()>c2.length()) System.out.println();
	      else {
	    	  while (c2.indexOf(c1,dem)!=-1){
	    		  System.out.println(c2.indexOf(c1,dem));
	    		  dem=c2.indexOf(c1,dem)+1;
	    	  }
	      }
	}
}
public class Need {
    /**
 * @param args
 */
public static void main(String[] args) {
	// TODO Auto-generated method stub
  Scanner sc=new Scanner(System.in);
 while (sc.hasNextLine()){
     String dem=sc.next();
     String ch1=sc.next();
     String ch2=sc.next();
     Ne nn=new Ne(ch1, ch2);
    
     }
}
}
i can't understand why I get TLE although I only use 2 loop,one for read input and one for solve.Please help me to check it..
  • created

    May '11
  • last reply

    May '11
  • 1

    reply

  • 219

    views

  • 2

    users

  • 1

    link

You are ignoring the complexity of indexOf. I dont know the exact complexity of it, but would expect it to run in O(length of string to find*length to find within).