栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在字符串数组中找到最长的子字符串,并将其从数组中的所有元素中删除

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在字符串数组中找到最长的子字符串,并将其从数组中的所有元素中删除

此扩展名找到最长,最常见的子字符串。请注意,

"1"
每个字符串中包含的频率也比更高
"10"
。(仅C#):

public static class StringExtensions{    public static IEnumerable<string> GetMostCommonSubstrings(this IList<string> strings)    {        if (strings == null) throw new ArgumentNullException("strings");        if (!strings.Any() || strings.Any(s => string.IsNullOrEmpty(s))) throw new ArgumentException("None string must be empty", "strings");        var allSubstrings = new List<List<string>>();        for (int i = 0; i < strings.Count; i++)        { var substrings = new List<string>(); string str = strings[i]; for (int c = 0; c < str.Length - 1; c++) {     for (int cc = 1; c + cc <= str.Length; cc++)     {         string substr = str.Substring(c, cc);         if (allSubstrings.Count < 1 || allSubstrings.Last().Contains(substr))  substrings.Add(substr);     } } allSubstrings.Add(substrings);        }        if (allSubstrings.Last().Any())        { var mostCommon = allSubstrings.Last()     .GroupBy(str => str)     .OrderByDescending(g => g.Key.Length)     .ThenByDescending(g => g.Count())     .Select(g => g.Key); return mostCommon;        }        return Enumerable.Empty<string>();    }}

现在很简单:

string[] x = new[] { "10111", "10122", "10250", "10113" };string mostCommonSubstring = x.GetMostCommonSubstrings().FirstOrDefault();if (mostCommonSubstring != null){    for (int i = 0; i < x.Length; i++)        x[i] = x[i].Replace(mostCommonSubstring, "");}Console.Write(string.Join(", ", x));

输出:

111, 122, 250, 113

[**DEMO**](http://ideone.com/dCBAir)


编辑
:如果只想找到最长的公共子串而不考虑出现的频率,则可以使用以下优化方法(O(n)操作)

HashSet<string>

public static string GetLongestCommonSubstring(this IList<string> strings){    if (strings == null)        throw new ArgumentNullException("strings");    if (!strings.Any() || strings.Any(s => string.IsNullOrEmpty(s)))        throw new ArgumentException("None string must be empty", "strings");    var commonSubstrings = new HashSet<string>(strings[0].GetSubstrings());    foreach (string str in strings.Skip(1))    {        commonSubstrings.IntersectWith(str.GetSubstrings());        if (commonSubstrings.Count == 0) return null;    }    return commonSubstrings.OrderByDescending(s => s.Length).First();}public static IEnumerable<string> GetSubstrings(this string str){    if (string.IsNullOrEmpty(str))        throw new ArgumentException("str must not be null or empty", "str");    for (int c = 0; c < str.Length - 1; c++)    {        for (int cc = 1; c + cc <= str.Length; cc++)        { yield return str.Substring(c, cc);        }    }}

以这种方式使用它:

string[] x = new[] { "101133110", "101233210", "102533010", "101331310" };string longestCommon = x.GetLongestCommonSubstring();  // "10"


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/485617.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号