英文:
How do I sort characters of a string by appearance order?
问题
我有以下的代码:
function main() {
let input = prompt("输入一个字符串:").toLowerCase().replace(/\s/g, "");
let process = "";
for (let i = 0; i < input.length; i++) {
let word = input[i];
let repeatWord = "";
for (let j = 0; j < word.length; j++) {
repeatWord += word[j].repeat(j + 1);
}
process += repeatWord;
}
console.log(process);
}
如何按照字符首次出现的顺序对字符串进行排序?
例如,如果我输入字符串:"Learning Center",则预期结果应为:"leeearrnnigct"。
英文:
I have the following code:
function main() {
let input = prompt("Enter a string:").toLowerCase().replace(/\s/g, "");
let process = "";
for (let i = 0; i < input.length; i++) {
let word = input[i];
let repeatWord = "";
for (let j = 0; j < word.length; j++) {
repeatWord += word[j].repeat(j + 1);
}
process += repeatWord;
}
console.log(process);
}
How do I sort the characters of the string by order of first appearance?
For example, if I enter the string: "Learning Center"
, then the expected result should be "leeearrnnigct"
.
答案1
得分: 1
使用Map
来存储字符串中所有字符的计数器。然后,由于Map
保留插入顺序,您可以简单地遍历计数器,并使用每个键的值通过字符串repeat()
方法和数组join()
方法重新生成字符串。
function appearanceSort(str) {
str = str.toLowerCase().replace(/\s/g, "");
const counter = new Map();
for (const char of str) {
if (counter.has(char)) {
counter.set(char, counter.get(char) + 1);
} else {
counter.set(char, 1);
}
}
const strArr = [];
for (const [key, value] of counter) {
strArr.push(key.repeat(value));
}
return strArr.join("");
}
console.log(appearanceSort("Learning Center")); // leeearrnnigct
然而,上面的实现很可能不会按预期处理任意的Unicode字符。要支持字形簇,您可以使用诸如grapheme-splitter
或浏览器内置的Intl.Segmenter
等库,示例如下:
function appearanceSort(str) {
str = str.toLowerCase().replace(/\s/g, "");
const segments = Array.from(new Intl.Segmenter().segment(str)).map(({ segment }) => segment);
const counter = new Map();
for (const char of segments) {
if (counter.has(char)) {
counter.set(char, counter.get(char) + 1);
} else {
counter.set(char, 1);
}
}
const strArr = [];
for (const [key, value] of counter) {
strArr.push(key.repeat(value));
}
return strArr.join("");
}
console.log(appearanceSort("👨👨👧👦 Learning 👨👨👧👦 Center")); // 👨👨👧👦👨👨👧👦leeearrnnnigct
注意: Intl.Segmenter
当前不受Firefox支持。使用Intl
中的"Segmenter"
来检查浏览器支持并有条件地应用一些后备行为。
英文:
Use a Map
to store a counter of all the characters in your string. Then, since Map
preserves insertion order, you can simply iterate through the counter and use each key’s value to regenerate the string via the String repeat()
method and the Array join()
method.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function appearanceSort(str) {
str = str.toLowerCase().replace(/\s/g, "");
const counter = new Map();
for (const char of str) {
if (counter.has(char)) {
counter.set(char, counter.get(char) + 1);
} else {
counter.set(char, 1);
}
}
const strArr = [];
for (const [key, value] of counter) {
strArr.push(key.repeat(value));
}
return strArr.join("");
}
console.log(appearanceSort("Learning Center")); // leeearrnnigct
<!-- end snippet -->
However, the implementation above most likely won't work as expected with arbitrary unicode characters. To support grapheme clusters, you can use a library such as grapheme-splitter
, or the browser built-in Intl.Segmenter
, which looks like so:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function appearanceSort(str) {
str = str.toLowerCase().replace(/\s/g, "");
const segments = Array.from(new Intl.Segmenter().segment(str)).map(({
segment
}) => segment);
const counter = new Map();
for (const char of segments) {
if (counter.has(char)) {
counter.set(char, counter.get(char) + 1);
} else {
counter.set(char, 1);
}
}
const strArr = [];
for (const [key, value] of counter) {
strArr.push(key.repeat(value));
}
return strArr.join("");
}
console.log(appearanceSort("👨👨👧👦 Learning 👨👨👧👦 Center")); // 👨👨👧👦👨👨👧👦leeearrnnnigct
<!-- end snippet -->
Note: Intl.Segmenter
is currently not supported in Firefox. Use "Segmenter" in Intl
to check for browser support and conditionally apply some fallback behavior.
答案2
得分: 0
我的方法是创建一个包含所有唯一字符的数组。然后遍历它,计算每个字符有多少个兄弟,然后重复该字符出现的次数。
英文:
My approach would be to create an array with all unique characters. Then iterate over it and count how many siblings each character has and then repeat the character as often as it appears.
function main(input: string) {
const arr = input.toLocaleLowerCase().replace(/[ ]/g, '').split("");
let uniqueLetters = [...new Set(arr)]
let word = "";
for (let i = 0; i < uniqueLetters.length; i++) {
const letter = uniqueLetters[i];
const letterCount = arr.filter((l) => l == letter).length;
word += letter.repeat(letterCount)
}
return word;
}
console.log(main("Learning Center")) // prints: leeearrnnnigct
答案3
得分: 0
这是另一个版本。这个版本将保留空格并区分大小写字母:
<!-- begin snippet:js console:true -->
<!-- language:lang-js -->
const str = "Learning Center".split(""),
freq = {};
str.forEach(c => freq[c] = (freq[c]??0) + 1);
const res = str.map(c => {
let s = c.repeat(freq[c]);
freq[c] = 0;
return s
}).join("");
console.log(res);
<!-- end snippet -->
如果您希望不区分大小写且没有空格的解决方案,只需将其修改为:
<!-- begin snippet:js console:true -->
<!-- language:lang-js -->
const str = "Learning Center".toLowerCase().replaceAll(" ","").split(""),
freq = {};
str.forEach(c => freq[c] = (freq[c]??0) + 1);
const res = str.map(c => {
let s = c.repeat(freq[c]);
freq[c] = 0;
return s
}).join("");
console.log(res);
<!-- end snippet -->
英文:
Here is yet another version. This one will keep blanks and distinguish between upper/lower case letters:
<!-- begin snippet:js console:true -->
<!-- language:lang-js -->
const str="Learning Center".split(""),
freq={};
str.forEach(c=>freq[c]=(freq[c]??0)+1);
const res=str.map(c=>{
let s=c.repeat(freq[c]);
freq[c]=0;
return s
}).join("");
console.log(res);
<!-- end snippet -->
If you prefer a case insensitive solution without blanks then simply modify it to:
<!-- begin snippet:js console:true -->
<!-- language:lang-js -->
const str="Learning Center".toLowerCase().replaceAll(" ","").split(""),
freq={};
str.forEach(c=>freq[c]=(freq[c]??0)+1);
const res=str.map(c=>{
let s=c.repeat(freq[c]);
freq[c]=0;
return s
}).join("");
console.log(res);
<!-- end snippet -->
答案4
得分: -1
当你遍历字符串中的每个字符时,创建一个新的索引,然后使用第三个变量来交换字符。
你可以尝试下面的代码。
function suffle(target) {
var chars = target.split("");
var length = chars.length;
for (var i = length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = chars[i];
chars[i] = chars[j];
chars[j] = tmp;
}
return chars.join("");
}
console.log(suffle("hello"));
英文:
When you run through each characters in the string, create a new index and then use a third variable to swap the characters.
You can try the code below.
function suffle(target) {
var chars = target.split("");
var length = chars.length;
for (var i = length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = chars[i];
chars[i] = chars[j];
chars[j] = tmp;
}
return chars.join("");
}
console.log(suffle("hello"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论