Guest User

Untitled

a guest
Jul 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. function isPermutation(string) {
  12. let charHash = buildCharFrequencyHash(string);
  13. return checkMaxOneOdd(charHash);
  14. }
  15.  
  16. function buildCharFrequencyHash(string){
  17. let charHash = [];
  18.  
  19. for (let i = 0; i < string.length; i++) {
  20. let char = string.charAt(i);
  21.  
  22. if(char === ' ') {
  23. continue;
  24. }
  25.  
  26. if (charHash[char]) {
  27. charHash[char]++;
  28. } else {
  29. charHash[char] = 1;
  30. }
  31. }
  32.  
  33. return charHash;
  34. }
  35.  
  36. function checkMaxOneOdd(charHash) {
  37. let foundOdd = false;
  38. for (let key in charHash) {
  39. if (charHash[key] % 2 == 1) {
  40. if (foundOdd) {
  41. return false;
  42. }
  43.  
  44. foundOdd = true;
  45. }
  46. }
  47. return true;
  48. }
  49.  
  50. console.log(isPermutation('taco cat'))
  51. </script>
  52.  
  53.  
  54.  
  55. <script id="jsbin-source-javascript" type="text/javascript">function isPermutation(string) {
  56. let charHash = buildCharFrequencyHash(string);
  57. return checkMaxOneOdd(charHash);
  58. }
  59.  
  60. function buildCharFrequencyHash(string){
  61. let charHash = [];
  62.  
  63. for (let i = 0; i < string.length; i++) {
  64. let char = string.charAt(i);
  65.  
  66. if(char === ' ') {
  67. continue;
  68. }
  69.  
  70. if (charHash[char]) {
  71. charHash[char]++;
  72. } else {
  73. charHash[char] = 1;
  74. }
  75. }
  76.  
  77. return charHash;
  78. }
  79.  
  80. function checkMaxOneOdd(charHash) {
  81. let foundOdd = false;
  82. for (let key in charHash) {
  83. if (charHash[key] % 2 == 1) {
  84. if (foundOdd) {
  85. return false;
  86. }
  87.  
  88. foundOdd = true;
  89. }
  90. }
  91. return true;
  92. }
  93.  
  94. console.log(isPermutation('taco cat'))
  95. </script></body>
  96. </html>
Add Comment
Please, Sign In to add comment