C#练习题答案: 倒计时 - 最长的单词【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

mac2025-03-06  15

倒计时 - 最长的单词【难度:2级】:

答案1:

using System.Linq; public partial class Kata { public static string[] LongestWord(string letters) { var matches = words.OrderByDescending(w => w.Length) .Where(w => w.All(l => letters.Contains(l))) .Where(w => w.Distinct() .Where(l => w.Count(c => c == l) > letters.Count(c => c == l)) .Count() == 0 ); return matches.Count() == 0 ? null : matches.TakeWhile(w => w.Length == matches.First().Length).ToArray(); } }

答案2:

using System.Linq; public partial class Kata { public static string[] LongestWord(string letters) { var res = words .Where(word => word.All(letters.Contains)) .Where(word => word.All(l => letters.Count(c => c == l) >= word.Count(c => c == l))) .OrderByDescending(w => w.Length); return res.Any() ? res.TakeWhile(x => x.Length == res.First().Length).ToArray() : null; } }

答案3:

using System.Linq; using System.Collections.Generic; public partial class Kata { public static string[] LongestWord(string letters) { int longest = 0; var answer = new List<string>(); foreach (var w in words) { if (w.Length >= longest &amp;&amp; w.All(c => letters.Count(x => x == c) >= w.Count(x => x == c))) { if (w.Length > longest) { answer.Clear(); longest = w.Length; } answer.Add(w); } } return answer.Count == 0 ? null : answer.OrderBy(x=>x).ToArray(); } }

答案4:

using System; using System.Linq; using System.Collections.Generic; public partial class Kata { public static string[] LongestWord(string letters) { var givenWordDict = letters.GroupBy( c => c) .ToDictionary( gr => gr.Key, gr => gr.Count()); var containWords = words.Select(word => Tuple.Create( word, word.GroupBy( c => c) .ToDictionary( gr => gr.Key, gr => gr.Count()))) .Where( pair => pair.Item2.Compare(givenWordDict)) .Select( pair => pair.Item1) .ToArray(); var maxLength = containWords.DefaultIfEmpty("") .Max( word => word.Length); return (maxLength == 0)? null : containWords.Where( word => word.Length == maxLength).OrderBy( x => x).ToArray() ; } } public static class LinqExtensions { public static bool Compare(this Dictionary<char, int> source, Dictionary<char, int> destination) { return source.All ( pair => destination.ContainsKey(pair.Key) &amp;&amp; pair.Value <= destination[pair.Key]); } }

答案5:

using System.Linq; using System.Collections.Generic; public partial class Kata { public static string[] LongestWord(string letters) { // Remove any words that do not start with a letter found in 'letters' - consider removing if does not improve performance List<string> solution = new List<string>(words.Where(w => letters.Contains(w[0]))); // Remove any words that contain any letters not found in 'letters' solution.RemoveAll(w => { foreach (char c in w) { if (!letters.Contains(c)) { return true; } } return false; }); if(solution.Count > 0) { solution.RemoveAll(w => { List<char> temp = new List<char>(letters.ToCharArray()); foreach (char c in w) { if(!temp.Contains(c)) { return true; } else { temp.Remove(c); } } return false; }); } if(solution.Count > 0) { int longestLength = 0; foreach(string w in solution) { if (w.Length > longestLength) { longestLength = w.Length; } } solution.RemoveAll(w => w.Length != longestLength); } return (solution.Count > 0) ? solution.ToArray() : null; } }

答案6:

using System.Collections.Generic; using System.Linq; public partial class Kata { public static string[] LongestWord(string letters) { var res = words.Where(word => word.All(letters.Contains) &amp;&amp; word.All(l => letters.Count(c => c == l) >= word.Count(c => c == l))).ToList(); var longest = res.Aggregate("", (max, cur) => max.Length > cur.Length ? max : cur).Length; return res.Any() ? res.Where(x => x.Length == longest).ToArray() : null; } }

答案7:

using System.Linq; public partial class Kata { public static string[] LongestWord(string letters) { if (string.IsNullOrEmpty(letters)) { return null; } var candidates = words.Where(w => w.Distinct().All(c => letters.Contains(c) &amp;&amp; w.Count(c1 => c1 == c) <= letters.Count(c1 => c1 == c))); if (!candidates.Any()) { return null; } int maxLength = candidates.Select(w => w.Length).Max(); return candidates.Where(w => w.Length == maxLength) .OrderBy(w => w) .ToArray(); } }

答案8:

using System.Linq; public partial class Kata { public static string[] LongestWord(string letters) { var filtered = words .Where(w => { var letters2 = letters; foreach (var index in w.Select(t => letters2.IndexOf(t))) { if (index == -1) return false; letters2 = letters2.Substring(0, index) + letters2.Substring(index + 1, letters2.Length - index - 1); } return true; }) .OrderByDescending(s=>s.Length); var result = filtered .TakeWhile((s, i) => s.Length == filtered.First().Length) .OrderBy(s=>s) .ToArray(); return !result.Any() ? null : result; } }

答案9:

using System; using System.Linq; public partial class Kata { public static string[] LongestWord(string letters) { var ww = words .Where(w => IsMatch(w, letters)) .OrderByDescending(w => w.Length).ToList(); if (ww.Count == 0) return null; var len = ww[0].Length; return ww.Where(w => w.Length == len).ToArray(); } static bool IsMatch(string word, string letters) { foreach (var ch in word) { var index = letters.IndexOf(ch); if (index < 0) return false; letters = letters.Remove(index, 1); } return true; } }

答案10:

using System.Linq; public partial class Kata { public static string[] LongestWord(string letters) { var poss = words.Where(w => w.All(c => w.Count(x => x == c) <= letters.Count(x => x == c))); return poss.Any() ? poss.Where(w => w.Length == poss.Max(x => x.Length)).ToArray() : null; } }
最新回复(0)