Skip to content

Latest commit

 

History

History
28 lines (23 loc) · 698 Bytes

File metadata and controls

28 lines (23 loc) · 698 Bytes

Given a string s, return a new string where consecutive duplicate characters are replaced with that character, followed by its repetition count.

Input: “aaabcccdd”
Output: “a3bc3d2”

Input: "aabbaabb"
Output: "a2b2a2b2"

Input: ""
Output: ""

function consecutiveDuplicate(strs) {
    let new_str = strs.split(""); // gives array of type string
    let count = 1;
    let result = "";

    for(let i = 0; i < new_str.length; i++) {
        if(new_str[i] === new_str[i + 1]) {
            count++;
        } else {
            result += new_str[i] + count;
            count = 1;
        }
    }
    return result
}