找出字符串中最长序列的长度

时间:2010-08-11 13:25:30

标签: c#

我最近在一次采访中遇到了这个问题,这就是我提出的问题。有什么反馈吗?

找出字符串中最长序列的持续时间。例如,在字符串“abccdeeeeef”中,答案是5。

    static int LongestSeq(string strPass)
    {
        int longestSeq = 0;

        char[] strChars = strPass.ToCharArray();

        int numCurrSeq = 1;
        for (int i = 0; i < strChars.Length - 1; i++)
        {
            if (strChars[i] == strChars[i + 1])
            {
                numCurrSeq++;
            }
            else
            {
                numCurrSeq = 1;
            }

            if (longestSeq < numCurrSeq)
            {
                longestSeq = numCurrSeq;
            }
        }

        return longestSeq;
    }

9 个答案:

答案 0 :(得分:4)

对于长度为1的字符串,它将返回0(当它应返回1时)。

答案 1 :(得分:3)

第一条评论:您不需要将其转换为char数组。您可以直接索引到字符串中。

第二条评论:如果您愿意,可以使用IEnumerable<T>并记住“当前”项目,轻松将其概括为foreach

第三条评论:我认为longestSeqnumCurrSeq之间的比较会更清晰:

if (numCurrSeq > longestSeq)

对我来说,这更自然,因为我通常首先拥有表达式的不同部分。

答案 2 :(得分:2)

只是添加我的2便士,这是使用正则表达式的替代方案:

string source = "eeabccdeeeeef";
Regex reg = new Regex(@"(\w)\1+");
MatchCollection matches = reg.Matches(source);

int longest = 0;
foreach (System.Text.RegularExpressions.Match match in matches)
{
    if (longest < match.Length) longest = match.Length;
}

由于在发布我之前的答案时没有正确地阅读问题,我应该添加一些实际的反馈,考虑到OP发布的问题。然而,Henrik或Job Skeet已经提到了我提出的每一点,所以我只想强调Jon Skeet的观点;您不必将字符串转换为char数组,您只需索引字符串中的特定点,如下所示:

char letter = someString[4];

如果您将strChars替换为strPass,那么它应该仍然有用。

答案 3 :(得分:0)

您始终可以重新标记最后一个字符,因此您无需在迭代中访问该数组两次。

在循环内部,只要当前字符与最后一个字符相同,就可以使用另一个迭代循环。在此子循环之后,您可以检查当前的numCurrSeq> longestSeq你不需要每次迭代检查,但每个子序列都需要检查。

答案 4 :(得分:0)

我真的不知道这是什么语言(C#?)所以请原谅任何轻微的句法故障(我不知道是否是“else if”或“elseif”或“elif”或其他)

static int LongestSeq(string strPass)
{
    int longestSeq = 1;

    int curSeqStart = 0;
    for (int i = 1; i < strPass.Length; i++)
    {
        if (strPass[i] != strPass[curSeq])
        {
            curSeqStart = i;
        }
        else if (i - curSeqStart + 1 > longestSeq) 
        {
            longestSeq = i - curSeqStart + 1;
        }
    }
    return longestSeq;
}

执行

效率可能更高
...
else
{
    len = i - curSeqStart + 1
    if ( len > longestSeq )
    {
        longestSeq = len;
    }
}

甚至只是

...
else
{
    longestSeq = max(longestSeq, i - curSeqStart + 1)
}

取决于'max'实现和编译器的好坏。

答案 5 :(得分:0)

我认为这有效吗?我通常不会写递归方法,我会完全拿出海报答案..

public static int recurse(Char last, int seqLength, int currentIndex, int largestSeqLength, string source)
{
    if (currentIndex > source.Length)
    {
        return largestSeqLength;
    }

    if (source[currentIndex] == last)
    {
        seqLength++;
        if (seqLength > largestSeqLength)
        {
            largestSeqLength = seqLength;
        }
    }
    else
    {
        seqLength = 1;
    }

    return recurse(source[currentIndex], seqLength, currentIndex++, largestSeqLength, source);
}

答案 6 :(得分:0)

另一个实现

public static int LongestSeq<T>(this IEnumerable<T> source)
{
    if (source == null)
        throw new ArgumentNullException("source");

    int result = 0;
    int currentCount = 0;

    using (var e = source.GetEnumerator())
    {
        var lhs = default(T);

        if (e.MoveNext())
        {
            lhs = e.Current;
            currentCount = 1;
            result = currentCount;
        }

        while (e.MoveNext())
        {
            if (lhs.Equals(e.Current))
            {
                currentCount++;
            }
            else
            {
                currentCount = 1;
            }

            result = Math.Max(currentCount, result);
            lhs = e.Current;
        }
    }

    return result;
}

答案 7 :(得分:0)

一个简单的(未经测试的)解决方案是:

int GetLongestSequence(string input)
{
  char c = 0;
  int maxSequenceLength = 0;
  int maxSequenceStart = 0;
  int curSequenceLength = 0;
  int length = input.Length;

  for (int i = 0; i < length; ++i)
  {
    if (input[i] == c)
    {
      ++curSequenceLength;
      if (curSequenceLength > maxSequenceLength)
      {
        maxSequenceLength = curSequenceLength;
        maxSequenceStart = i - (curSequenceLength - 1);
      }
    }
    else
    {
      curSequenceLength = 1;
      c = input[i];
    }
  }
  return maxSequenceStart;
}

或更好的结构化代码(也未经测试):

private int GetSequenceLength(string input, int start)
{
  int i = start;
  char c = input[i];
  while (input[i] == c) ++i; // Could be written as `while (input[i++] == c);` but i don't recommend that
  return (i - start);
}

public int GetLongestSequence(string input)
{
  int length = input.Length;
  int maxSequenceLength = 0;
  int maxSequenceStart = 0;

  for (int i = 0; i < length; /* no ++i */)
  {
    int curSequenceLength = this.GetSequenceLength(input, i);
    if (curSequenceLength > maxSequenceLength)
    {
      maxSequenceLength = curSequenceLength;
      maxSequenceStart = i;
    }
    i += curSequenceLength;
  }
  return maxSequenceStart;
}

答案 8 :(得分:0)

此扩展方法在字符串中查找相同字符的最长序列。

   public static int GetLongestSequenceOfSameCharacters(this string sequence)
        {
            var data = new List<char>();
            for (int i = 0; i < sequence.Length; i++)
            {
                if (i > 0 && (sequence[i] == sequence[i - 1]))
                {
                    data.Add(sequence[i]);
                }               
            }

            return data.GroupBy(x => x).Max(x => x.Count()) + 1;
        }

 [TestMethod]
        public void TestMethod1()
        {
            // Arrange
            string sequence = "aabbbbccccce";

            // Act
            int containsSameNumbers = sequence.GetLongestSequenceOfSameCharacters();

            // Assert
            Assert.IsTrue(containsSameNumbers == 5);

        }