Skip to content

Commit

Permalink
add solution for sherlock and anagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
ragmha committed Apr 25, 2024
1 parent 02cd648 commit bcd060c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Algorithms (16/ 167)
## Algorithms (17/ 167)

| Name | Tags | Solution |
| --------------------------------------------------------- | ----------------------- | ----------------------------------------------------------------------- |
Expand All @@ -10,7 +10,7 @@
| Array Manipulation | `Arrays` | [TypeScript](./src/algorithms/arrays/array-manipulation) |
| Ransom Note | `Hash Tables` | [TypeScript](./src/algorithms/hashtables/ransom-note) |
| Two Strings | `Hash Tables` | [TypeScript](./src/algorithms/hashtables/two-strings) |
| Sherlock and Anagrams | `Hash Tables` | |
| Sherlock and Anagrams | `Hash Tables` | [TypeScript](./src/algorithms/hashtables/sherlock-and-anagrams) |
| Count Triplets | `Hash Tables` | |
| Frequency Queries | `Hash Tables` | |
| Making Anagrams | `Strings` | [TypeScript](./src/algorithms/strings/make-anagram) |
Expand Down
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)
})
})
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
)
}

0 comments on commit bcd060c

Please sign in to comment.