[デイリーコーディング]複数の文字列を連結/ソート/重複削除して返す
今日の問題: 複数の文字列を連結/ソート/重複削除して返せ
回答
using System;
using System.Linq;
using static System.Console;
namespace demo
{
class Program
{
static void Main(string[] args)
{
WriteLine(Longest("aretheyhere", "yestheyarehere")); // "aehrsty"
}
public static string Longest(string s1, string s2)
{
string str = s1 + s2; // 連結
char[] uniq_str = str.ToCharArray().Distinct().ToArray(); // 配列に変換して重複削除
Array.Sort(uniq_str); // ソート
string result = String.Join("", uniq_str); // 文字列に再変換
return result;
}
}
}
結果
aehrsty
回答
using System;
using System.Linq;
using static System.Console;
namespace demo
{
class Program
{
static void Main(string[] args)
{
WriteLine(Longest("aretheyhere", "yestheyarehere")); // "aehrsty"
}
public static string Longest(string s1, string s2)
{
string str = s1 + s2; // 連結
char[] uniq_str = str.ToCharArray().Distinct().ToArray(); // 配列に変換して重複削除
Array.Sort(uniq_str); // ソート
string result = String.Join("", uniq_str); // 文字列に再変換
return result;
}
}
}
結果
aehrsty
ベターな回答:
回答
public static string Longest(string s1, string s2)
{
return new string((s1 + s2).Distinct().OrderBy(x => x).ToArray());
}
参照: Two to One (Codewars)
Author And Source
この問題について([デイリーコーディング]複数の文字列を連結/ソート/重複削除して返す), 我々は、より多くの情報をここで見つけました https://qiita.com/tomokitointhe6ix/items/4959278013ef7ee44975著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .