Created
April 17, 2016 17:59
-
-
Save mattcollier/a3064f8328c9cf3e6c6c770e790c2e28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mix(s1, s2) { | |
const LETTERS = 'abcdefghijklmnopqrstuvwxyz'; | |
var letterMap = {}; | |
var letters = []; | |
//console.log(mapString(string1)); | |
var map1 = mapString(s1.replace(/[^a-z]/g, '')); | |
var map2 = mapString(s2.replace(/[^a-z]/g, '')); | |
LETTERS.split('').forEach(function(e) { | |
if(e in map1 && e in map2) { | |
if(map1[e] > 1 && map1[e] > map2[e]) { | |
return letters.push('1:' + printChar(e, map1[e])); | |
} | |
if(map2[e] > 1 && map1[e] < map2[e]) { | |
//return letterMap[e] = '2:' + printChar(e, map2[e]); | |
return letters.push('2:' + printChar(e, map2[e])); | |
} | |
if(map1[e] > 1) { | |
letters.push('=:' + printChar(e, map1[e])); | |
} | |
} else if(e in map1 && map1[e] > 1) { | |
letters.push('1:' + printChar(e, map1[e])); | |
} else if(e in map2 && map2[e] > 1) { | |
letters.push('2:' + printChar(e, map2[e])); | |
} | |
}); | |
letters.sort(function(a, b) { | |
if(a.length < b.length) { | |
return 1; | |
} | |
if(a.length > b.length) { | |
return -1; | |
} | |
if(a < b) { | |
return -1; | |
} | |
if(a > b) { | |
return 1; | |
} | |
return 0; | |
}); | |
//console.log(letters); | |
return letters.join('/'); | |
} | |
function printChar(char, num) { | |
var string = ''; | |
for(var i = 0; i < num; i++) { | |
string += char; | |
} | |
return string; | |
} | |
function mapString(str) { | |
var map = {}; | |
str.split('').forEach(function(c) { | |
if(c in map) { | |
return map[c]++; | |
} | |
map[c] = 1; | |
}); | |
return map; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment