-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add solution for sherlock and anagrams
- Loading branch information
Showing
3 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/algorithms/hashtables/sherlock-and-anagrams/sherlock-and-anagrams.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { | ||
sherlockAndAnagrams, | ||
sherlockAndAnagrams2, | ||
} from './sherlock-and-anagrams' | ||
|
||
describe('Sherlock and Anagrams', () => { | ||
it('should return correct number of anagram pairs for "mom"', () => { | ||
expect(sherlockAndAnagrams('mom')).toBe(2) | ||
expect(sherlockAndAnagrams2('mom')).toBe(2) | ||
}) | ||
|
||
it('should return the correct number of anagram pairs for "abba"', () => { | ||
expect(sherlockAndAnagrams('abba')).toBe(4) | ||
expect(sherlockAndAnagrams2('abba')).toBe(4) | ||
}) | ||
|
||
it('should return 0 for no anagrams', () => { | ||
expect(sherlockAndAnagrams('abcd')).toBe(0) | ||
expect(sherlockAndAnagrams2('abcd')).toBe(0) | ||
}) | ||
}) |
61 changes: 61 additions & 0 deletions
61
src/algorithms/hashtables/sherlock-and-anagrams/sherlock-and-anagrams.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Sherlock and Anagrams | ||
* | ||
* Two strings are anagrams of each other if the letters of one string can be rearranged to form the other string. | ||
* Given a string, find the number of pairs of substrings of the string that are anagrams of each other. | ||
* | ||
* Example: | ||
* s = mom | ||
* | ||
* The list of all anagrammatic pairs is [m,m], [mo,om] at positions 0 and 1 respectively. | ||
* | ||
* Constraints | ||
* | ||
* 1 <= q <= 100 at positions respectively. | ||
* | ||
*/ | ||
|
||
export function sherlockAndAnagrams(s: string): number { | ||
const substringCounts = new Map<string, number>() | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
for (let j = i + 1; j <= s.length; j++) { | ||
let curr = [...s.slice(i, j)].sort().join('') | ||
|
||
if (substringCounts.has(curr)) { | ||
substringCounts.set(curr, substringCounts.get(curr)! + 1) | ||
} else { | ||
substringCounts.set(curr, 1) | ||
} | ||
} | ||
} | ||
|
||
return [...substringCounts.values()].reduce( | ||
(acc, curr) => acc + (curr * (curr - 1)) / 2, | ||
0 | ||
) | ||
} | ||
|
||
export function sherlockAndAnagrams2(s: string): number { | ||
const generateSortedSubstrings = (_: unknown, i: number) => | ||
Array.from({ length: s.length - i }, (_, j) => | ||
s | ||
.slice(j, j + i + 1) | ||
.split('') | ||
.sort() | ||
.join('') | ||
) | ||
|
||
const sortedSubstringCounts = s | ||
.split('') | ||
.flatMap(generateSortedSubstrings) | ||
.reduce( | ||
(map, substr) => map.set(substr, (map.get(substr) || 0) + 1), | ||
new Map<string, number>() | ||
) | ||
|
||
return [...sortedSubstringCounts.values()].reduce( | ||
(acc, count) => acc + (count * (count - 1)) / 2, | ||
0 | ||
) | ||
} |