Best Solution for Arrays, DP, String 🚀 in C++, Python and Java || 100% working
💡 IntuitionFor each word, count the permutations by dividing the factorial of its length by the factorials of the frequencies of its characters. Use modular ari
# 💡 Intuition
For each word, count the permutations by dividing the factorial of its length by the factorials of the frequencies of its characters. Use modular arithmetic to handle large numbers. 🚀

# 🔮 Approach

1. Put all numbers in a HashSet (removes duplicates automatically)
2. Loop from 1 to n
3. If number isn't in set, it's missing! ✨

# ⏳ Complexity
- Time: $$O(n)$$ for creating set and checking numbers
- Space: $$O(n)$$ for storing set

# 💻 Code
```cpp []
Code
class Solution {
public:
    int countAnagrams(string s) {
        long long MOD = 1e9 + 7;
        vector<long long> fact(101, 1);
        for (int i = 2; i <= 100; i++) {
            fact[i] = fact[i - 1] * i % MOD;
        }
        
        long long ans = 1;
        stringstream ss(s);
        string word;
        
        while (ss >> word) {
            vector<int> freq(26, 0);
            for (char c : word) freq[c - 'a']++;
            long long count = fact[word.size()];
            for (int f : freq) {
                if (f > 1) count = count * pow(fact[f], MOD - 2, MOD) % MOD;
            }
            ans = ans * count % MOD;
        }
        
        return ans;
    }
};
```

```python []
Code
class Solution(object):
    def countAnagrams(self, s):
        MOD = 10**9 + 7
        
        def mod_inv(x, mod=MOD):
            return pow(x, mod - 2, mod)
        
        arr = s.split()
        max_len = max(len(word) for word in arr)
        fact = [1] * (max_len + 1)
        for i in range(2, max_len + 1):
            fact[i] = fact[i - 1] * i % MOD
        
        ans = 1
        for word in arr:
            count = fact[len(word)]
            freq = {}
            for char in word:
                freq[char] = freq.get(char, 0) + 1
            for v in freq.values():
                count = count * mod_inv(fact[v]) % MOD
            ans = ans * count % MOD
        
        return ans
```

```java []
Code
class Solution {
    public int countAnagrams(String s) {
        long MOD = 1_000_000_007;
        String[] words = s.split(" ");
        int maxLen = 0;
        for (String word : words) {
            maxLen = Math.max(maxLen, word.length());
        }
        
        long[] fact = new long[maxLen + 1];
        fact[0] = 1;
        for (int i = 1; i <= maxLen; i++) {
            fact[i] = fact[i - 1] * i % MOD;
        }
        
        long ans = 1;
        for (String word : words) {
            long count = fact[word.length()];
            int[] freq = new int[26];
            for (char c : word.toCharArray()) {
                freq[c - 'a']++;
            }
            for (int f : freq) {
                if (f > 1) count = count * modInverse(fact[f], MOD) % MOD;
            }
            ans = ans * count % MOD;
        }
        return (int) ans;
    }
    
    private long modInverse(long x, long mod) {
        long res = 1, pow = mod - 2;
        while (pow > 0) {
            if ((pow & 1) == 1) res = res * x % mod;
            x = x * x % mod;
            pow >>= 1;
        }
        return res;
    }
}
```
['Hash Table', 'Math', 'String', 'Combinatorics', 'Counting', 'Python', 'C++', 'Java', 'Python3']