Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                /* Problem - Check Permutation: Given two strings, write a method to decide if one is a permutation of the other.

Key points to solve this problem
A> It is assumed that the characters are stored using 8 bit and there can be 256 possible characters.

B> We will create a Hashmap with the charCodeAt number of the characters of the first string as keys and the number of occurances of the character as value; So, at the first occurance of the character increment, the value of the key by 1 and at the second occurance increment the value by another 1. Result, for each character, the no of occurance will be equal to that key's value' All the rest of the values of the Hashmap will be filled with zeros.

Then go through the second string and for each character, look up the hash table for that key-value pair. And decrement the value by one, when that key is found the first time, and by another one, if the character occurs twice. If the two strings are permutations then they should have the same unique characters each with the same number of occurrences.

So, none of the second for loop's if condition can have a negative value for a key, if the 2 strings are permutation of each other.

C> Create a hash-table of size 256 (i.e. 256 elements) and initialize all values with 0's.

D> With the for loop, for each character in str1, I am setting its "UTF-16 code unit" value (with charCodeAt method) as the key (incex position) for that hash_table.

E> Compare the occurrence of each character, using the char value as index into an array of counts.

Time Complexity of this solution : O(n) and  Space Complexity: O(1)
*/

function isPermutation(str1, str2) {
  if (str1.length !== str2.length) {
    return false;
  } 

  var hash_table_array = Array.apply(null, Array(256)).map(Number.prototype.valueOf, 0); 

  for (var i = 0; i < str1.length; i++) {
    hash_table_array[str1[i].charCodeAt(0)] += 1;
  }

  for (var i = 0; i < str2.length; i++) {
    if ((hash_table_array[str2[i].charCodeAt(0)] -= 1) < 0) {
      return false;
    }
  }
  return true;
};

module.exports = isPermutation;

// console.log(isPermutation('asdf', 'fdsa')); // returns true
// console.log(isPermutation('asdf', 'fdsaa')); // returns false
// console.log(isPermutation('asdf', 'fdsf')); // returns false
              
            
!
999px

Console