Pages

Monday 28 January 2013

UVA - 902 - Password Search

import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;

public class Main {


public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        StringBuffer sb = new StringBuffer();
        while(sc.hasNext()){
            int n=sc.nextInt();
            String str=sc.next();
            HashMap<String,Integer> hm=new HashMap<String, Integer>();
            String max="";
            int freq=-1;
            for(int i=0;i<str.length()-n;i++){
                StringBuilder temp=new StringBuilder();
                for(int j=i;j<i+n;j++){
                    temp.append(str.charAt(j));
                }
                if(hm.containsKey(temp.toString())){
                    int tempf=hm.get(temp.toString())+1;
                    hm.put(temp.toString(), tempf);
                    if(tempf>freq){
                        max=temp.toString();
                        freq=tempf;
                    }
                }else{
                    hm.put(temp.toString(), 1);
                }
            }
            sb.append(max).append("\n");
        }
        System.out.print(sb);
    }

}

No comments:

Post a Comment