Ryan Aulia

Duplicate Encoder

# Problem Solving
Problem
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.

Example:

Notes:

Assertion messages may be unclear about what they display in some languages. If you read "...It Should encode XXX", the "XXX" is the expected result, not the input!
 
Solution
The goal of this problem is to convert a given string into a new string based on the following rules:
  1. Each character in the new string will be replaced with "(" if that character appears only once in the original string.
  2. If a character appears more than once in the original string, it will be replaced with ")" in the new string.
Here are some important points to consider:
  • Capitalization differences should be ignored. This means that uppercase and lowercase letters are considered the same. So, "A" and "a" are treated as the same character.
  • Spaces or non-alphabetic characters should be ignored in the counting. Only alphabetic characters are considered.
For example, if we have the original string "recede", the resulting string will be "()()()". This is because the characters "r" and "d" appear only once, while the character "e" appears three times.
Here are a few more examples of string conversions:
  • Original string: "din", converted string: "(((" because all characters ("d", "i", and "n") appear only once.
  • Original string: "recede", converted string: "()()()" because the character "e" appears three times, while the other characters appear only once.
  • Original string: "Success", converted string: ")())())" because the character "s" appears three times, the character "c" appears twice, and the other characters appear only once.
  • Original string: "(( @", converted string: "))((" because the character "(" appears twice, while the other characters appear only once.

Step-by-step Explanation

Here are the steps in solving the "Duplicate Encoder" problem using JavaScript:
  1. Start by defining a function called
    that takes a single parameter,
    . This function will be used to convert the input string according to the given rules.
  2. Inside the
    function, convert the
    to lowercase using the
    method. Save the result in a variable called
    . This is done to ignore capitalization differences.
  3. Create an empty variable called
    to store the converted string.
  4. Begin iterating through each character in the
    string. For each character, follow these steps:
    • Take the character at index
      using
      and store it in a variable called
      .
    • Count the number of occurrences of the
      in
      by splitting the string into an array using the
      method and then obtaining the length of the resulting array using the
      property. Subtract 1 from the length because the split method will create an array with one extra element.
    • If the count is greater than 1, it means the character is a duplicate. In this case, add ")" to the
      variable.
    • If the count is equal to 1, it means the character appears only once. In this case, add "(" to the
      variable.
  5. After iterating through all the characters in
    , return the value of the
    variable as the converted string.
  6. Finally, you can call the
    function by providing the input string that you want to convert and store the result in a variable for further use or print it to the console.
Here's the complete implementation in JavaScript:
The resulting output will be printed to the console:
In this example, the string "Success" is converted to ")())())" according to the rules given in the problem.
JavaScript Playground
JavaScript
Loading...
Console