Created
March 21, 2016 18:13
-
-
Save jianminchen/8f1d0a7d1765ba5cdc83 to your computer and use it in GitHub Desktop.
Two string - Structured code, using prototype, simulated class function etc., two sliding pointers, make me laugh
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 processData(input) { | |
//Enter your code here | |
//split input into arrays | |
var inArr = input.split('\n'); | |
inArr.splice(0,1); | |
for(var i=0; i<inArr.length; i=i+2){ | |
var st = new strObj(inArr[i], inArr[i+1]); | |
st.isCommon(); | |
} | |
} | |
function strObj(str1, str2) | |
{ | |
this.str1=str1; | |
this.str2=str2; | |
} | |
strObj.prototype.isCommon = function() { | |
console.log(getCommons(this.str1, this.str2)); | |
} | |
function getCommons(s1, s2){ | |
var a = s1.split(''); | |
var b = s2.split(''); | |
a.sort(); | |
b.sort(); | |
while( a.length > 0 && b.length > 0 ) | |
{ | |
if (a[0] < b[0] ){ a.shift(); } | |
else if (a[0] > b[0] ){ b.shift(); } | |
else /* they're equal */ | |
{ | |
return 'YES'; | |
} | |
} | |
return 'NO'; | |
} | |
process.stdin.resume(); | |
process.stdin.setEncoding("ascii"); | |
_input = ""; | |
process.stdin.on("data", function (input) { | |
_input += input; | |
}); | |
process.stdin.on("end", function () { | |
processData(_input); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment