Created
July 28, 2016 01:34
-
-
Save quangnd/e36b22fa6cfe1cbabed04ad6162db27f to your computer and use it in GitHub Desktop.
Find new sorted string and remove duplicate char in string
This file contains hidden or 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
//Take 2 strings s1 and s2 including only letters from ato z. Return a new sorted string, the longest possible, containing distinct letters, - each taken only once - coming from s1 or s2. | |
/* | |
Examples: | |
a = "xyaabbbccccdefww" | |
b = "xxxxyyyyabklmopq" | |
longest(a, b) -> "abcdefklmopqwxy" | |
a = "abcdefghijklmnopqrstuvwxyz" | |
longest(a, a) -> "abcdefghijklmnopqrstuvwxyz" | |
*/ | |
function longest(s1, s2) { | |
var newStr = s1.concat(s2); | |
var longestCharArr = unique(newStr).split("").sort(); | |
return longestCharArr.join(""); | |
} | |
function unique(str) { | |
var result = ""; | |
for(var i = 0; i < str.length; i++) { | |
if(result.indexOf(str[i]) == -1) { | |
result += str[i]; | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment