Advertisement
Guest User

Untitled

a guest
May 30th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. ```js
  2. function numberOfAnagrams(S) {
  3.  
  4. // S.length!/repeat!
  5.  
  6. var total = factorial(S.length),
  7. repeat = 1,
  8. array = S.split('').sort();
  9.  
  10. for (var i=1;i<array.length;i++) {
  11. if (array[i] == array[i-1]) {
  12. repeat+=1
  13. } else {
  14. total = total / factorial(repeat);
  15. repeat = 1
  16. }
  17. }
  18.  
  19.  
  20. return total/factorial(repeat)
  21. }
  22.  
  23. function factorial(num)
  24. {
  25. // If the number is less than 0, reject it.
  26. if (num < 0) {
  27. return -1;
  28. }
  29. // If the number is 0, its factorial is 1.
  30. else if (num == 0) {
  31. return 1;
  32. }
  33. var tmp = num;
  34. while (num-- > 2) {
  35. tmp *= num;
  36. }
  37. return tmp;
  38. }
  39.  
  40. // factorials
  41. // f = function(n) {
  42. // return n ? n * f(n - 1) : 1
  43. //}
  44. ```
  45.  
  46. 140 char
  47. ```js
  48. f = function(n) {
  49. return n ? n * f(n - 1) : 1
  50. }
  51. S = arguments[0]
  52. t = f(S.length)
  53. i=r = 1
  54. a = S.split('').sort()
  55.  
  56. for (;S[i];)
  57. a[i-1] == a[i++] ? r++ : (t /= f(r), r=1)
  58.  
  59. return t/f(r)
  60. ```
  61. my answer is so stupid after reviewing other's work
  62. ```js
  63. r = 1
  64. v = {}
  65. n = 0
  66. for (;l = arguments[0][n];) {
  67. v[l] = v[l] || 0
  68. r *= ++n
  69. r /= ++v[l]
  70. }
  71.  
  72. return r
  73. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement