Summary: Given a string find the number of anagramic pairs of substrings of it. e.g 'abba' pairs are [a,a],[b,b],[ab,ba],[abb,bba] so we have 4 pairs.
I'm wondering if anyone can help me improve my code so that it runs faster. My code passes all test cases when running locally, but times out on hackerrank. Any ideas ?
module.exports = (s = '') => {
let count = 0
let pairs = []
for (let wordSize = 1; wordSize < s.length; wordSize++) {
for (let wordPosition = 0; wordPosition < s.length; wordPosition++) {
const letters = s.substr(wordPosition, wordSize)
const wordKeys = letters.split('');
pairs.push({ letters, keys: wordKeys, wordPosition })
}
}
let pairIndexes = {}
for (let i = 0; i < pairs.length; i++) {
const word = pairs[i]
pairs.forEach((value, index) => {
if (word.wordPosition !== value.wordPosition && index !== i && value.keys.length === word.keys.length) {
const wordSorted = word.keys.sort().join('')
const valueSorted = value.keys.sort().join('')
if (wordSorted !== valueSorted) return
if (pairIndexes[wordSorted] === undefined) pairIndexes[wordSorted] = {}
const pairKeyWord = `${word.wordPosition}-${value.wordPosition}`
const pairValueWord = `${value.wordPosition}-${word.wordPosition}`
if (pairIndexes[wordSorted][pairValueWord] === pairKeyWord) return
pairIndexes[wordSorted][pairKeyWord] = pairValueWord
}
})
}
//console.log(pairs)
//console.log(pairIndexes)
for (const key in pairIndexes) {
if (pairIndexes.hasOwnProperty(key)) {
const pairs = pairIndexes[key];
count += Object.keys(pairs).length
}
}
return count
}
Test cases:
test('A - Given a string return pairs of anagrams', () => {
const s = 'mom'
const expected = 2
const result = anagrams(s)
expect(result).toBe(expected)
})
test('B - Given a string return pairs of anagrams', () => {
const s = 'abba'
const expected = 4
const result = anagrams(s)
expect(result).toBe(expected)
})
test('C - Given a string return pairs of anagrams', () => {
const s = 'abcd'
const expected = 0
const result = anagrams(s)
expect(result).toBe(expected)
})
test('D - Given a string return pairs of anagrams', () => {
const s = 'ifailuhkqq'
const expected = 3
const result = anagrams(s)
expect(result).toBe(expected)
})
test('E - Given a string return pairs of anagrams', () => {
const s = 'kkkk'
const expected = 10
const result = anagrams(s)
expect(result).toBe(expected)
})
test('F - Given a string return pairs of anagrams', () => {
const s = 'cdcd' // c,c d,d cd,cd, cd,dc dc,cd
const expected = 5
const result = anagrams(s)
expect(result).toBe(expected)
})
test('G - Given a string return pairs of anagrams', () => {
const s = 'ifailuhkqqhucpoltgtyovarjsnrbfpvmupwjjjfiwwhrlkpekxxnebfrwibylcvkfealgonjkzwlyfhhkefuvgndgdnbelgruel'
const expected = 399
const result = anagrams(s)
expect(result).toBe(expected)
})
test('H - Given a string return pairs of anagrams', () => {
const s = 'gffryqktmwocejbxfidpjfgrrkpowoxwggxaknmltjcpazgtnakcfcogzatyskqjyorcftwxjrtgayvllutrjxpbzggjxbmxpnde'
const expected = 471
const result = anagrams(s)
expect(result).toBe(expected)
})