Skip to content

Instantly share code, notes, and snippets.

@hzhu
Last active February 26, 2017 07:46
Show Gist options
  • Select an option

  • Save hzhu/4087a121d39b8b3ed76548671c7016f2 to your computer and use it in GitHub Desktop.

Select an option

Save hzhu/4087a121d39b8b3ed76548671c7016f2 to your computer and use it in GitHub Desktop.

Revisions

  1. hzhu revised this gist Feb 25, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion dedup_neighbors.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ reduce_dups('aba') => "aba"
    ```

    ### Analysis of Problem
    We can solve this problem by `for` looping through the given string starting at index 1. We then compare the current character `str[i]`, to the previous character `prev`.
    We can solve this problem by `for` looping through the string starting at index 1. Then compare the current character `str[i]`, to the previous character `prev`.

    When we find a set of duplicate touching characters `(str[i] === prev)`, we can concat all the characters *before* the dups, and all the characters *after* the dups, creating a new string.

  2. hzhu revised this gist Feb 25, 2017. 1 changed file with 10 additions and 16 deletions.
    26 changes: 10 additions & 16 deletions dedup_neighbors.md
    Original file line number Diff line number Diff line change
    @@ -16,27 +16,21 @@ Finally, we reset our variables: `str`, `i`, `prev`. This allows our program to

    ### Solution Code
    ```
    function reduce_dups(str) {
    let prev = str[0]
    function remove_dups(str) {
    let prev = str[0];
    for (let i = 1; i < str.length; i++) {
    if (str[i] === prev) {
    let before_dups = str.slice(0, i - 1)
    let after_dups = str.slice(i + 1, str.length)
    str = before_dups + after_dups
    i = -1
    prev = undefined
    str = str.slice(0, i - 1) + str.slice(i + 1, str.length);
    prev = str[0];
    i = 0;
    } else {
    prev = str[i]
    prev = str[i];
    }
    }
    return str
    return str;
    }
    reduce_dups('abba') // ''
    reduce_dups('aab') // 'b'
    reduce_dups('aba') // 'aba'
    remove_dups('abba') // ''
    remove_dups('aab') // 'b'
    remove_dups('aba') // 'aba'
    ```
  3. hzhu revised this gist Feb 17, 2017. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions dedup_neighbors.md
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,11 @@ reduce_dups('aba') => "aba"
    ```

    ### Analysis of Problem
    We can solve this problem by using a loop to find duplicate touching characters.
    When we find a set of duplicate touching characters, we can concat all the characters *before* the dups, and all the characters *after* the dups, creating a new string.
    Then we reset our variables to run a new loop, on our newly created string, repeating the steps above.
    We can solve this problem by `for` looping through the given string starting at index 1. We then compare the current character `str[i]`, to the previous character `prev`.

    When we find a set of duplicate touching characters `(str[i] === prev)`, we can concat all the characters *before* the dups, and all the characters *after* the dups, creating a new string.

    Finally, we reset our variables: `str`, `i`, `prev`. This allows our program to run a new loop, on our newly created string, repeating the steps above.

    ### Solution Code
    ```
  4. hzhu revised this gist Feb 17, 2017. 1 changed file with 5 additions and 7 deletions.
    12 changes: 5 additions & 7 deletions dedup_neighbors.md
    Original file line number Diff line number Diff line change
    @@ -15,15 +15,13 @@ Then we reset our variables to run a new loop, on our newly created string, repe
    ### Solution Code
    ```
    function reduce_dups(str) {
    let prev = undefined
    for (let i = 0; i < str.length; i++) {
    if (!prev) {
    prev = str[i]
    } else if (str[i] === prev) {
    let prev = str[0]
    for (let i = 1; i < str.length; i++) {
    if (str[i] === prev) {
    let before_dups = str.slice(0, i - 1)
    let after_dups = str.slice(i + 1, str.length)
    str = before_dups + after_dups
    i = -1
    prev = undefined
  5. hzhu revised this gist Feb 17, 2017. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions dedup_neighbors.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,12 @@
    ### Problem
    Write a function that accepts a string argument and removes duplicate characters that are neighbors. If the new string also has duplicate neighbors, those should be removed also.

    ```
    reduce_dups('abb') => "a"
    reduce_dups('abba') => ""
    reduce_dups('aba') => "aba"
    ```

    ### Analysis of Problem
    We can solve this problem by using a loop to find duplicate touching characters.
    When we find a set of duplicate touching characters, we can concat all the characters *before* the dups, and all the characters *after* the dups, creating a new string.
  6. hzhu revised this gist Feb 17, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion dedup_neighbors.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ### Analysis of Problem
    We can solve this problem by using a loop to find duplicate touching characters.
    When we find a set of duplicate touching characters, we can concat all the characters *before* the dups, and all the characters after the *dups*, creating a new string.
    When we find a set of duplicate touching characters, we can concat all the characters *before* the dups, and all the characters *after* the dups, creating a new string.
    Then we reset our variables to run a new loop, on our newly created string, repeating the steps above.

    ### Solution Code
  7. hzhu created this gist Feb 17, 2017.
    33 changes: 33 additions & 0 deletions dedup_neighbors.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    ### Analysis of Problem
    We can solve this problem by using a loop to find duplicate touching characters.
    When we find a set of duplicate touching characters, we can concat all the characters *before* the dups, and all the characters after the *dups*, creating a new string.
    Then we reset our variables to run a new loop, on our newly created string, repeating the steps above.

    ### Solution Code
    ```
    function reduce_dups(str) {
    let prev = undefined
    for (let i = 0; i < str.length; i++) {
    if (!prev) {
    prev = str[i]
    } else if (str[i] === prev) {
    let before_dups = str.slice(0, i - 1)
    let after_dups = str.slice(i + 1, str.length)
    str = before_dups + after_dups
    i = -1
    prev = undefined
    } else {
    prev = str[i]
    }
    }
    return str
    }
    reduce_dups('abba') // ''
    reduce_dups('aab') // 'b'
    reduce_dups('aba') // 'aba'
    ```