0
\$\begingroup\$

Here is the hackerrank question:

For two strings A and B, we define the similarity of the strings to be the length of the longest prefix common to both strings. For example, the similarity of strings "abc" and "abd" is 2, while the similarity of strings "aaa" and "aaab" is 3.

Calculate the sum of similarities of a string S with each of it's suffixes.


I am getting a timeout on some of the test cases but not all of them.

function usernameDisparity(inputs) {
    return inputs.map(function (input) {
        return getDisparity(input);
    });
}


function getDisparity(input) { 
    var sum = 0;
    for (var i = 0; i < input.length; i++) { 
        sum = sum + getSimilarity(i, input);
    }
    return sum;
}

function getSimilarity(index, input) {
    var similarity = 0; 
    for (var i = 0; i < input.length; i++, index++) { 
        if (input[index] !== input[i]) {
            return similarity;
        } else { 
            similarity = similarity + 1;
        }
    }
    return similarity;
}
\$\endgroup\$
  • 2
    \$\begingroup\$ Your code does not match the quoted problem. \$\endgroup\$ – Blindman67 Jan 27 at 4:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.