JavaScript(ECMAScript 6)-148 139 135文字
バージョン2:
配列内包表記を使用するように更新:
[a[i][0]for(i in a=[].concat(...s.split('\n').map(x=>x.split(/ */).sort().map((x,i,a)=>x+(a[i-1]==x?++j:j=0)))).sort())if(a[i-1]<a[i])]
バージョン1:
[].concat(...s.split('\n').map(x=>x.split(/ */).sort().map((x,i,a)=>x+(a[i-1]==x?++j:j=0)))).sort().filter((x,i,a)=>a[i-1]!=x).map(x=>x[0])
次のことを想定しています:
- 入力文字列は変数にあります
s
;
- 入力の大文字小文字を無視することができます(質問で指定されたとおり-つまり、すべて大文字または小文字のいずれかです)。
- 出力は文字の配列です(これは、JavaScriptがOPの文字リストの要件に到達できる程度に近い)。そして
- 出力はコンソールに表示されます。
コメント付き:
var l = s.split('\n') // split the input up into sentences
.map(x=>x.split(/ */) // split each sentence up into letters ignoring any
// whitespace
.sort() // sort the letters in each sentence alphabetically
.map((x,i,a)=>x+(a[i-1]==x?++j:j=0)))
// append the frequency of previously occurring identical
// letters in the same sentence to each letter.
// I.e. "HELLO WORLD" =>
// ["D0","E0","H0","L0","L1","L2","O0","O1","R0","W0"]
[].concat(...l) // Flatten the array of arrays of letters+frequencies
// into a single array.
.sort() // Sort all the letters and appended frequencies
// alphabetically.
.filter((x,i,a)=>a[i-1]!=x) // Remove duplicates and return the sorted
.map(x=>x[0]) // Get the first letter of each entry (removing the
// frequencies) and return the array.
あなたがしたい場合は:
- 文字列として返し
.join('')
、最後に追加します。
- ユーザーからの入力を受け取り、
s
変数をprompt()
;に置き換えます。または
- 関数として記述してから、先頭に
f
追加f=s=>
します。
ランニング:
s="HELLO\nI LOVE CAT\nI LOVE DOG\nI LOVE MOMMY\nMOMMY LOVE DADDY";
[].concat(...s.split('\n').map(x=>x.split(/ */).sort().map((x,i,a)=>x+(a[i-1]==x?++j:j=0)))).sort().filter((x,i,a)=>a[i-1]!=x).map(x=>x[0])
出力を与えます:
["A","C","D","D","D","E","G","H","I","L","L","M","M","M","O","O","T","V","Y","Y"]
v
出力に手紙があるはずです;)