Commit 4a7b4724 authored by Scott Sun's avatar Scott Sun

scott

parent eb680f48
function stringZip(iStr) {
var arr = [];
var temp = {};
var res = "";
var temp = 1;
for (var i = 0, len = iStr.length; i < len; i++) {
var element = iStr[i];
if (i === 0) {
temp[element] = 1
} else if (element === iStr[i-1]) {
temp[element]++
res = len===1? "1" + iStr[i] : ""
} else if (iStr[i] === iStr[i-1]) {
temp++
} else {
arr.push(temp)
temp = {}
temp[element] = 1
res += temp + iStr[i-1]
temp = 1
}
if(i >= len - 1){
arr.push(temp)
if(i >= len - 1 && len > 1){
res += temp + iStr[i-1]
}
}
var res = "";
arr.forEach(function(v){
for (var key in v) {
res += v[key] + key
}
})
return res
}
......@@ -36,7 +28,9 @@ function stringUnZip(iStr) {
return str
}
// console.log(stringZip("aaabccccaaaaccddecccee"));
console.log(stringZip("aaabccccaaaaccddecccee"));
console.log(stringZip("a"));
console.log(stringUnZip("3a1b6c2d3e"));
console.log(stringUnZip("1a"));
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment