The goal of this exercise is to convert a string to a new string where each character in the new string is "(" if that character appears only once in the original string, or ")" if that character appears more than once in the original string. Ignore capitalization when determining if a character is a duplicate.
Examples
"Success" => ")())())"
"(( @" => "))(("
Tests
"din"
"recede"
Good luck!
This challenge comes from obnounce on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (27)
Javascript solution:
Neat!
What is this syntax at the end?
join``
I always use
.join('').
That is Tagged template literals, to save 2 character :D
I forgot all about that syntax. I've probably only used it a few times.
My Haskell solution:
Go
Python solution 🐍
Here is the simple approach with nested for loops with PHP:
In Rust – playground link (with tests)
Why not use std::str::matches to iterate over the same chars?
I suppose that is a valid alternative. Just not the first thing that came to mind ...
I just love how Rust has a helpful method for about everything in its std/core libs.
A simple (and somewhat slow) JS solution:
Not that slow :) Better than anything that searched/scanned or did an indexOf for sure! You know I like that || 1 as well, I always do (acc[val] || 0) + 1 - but that is much neater. Borrowing that.
Same to me
|| 0
Python:
TypeScript: