Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```js
- function numberOfAnagrams(S) {
- // S.length!/repeat!
- var total = factorial(S.length),
- repeat = 1,
- array = S.split('').sort();
- for (var i=1;i<array.length;i++) {
- if (array[i] == array[i-1]) {
- repeat+=1
- } else {
- total = total / factorial(repeat);
- repeat = 1
- }
- }
- return total/factorial(repeat)
- }
- function factorial(num)
- {
- // If the number is less than 0, reject it.
- if (num < 0) {
- return -1;
- }
- // If the number is 0, its factorial is 1.
- else if (num == 0) {
- return 1;
- }
- var tmp = num;
- while (num-- > 2) {
- tmp *= num;
- }
- return tmp;
- }
- // factorials
- // f = function(n) {
- // return n ? n * f(n - 1) : 1
- //}
- ```
- 140 char
- ```js
- f = function(n) {
- return n ? n * f(n - 1) : 1
- }
- S = arguments[0]
- t = f(S.length)
- i=r = 1
- a = S.split('').sort()
- for (;S[i];)
- a[i-1] == a[i++] ? r++ : (t /= f(r), r=1)
- return t/f(r)
- ```
- my answer is so stupid after reviewing other's work
- ```js
- r = 1
- v = {}
- n = 0
- for (;l = arguments[0][n];) {
- v[l] = v[l] || 0
- r *= ++n
- r /= ++v[l]
- }
- return r
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement