Skip to content

Commit

Permalink
Corrected regular expression
Browse files Browse the repository at this point in the history
  • Loading branch information
zhabinka committed Jan 8, 2019
1 parent 344838c commit 22e1857
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions javascript/run-length-encoding/run-length-encoding.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
const encode = (str) => {
const iter = (s, arr) => {
if (s.length === 0) {
return arr;
}
const regex = new RegExp(`${s[0]}+`);
const charsGroup = s.match(regex)[0];

return iter(s.substr(charsGroup.length), arr.concat(charsGroup));
};

const groups = iter(str, []);
if (str === '') {
return str;
}
const groups = str.match(/(.)\1*/g);

return groups
.map(el => (el.length === 1 ? el : `${el.length}${el[0]}`))
Expand Down

0 comments on commit 22e1857

Please sign in to comment.