You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
748 B
TypeScript
45 lines
748 B
TypeScript
export function duplicateEncode(word: string) {
|
|
let res = "";
|
|
let i = 0;
|
|
|
|
while (i < word.length) {
|
|
const char = word[i];
|
|
|
|
const firstIndex = word.indexOf(char)
|
|
const lastIndex = word.lastIndexOf(char)
|
|
|
|
if (firstIndex !== lastIndex) {
|
|
res += ")"
|
|
} else {
|
|
res += "("
|
|
}
|
|
|
|
i++
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
export function duplicateEncode1(word: string) {
|
|
return word
|
|
.toLowerCase()
|
|
.split('')
|
|
.map((a, _, w) => {
|
|
return w.indexOf(a) == w.lastIndexOf(a) ? '(' : ')'
|
|
})
|
|
.join('')
|
|
}
|
|
|
|
console.log(
|
|
duplicateEncode("din"), "((("
|
|
)
|
|
console.log(
|
|
duplicateEncode("recede"), "()()()"
|
|
)
|
|
console.log(
|
|
duplicateEncode("Success"), ")())())"
|
|
)
|
|
console.log(
|
|
duplicateEncode("(( @"), "))(("
|
|
)
|