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);
}