Last active
October 31, 2020 14:37
-
-
Save remi-bruguier/2c728e25e7dac52b0678605446680bcf to your computer and use it in GitHub Desktop.
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
/** | |
* Given a string, we want to remove 2 adjacent characters that are the same, and repeat the process with the new string until we can no longer perform the operation. | |
* | |
* @param base the original string | |
* | |
* @returns the `cleansed` string | |
*/ | |
const remove_adjacent_dup = (base:string): string => { | |
const adjacentDupRegex = new RegExp(/(\w)\1/g); | |
while(adjacentDupRegex.test(base)){ | |
base = base.replace(adjacentDupRegex,""); | |
} | |
return base; | |
}; | |
export default remove_adjacent_dup; | |
// ---------------------------------------------- | |
import remove_adjacent_dup from './base'; | |
describe('remove_adjacent_dup', () => { | |
it('should transform aa into an empty string', () => { | |
expect(remove_adjacent_dup('aa')).toEqual(''); | |
}); | |
it('should transform baa into b', () => { | |
expect(remove_adjacent_dup('baa')).toEqual('b'); | |
}); | |
it('should transform cabba into c', () => { | |
expect(remove_adjacent_dup('cabba')).toEqual('c'); | |
}); | |
it('should transform abbaca into ca', () => { | |
expect(remove_adjacent_dup('abbaca')).toEqual('ca'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment