Answer the question
In order to leave comments, you need to log in
What is the complexity of the algorithm?
Help plz estimate the complexity of the algorithm (expands letters in words without changing the order of words):
const reverseWords = (str) => {
const wordsArr = str.split(' ');
const reversedArr = wordsArr.map((word) => {
let newWord = '';
for (let i = word.length - 1; i >= 0; i--) {
newWord += word[i];
}
return newWord;
});
const reversedStr = reversedArr.join(' ');
return reversedStr;
};
module.exports = reverseWords;
// O(n)?
Answer the question
In order to leave comments, you need to log in
In this implementation, most likely at least O(4n)
Split, reverse, join + concatenation (newWord+=)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question