1

I need to write an algorithm \ program that received only single output (string) and return all the possible substrings and the number of occurrences of each substring.

I managed to do so, using 2 for loops, and complexity time of O(N^2).

My Question is - Is there more efficient way to do so with lower complexity time?!

I provide my code here, enter link description here

or -

const myString = 'dgfdgwababccgregabcaavbrabcrafsfabcdsdsfhjuuyiuyuitrerttfvbcvretrcw';
const stringLength = myString.length;

var myMap = new Map();

for(var i=0 ; i< stringLength; ++i){
    for(var j=0; j< (stringLength - i) ; ++j){
        let key = myString.substring(j, i+j+1)
        //check if substring already exists
        let exists = myMap.get(key);
        let count = exists ? exists+1 : 1;
        //add elements to the map
        myMap.set(myString.substring(j, i+j+1), count)
    }
}

//iterate the map and prints the entries
for (var [key, value] of myMap) {
    console.log(key + ' = ' + value);
}
Zlil Korman
  • 195
  • 1
  • 10
  • 2
    Given the requirement, not sure how you're going to be able to iterate much less than that. Is the problem being described in its entirety, or have you chosen THIS particular solution to the problem? In other words, maybe the root requirement could be met differently (if there IS a root requirement beyond what we see here). Also worth mentioning, if your expected use case is very similar to this (length of input string), this was acceptably fast. No point optimizing until a pain point is reached. – Greg Pettit Jan 04 '18 at 15:44
  • I don't think it's possible to generate all sub-strings in less than O(n^2) (unless maybe parallel processing), but I think it's possible to get either all counts without the sub-string values, or all character and **repeating** sub-string counts in close to average O(n) ( O(n) best and O(n^2) worst ). – Slai Jan 04 '18 at 16:36
  • the question is just as i described, single i input of some string, output all possible substrings and number of occurrences. i'm wondering if i need to count the complexity of substring function also in my total complexity? – Zlil Korman Jan 04 '18 at 20:55

1 Answers1

0

You can basically not get faster than O(n^2) since the number of possible different substrings is n^2.

If you are interested in the longest substrings (so substrings of substrings are not explicitly counted) that occur at least two times then there is a more efficient algorithm running in O(n log n) in average time. There are some worst cases, e.g, when all characters are the same.

I sketched the outline of the algorithm in this answer. The answer provided is for a set of strings, so you can ignore the first step.

SaiBot
  • 3,595
  • 1
  • 13
  • 19