jsacademyio / Data-Structures-And-Algorithms-Workshop

This is the repository for the Data Structures & Algorithms workshop
MIT License
2 stars 11 forks source link

Anagrams #13

Open acha-bill opened 4 years ago

acha-bill commented 4 years ago

Two strings are anagrams of each other if the letters of one string can be rearranged to form the other string. e.g Stressed = Desserts, Bored = Robed, etc.

Write a function that given a string, find the number of pairs of substrings of the string that are anagrams of each other.

For example s = mom, the list of all anagrammatic pairs is [m,m], [mo,om]. Therefore, the answer is 2.

explanation: e.g for the string "mom", here are the substrings The square brackets are the range that makes the substring

- mom: [0..2]
- mo:  [0,1]
- m: [0]
- o: [1]
- om: [1,2]
- m: [2]

so from all those substrings, these are anagrams of each other

- "mo" : [0,1] is an anagram with "om": [1,2]
- "m": [0] is an anagram with "m": [2]