Skip to content

Instantly share code, notes, and snippets.

@jeffschwartz
Created November 15, 2012 16:17
Show Gist options
  • Save jeffschwartz/4079460 to your computer and use it in GitHub Desktop.
Save jeffschwartz/4079460 to your computer and use it in GitHub Desktop.
My implementation of String Permutation in response to the coding challenge found @ http://java.dzone.com/articles/thursday-code-puzzler-string-0#comment-90277
/**
* Created with JetBrains WebStorm.
* User: jeffrey
* Date: 11/14/12
* Time: 3:53 PM
* To change this template use File | Settings | File Templates.
*/
var perm = function(window, undefined) {
"use strict";
// var s = 'abcdefghijklmnopqrstuvwxyz';
var s = 'abccba',
i,
ii,
len,
temps = '',
list = [],
result;
/*
* Make one pass of the string for each one of its characters, bypassing the current character.
*/
for(i = 0, len = s.length; i < len; i += 1) {
for(ii = 0; ii < len; ii += 1) {
if(s[ii] !== s[i]) {
temps = s[i] + s[ii];
list.push(temps);
console.log(temps);
temps = '';
}
}
}
console.log(list);
result = new JS.SortedSet(list);
result.forEach(function(x) {
console.log(x);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment