Algorithm to find the most common substrings in a string

后端 未结 5 2122
耶瑟儿~
耶瑟儿~ 2020-12-01 03:36

Is there any algorithm that can be used to find the most common phrases (or substrings) in a string? For example, the following string would have \"hello world\" as its most

5条回答
  •  半阙折子戏
    2020-12-01 04:35

    just pseudo code, and maybe this isn't the most beautiful solution, but I would solve like this:

    function separateWords(String incomingString) returns StringArray{
      //Code
    }
    
    function findMax(Map map) returns String{
      //Code
    }
    
    function mainAlgorithm(String incomingString) returns String{
        StringArray sArr = separateWords(incomingString);
        Map map; //init with no content
        for(word: sArr){
            Integer count = map.get(word);
            if(count == null){
                map.put(word,1);
            } else {
                //remove if neccessary
                map.put(word,count++); 
            }
       }
       return findMax(map);
    }
    

    Where map can contain a key, value pairs like in Java HashMap.

提交回复
热议问题