与えられた文字列のランレングス エンコーディング用のコードを書く
サンプル入力: aaaaaaaaaaabcccccc
出力: a10bc6
私のコード:
static void Main(string[] args)
{
string str = "aaaaaaaaaabcccccc";
var qry = (from c in str
group c by c into grp
select new
{
output = grp.Key.ToString() + grp.Count().ToString()
});
StringBuilder sb = new StringBuilder();
foreach (var item in qry)
{
sb.Append(item.output);
}
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
ただし、次のように返されます。
a10b1c6
非繰り返し文字のカウントを削除したいのですが、ここでは文字「b」の「1」です。
ソートされた文字列であると仮定します。