简单有趣#135:缺少字母【难度:2级】:
答案1:
namespace myjinxin
{
using System. Linq;
public class Kata
{
public string MissingAlphabets ( string s) {
var alphabet = "abcdefghijklmnopqrstuvwxyz" ;
var letterOccurencesCount = alphabet. ToDictionary ( l = > l, l = > s. Count ( x = > x == l) ) ;
var alphabetsCount = letterOccurencesCount. Max ( x = > x. Value) ;
return string . Concat ( alphabet. SelectMany ( l = > Enumerable. Repeat ( l, alphabetsCount - letterOccurencesCount[ l] ) ) ) ;
}
}
}
答案2:
namespace myjinxin
{
using System. Linq;
using System. Text;
public class Kata
{
public string MissingAlphabets ( string s) {
var letters = new int [ 26 ] ;
foreach ( var c in s ) {
letters[ c - 97 ] ++ ;
}
var result = new StringBuilder ( ) ;
var max = letters. Max ( ) ;
for ( var i = 0 ; i < letters. Length; i++ ) {
result. Append ( ( char ) ( i + 97 ) , max - letters[ i ] ) ;
}
return result. ToString ( ) ;
}
}
}
答案3:
using System;
using System. Linq;
using System. Collections. Generic;
namespace myjinxin
{
public class Kata
{
private const string letters = "abcdefghijklmnopqrstuvwxyz" ;
public string MissingAlphabets ( string s)
{
var map = ( letters + s) . GroupBy ( c = > c) . ToDictionary ( g = > g. Key, g = > g. Count ( ) - 1 ) ;
var max = map. Values. Max ( ) ;
return new string ( map. Where ( v = > v. Value < max) . SelectMany ( v = > string . Empty. PadLeft ( max - v. Value, v. Key) ) . OrderBy ( c = > c) . ToArray ( ) ) ;
}
}
}
答案4:
namespace myjinxin
{
using System;
using System. Linq;
using System. Collections. Generic;
using System. Text. RegularExpressions;
public class Kata
{
public string MissingAlphabets ( string s) {
var r= new Dictionary < char , int > ( ) ;
int m= 0 ;
for ( int i= 0 ; i< s. Length; i++ ) {
if ( r. ContainsKey ( s[ i] ) ) r[ s[ i] ] ++ ;
else r. Add ( s[ i] , 1 ) ;
m= r[ s[ i] ] > m? r[ s[ i] ] : m;
}
var rs= "" ;
var t= 'a' ;
for ( int c= 97 ; c<= 122 ; c++ ) {
t= ( char ) c;
rs+ = repeat ( "" + t, m- ( r. ContainsKey ( t) ? r[ t] : 0 ) ) ;
}
return rs;
}
public string repeat ( string c, int n) {
var s= new string ( '&' , n) ;
return Regex. Replace ( s, "&" , c) ;
}
}
}
答案5:
namespace myjinxin
{
using System;
using System. Linq;
public class Kata
{
public string MissingAlphabets ( string s) {
var alph = Enumerable. Range ( 0 , 26 ) . Select ( i= > ( char ) ( 'a' + i) ) ;
var alphCnt = alph. Select ( x= > s. Where ( c= > c== x) . Count ( ) ) . ToArray ( ) ;
var max = alphCnt. Max ( ) ;
var chs = alph. Select ( ( x, i) = > max == alphCnt[ i] ? "" :
string . Concat ( Enumerable. Repeat ( x, max - alphCnt[ i] ) )
) ;
return string . Concat ( chs) ;
}
}
}
答案6:
using System. Linq;
using System;
public class Kata
{
public string MissingAlphabets ( string s)
{
var lc = Enumerable. Range ( 'a' , 26 ) . Select ( i = > s. Count ( c = > c == i) ) ;
return string . Concat ( lc. SelectMany ( ( n, i) = > new String ( ( char ) ( 'a' + i) , lc. Max ( ) - n) ) ) ;
}
}
答案7:
namespace myjinxin
{
using System;
public class Kata
{
public string MissingAlphabets ( string s) {
string q, al;
string alfa= "abcdefghijklmnopqrstuvwxyz" ;
int a, b;
a= 0 ;
b= 0 ;
q= "" ;
al= "" ;
char [ ] qq= s. ToCharArray ( ) ;
for ( int e= 0 ; e< qq. Length; e++ )
{
for ( int i= 0 ; i< qq. Length- 1 ; i++ )
{
if ( ( int ) qq[ i] > ( int ) qq[ i+ 1 ] )
{
a= qq[ i] ;
qq[ i] = qq[ i+ 1 ] ;
qq[ i+ 1 ] = ( char ) a;
}
}
}
for ( int i= 0 ; i< qq. Length; i++ )
{
a= 0 ;
for ( int e= 0 ; e< qq. Length; e++ )
{
if ( qq[ i] == qq[ e] )
{
a++ ;
}
}
if ( a> b)
{
b= a;
}
}
a= 0 ;
for ( int i= 0 ; i< alfa. Length; i++ )
{
for ( int e= 0 ; e< qq. Length; e++ )
{
if ( alfa[ i] != qq[ e] )
{
a++ ;
}
else
{
a= 0 ;
}
}
if ( a== qq. Length)
{
al= al+ alfa[ i] ;
}
a= 0 ;
}
a= b;
for ( int i= 0 ; i< qq. Length- 1 ; i++ )
{
if ( qq[ i] != qq[ i+ 1 ] )
{
for ( int e= 0 ; e< a- 1 ; e++ )
{
q= q+ qq[ i] ;
}
a= b;
}
else
{
a-- ;
}
}
a= 0 ;
for ( int i= 0 ; i< qq. Length; i++ )
{
if ( qq[ i] == qq[ qq. Length- 1 ] )
{
a++ ;
}
}
for ( int i= 0 ; i< b- a; i++ )
{
q= q+ qq[ qq. Length- 1 ] ;
}
string yh= "" ;
for ( int i= 0 ; i< al. Length; i++ )
{
for ( int e= 0 ; e< b; e++ )
{
yh= yh+ al[ i] ;
}
}
q= q+ yh;
qq= q. ToCharArray ( ) ;
for ( int e= 0 ; e< qq. Length; e++ )
{
for ( int i= 0 ; i< qq. Length- 1 ; i++ )
{
if ( ( int ) qq[ i] > ( int ) qq[ i+ 1 ] )
{
a= qq[ i] ;
qq[ i] = qq[ i+ 1 ] ;
qq[ i+ 1 ] = ( char ) a;
}
}
}
string oppo= "" ;
for ( int i= 0 ; i< qq. Length; i++ )
{
oppo+ = ( qq[ i] ) ;
}
return oppo;
}
}
}
答案8:
namespace myjinxin
{
using System;
using System. Collections. Generic;
using System. Linq;
using System. Text;
public class Kata
{
public string MissingAlphabets ( string s)
{
var letters = new Dictionary < char , int > ( capacity: 26 ) ;
foreach ( char c in s)
{
if ( letters. ContainsKey ( c) )
{
letters[ c] ++ ;
}
else
{
letters. Add ( c, 1 ) ;
}
}
int max_letter_count = letters. Values. Max ( ) ;
var missing_letters = new StringBuilder ( capacity: ( max_letter_count * 26 ) - s. Length) ;
for ( char c = 'a' ; c <= 'z' ; c++ )
{
missing_letters. Append ( c, letters. ContainsKey ( c) ? max_letter_count - letters[ c] : max_letter_count) ;
}
return missing_letters. ToString ( ) ;
}
}
}
答案9:
namespace myjinxin
{
using System;
using System. Collections. Generic;
using System. Linq;
public class Kata
{
public string MissingAlphabets ( string s)
{
string a = "abcdefghijklmnopqrstuvwxyz" ;
a + = s;
var literki = a. GroupBy ( x = > x) ;
int maks = literki. Max ( x = > x. Count ( ) ) ;
Dictionary< char , int > zbiornik = literki. ToDictionary ( x = > x. Key, y = > y. Count ( ) ) ;
string res = "" ;
foreach ( KeyValuePair< char , int > ele in zbiornik)
{
res + = new string ( ele. Key, maks - ele. Value) ;
}
return res;
}
}
}
答案10:
using System;
using System. Linq;
namespace myjinxin
{
using System;
public class Kata
{
public string MissingAlphabets ( string s) {
var alphabet = "abcdefghijklmnopqrstuvwxyz" ;
var repeatedAlphabet = s
. GroupBy ( c = > c)
. Select ( c = > new { Char = c. Key, Count = c. Count ( ) } ) ;
var max = repeatedAlphabet. Max ( x = > x. Count) ;
var str = string . Empty;
foreach ( var c in alphabet)
{
if ( repeatedAlphabet. Count ( x = > x. Char == c) == 0 )
str + = new string ( c, max) ;
else
str + = new string ( c, max - repeatedAlphabet. Where ( x = > x. Char == c) . First ( ) . Count) ;
}
return str;
}
}
}