I'm solving problems at hackerrank. I've solved some hard problems, but didn't accepted because of TLE.
In Java, BufferReader helps against TLE.
Is there any way in Javascript to prevent TLE?? I've changed my algo few times.. but didn't work. Problem:
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.
My code:
function stringSimilarity(s) {
// Write your code here
let myinput = s.split("\n")
myinput.forEach((elem) => {
prefcount(elem)
})
function prefcount(word) {
let array = []
let x
for(let i = 0; i < word.length; i++) {
x = word.substring(i)
array.push(x)
}
for(let arr = 0; arr < array.length;){
let res = 0
for(let string = 0; string < array[arr].length;) {
if(array[arr][string] == word[string]) {
res++
string++
arr++
} else if(array[arr][string] != word[string]){
arr++
}
}
array[arr] = res
}
console.log(array)
}
}