在一个字符串测试对于重复字符在一、字符串、字符、测试

2023-09-11 01:52:40 作者:绾生

我做处理字符串一些工作,我有一种情况,我需要确定一个字符串(通常是一个小的小于10个字符)。包含重复字符

I'm doing some work with strings, and I have a scenario where I need to determine if a string (usually a small one < 10 characters) contains repeated characters.

`ABCDE`  // does not contain repeats 
`AABCD`  // does contain repeats, ie A is repeated

我可以通过string.ToCharArray循环()和测试的焦炭中的[]所有其他角色各的性格,但我觉得我缺少明显的东西....也许我只需要咖啡。任何人都可以帮忙吗?

I can loop through the string.ToCharArray() and test each character against every other character in the char[], but I feel like I am missing something obvious.... maybe I just need coffee. Can anyone help?

编辑:

的字符串进行排序,所以顺序并不重要,所以ABCDA => AABCD

The string will be sorted, so order is not important so ABCDA => AABCD

重复的频率也很重要,所以我需要知道,如果重复是对或三线等。

The frequency of repeats is also important, so I need to know if the repeat is pair or triplet etc.

推荐答案

如果字符串很短,然后就循环和测试很可能是最简单,最有效的方式。我的意思是,你的可以的创建一个哈希集合(在任何平台,你正在使用),并遍历字符,否则如果字符已经在集并将其添加到设置,否则 - 但这只是可能提供的任何好处,当字符串是更长的时间。

If the string is short, then just looping and testing may well be the simplest and most efficient way. I mean you could create a hash set (in whatever platform you're using) and iterate through the characters, failing if the character is already in the set and adding it to the set otherwise - but that's only likely to provide any benefit when the strings are longer.

编辑:现在我们知道它的排序,mquander's回答是最好的一个海事组织。下面是一个实现:

Now that we know it's sorted, mquander's answer is the best one IMO. Here's an implementation:

public static bool IsSortedNoRepeats(string text)
{
    if (text.Length == 0)
    {
        return true;
    }
    char current = text[0];
    for (int i=1; i < text.Length; i++)
    {
        char next = text[i];
        if (next <= current)
        {
            return false;
        }
        current = next;
    }
    return true;
}

一个较短的选择,如果你不介意重复使用索引:

A shorter alternative if you don't mind repeating the indexer use:

public static bool IsSortedNoRepeats(string text)
{
    for (int i=1; i < text.Length; i++)
    {
        if (text[i] <= text[i-1])
        {
            return false;
        }
    }
    return true;
}

编辑:好的,用频率的一面,我要把这个问题全面一些。我还是要假定该字符串进行排序,所以我们要知道什么是运行时间最长的长度。当没有重复序列,最长的运行长度将是0(对于空字符串)或1(用于非空字符串)。否则,这将是2以上。

Okay, with the "frequency" side, I'll turn the problem round a bit. I'm still going to assume that the string is sorted, so what we want to know is the length of the longest run. When there are no repeats, the longest run length will be 0 (for an empty string) or 1 (for a non-empty string). Otherwise, it'll be 2 or more.

首先一个字符串特定版本:

First a string-specific version:

public static int LongestRun(string text)
{
    if (text.Length == 0)
    {
        return 0;
    }
    char current = text[0];
    int currentRun = 1;
    int bestRun = 0;

    for (int i=1; i < text.Length; i++)
    {
        if (current != text[i])
        {
            bestRun = Math.Max(currentRun, bestRun);
            currentRun = 0;
            current = text[i];
        }
        currentRun++;
    }
    // It's possible that the final run is the best one
    return Math.Max(currentRun, bestRun);
}

现在我们也可以做到这一点作为的IEnumerable℃的通用扩展方法; T&GT;

Now we can also do this as a general extension method on IEnumerable<T>:

public static int LongestRun(this IEnumerable<T> source)
{
    bool first = true;
    T current = default(T);
    int currentRun = 0;
    int bestRun = 0;

    foreach (T element in source)
    {
        if (first || !EqualityComparer<T>.Default(element, current))
        {
            first = false;
            bestRun = Math.Max(currentRun, bestRun);
            currentRun = 0;
            current = element;
        }
    }
    // It's possible that the final run is the best one
    return Math.Max(currentRun, bestRun);
}

然后就可以调用AABCD.LongestRun()例如。