Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hashJUNI;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- public class MostFrequentSubstring {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- HashMap<String, Integer> map = new HashMap<String,Integer>(200);
- String red = br.readLine().trim();
- int brojac=0;
- for(int i=0;i<=red.length();i++){
- for(int j=0;j+i<=red.length();j++){
- if(map.get(red.substring(j,i+j)) == null){
- map.put(red.substring(j,i+j), 1);
- }
- else{
- int result = map.get(red.substring(j,i+j));
- brojac = result + 1;
- map.put(red.substring(j,i+j),brojac);
- }
- }
- }
- int maxi = 0;
- int maxDolzina =0;
- String najdolga = null;
- for(int i=0;i<=red.length();i++){
- for(int j=0;j+i<=red.length();j++){
- int result = map.get(red.substring(j,i+j));
- if(red.substring(j,j+i).equals("")){
- continue;
- }
- if(result > maxi){
- maxi = result;
- najdolga = red.substring(j,i+j);
- }
- if(result == maxi){
- if(red.substring(j,i+j).length() > maxDolzina){
- maxDolzina = red.substring(j,i+j).length();
- najdolga = red.substring(j,i+j);
- }
- else{
- if(red.substring(j,i+j).length() == maxDolzina){
- if(najdolga.compareTo(red.substring(j,i+j))>0){
- najdolga = red.substring(j,i+j);
- }
- }
- }
- }
- }
- }
- System.out.println(najdolga);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement