描述:
本次练习的目标是将一个字符串转化成一个新字符串,若字符串的字符只出现一次,新字符串的字符为”(“,若出现多次,新字符串的字符为”)”
例如:
“din” => “(((“
“recede” => “()()()”
“Success” => “)())())”
“(( @” => “))((“
MyCode:
using System.Linq;
public class Kata
{
public static string DuplicateEncode(string word)
{
string newWord = word.ToLower();
string retStr = "";
for (int i = 0; i < newWord.Length; i++)
{
if (newWord.Count(x => x == newWord[i]) > 1)
retStr += ")";
else
retStr += "(";
}
return retStr;
}
}
CodeWar:
using System.Linq;
public class Kata
{
public static string DuplicateEncode(string word)
{
return new string(word.ToLower().Select(ch => word.ToLower().Count(innerCh => ch == innerCh) == 1 ? '(' : ')').ToArray());
}
}