MCPcopy Index your code
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / semordnilap

Function semordnilap

strings/semordnilap.js:3–18  ·  view source on GitHub ↗
(words)

Source from the content-addressed store, hash-verified

1// Time O(n*m) - where n is the number of words and m the length of the longest word
2// Space O(n*m)
3function semordnilap(words) {
4 if (words.length === 0) return [];
5 const wordsSet = new Set(words);
6 const semordnilapPairs = [];
7
8 for (const word of words) {
9 const reverse = word.split("").reverse().join("");
10 if (wordsSet.has(reverse) && reverse !== word) {
11 semordnilapPairs.push([word, reverse]);
12 wordsSet.delete(word);
13 wordsSet.delete(reverse);
14 }
15 }
16
17 return semordnilapPairs;
18}

Callers

nothing calls this directly

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected