Skip to content

Commit d005985

Browse files
committed
chore: update Duplicate Encoder?
1 parent dc0603a commit d005985

4 files changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: Duplicate Encoder?
3+
description: 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.
4+
keywords: [Codewars, Duplicate Encoder?, 6 kyu, Strings, Arrays, Fundamentals]
5+
tags: [Codewars, 6 kyu, Strings, Arrays, Fundamentals]
6+
---
7+
8+
# [{frontMatter.title}](https://www.codewars.com/kata/54b42f9314d9229fd6000d9c/solutions/javascript)
9+
10+
import Tabs from "@theme/Tabs";
11+
import TabItem from "@theme/TabItem";
12+
import CodeBlock from "@theme/CodeBlock";
13+
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
14+
import Solution from "!!raw-loader!./solution.js";
15+
import Description from "./_Description.md";
16+
import Examples from "./_Examples.md";
17+
18+
<DifficultyBadge kyu={6} />
19+
20+
<Tabs>
21+
<TabItem value="description" label="題目描述" default>
22+
<details open>
23+
<summary>Description</summary>
24+
<Description />
25+
<Examples />
26+
</details>
27+
</TabItem>
28+
29+
<TabItem value="solution" label="解答">
30+
## Solution
31+
<CodeBlock language="js">{Solution}</CodeBlock>
32+
</TabItem>
33+
</Tabs>
34+
35+
### 解題思路
36+
37+
練習 counter 與把題目描述轉成 JS 程式碼
38+
39+
### 心得
40+
41+
題目滿簡單但我的答案滿醜ㄉ,應該能寫得更優雅!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
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.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
```
2+
"din" => "((("
3+
"recede" => "()()()"
4+
"Success" => ")())())"
5+
"(( @" => "))(("
6+
```
7+
8+
#### Notes
9+
10+
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!
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function duplicateEncode(word) {
2+
const counter = {};
3+
word = word.toLowerCase();
4+
for (let i = 0; i < word.length; i++) {
5+
if (counter[word[i]] >= 1) {
6+
counter[word[i]] += 1;
7+
} else {
8+
counter[word[i]] = 1;
9+
}
10+
}
11+
12+
result = "";
13+
14+
for (let i = 0; i < word.length; i++) {
15+
result += counter[word[i]] === 1 ? "(" : ")";
16+
}
17+
18+
return result;
19+
}

0 commit comments

Comments
 (0)