查找字符串中有效子字符串的长度。 C#

时间:2018-08-09 12:25:11

标签: c#

嗨,我正在尝试找出最长的有效子字符串的长度。有效的子字符串是包含至少1个大写字母且没有数字的子字符串。我的代码不起作用,有人可以帮忙谢谢你。

class Program

{

    public static void Main(string[] args)
    {
        string S = "a02caa3ThisIsValid1bC2a";

        Console.WriteLine("The longest valid substring is {0}", solution(S));

        Console.ReadKey();
    }

    public static int solution(string S)
    {
        char[] stringarray = S.ToCharArray();
        int slength = S.Length;
        int result = 0;
     //   string resultstring = "";

        for (int i = 0; i < slength; i++)
        {
            char Z = stringarray[i];

            if(char.IsUpper(Z) || char.IsLower(Z) || !char.IsDigit(Z))
            {
                while (char.IsUpper(Z) || char.IsLower(Z) && !char.IsDigit(Z))
                {
                    result += 1;
                 //   resultstring = result.ToString();
                }
            }
        }         
        return result;
    }

}

6 个答案:

答案 0 :(得分:2)

这对我有用:

public static int solution(string S)
{
    return
        S
            .Split("1234567890".ToCharArray()) // split on invalid characters
            .Where(x => x.Any(y => char.IsUpper(y))) // keep only those portions containing an uppercase char
            .Select(x => x.Length) // get the length of each string
            .Max(); // find the longest
}

以下是基于问题代码的解决方案:

public static int solution(string S)
{
    int result = 0;
    int tally = 0;
    bool foundUpper = false;

    for (int i = 0; i < S.Length; i++)
    {
        char Z = S[i];

        if (char.IsDigit(Z))
        {
            if (foundUpper && tally > result)
            {
                result = tally;
            }
            tally = 0;
            foundUpper = false;
        }
        else
        {
            tally++;
            foundUpper |= char.IsUpper(Z);
        }
    }
    if (foundUpper && tally > result)
    {
        result = tally;
    }
    return result;
}

第三个选项:

public static int solution3(string S)
{
    return S.ToCharArray().Concat(new [] { '0' }).Aggregate(
        new { result = 0, tally = 0, foundUpper = false },
        (a, x) => char.IsDigit(x)
            ? new { result = (a.foundUpper && a.tally > a.result) ? a.tally : a.result, tally = 0, foundUpper = false }
            : new { result = a.result, tally = a.tally + 1, foundUpper = a.foundUpper || char.IsUpper(x) })
        .result;
}

答案 1 :(得分:1)

    public static int solution(string s)
    {
        int result = 0;
        for (int i = 0; i < s.Length; i++)
        {
            bool containsUpper = false;
            if (Char.IsLetter(s[i]))
            {
                int len = 0;
                do 
                {
                    if (Char.IsUpper(s[i])){
                        containsUpper = true;
                    }
                    i++;
                    len++;


                } while (i<s.Length&&Char.IsLetter(s[i])) ;

                if( (len > result )&&containsUpper)
                    result = len;
            }

        }

        return result;
    }

答案 2 :(得分:1)

代码与您编写的代码完全不同。您正在寻找非数字的子字符串,然后在那些子字符串的末尾,您必须检查子字符串的至少一个字符是否为大写。如果是,则此子字符串可以作为候选,然后必须考虑其长度。通过upperCaseFound布尔变量来检查大写字符是否存在。

public static int LongestSubstring(string s)
{
    int maxLength = 0;

    bool upperCaseFound = false;
    int length = 0;

    foreach (char ch in s)
    {
        if (char.IsDigit(ch))
        {
            if (upperCaseFound && length > maxLength)
            {
                maxLength = length;
            }

            upperCaseFound = false;
            length = 0;
            continue;
        }

        if (char.IsUpper(ch))
        {
            upperCaseFound = true;
        }

        length++;
    }

    if (upperCaseFound && length > maxLength)
    {
        maxLength = length;
    }

    return maxLength;
}

答案 3 :(得分:0)

您的问题出在while循环中

while (char.IsUpper(Z) || char.IsLower(Z) && !char.IsDigit(Z))

这将无限期地运行,因为您没有在循环内更新Z。因此它永远不会改变,并且没有改变就不会有退出点。

答案 4 :(得分:0)

    public static int MaxValid(string input)
    {
        var max = 0;
        var current = 0;
        bool upper = false;

        for (int i = 0; i < input.Length; i++)
        {
            if (char.IsDigit(input, i))
            {
                if (upper && current > max)
                    max = current;

                current = 0;
                upper = false;
            }
            else
            {
                current++;
                if (char.IsUpper(input, i))
                    upper = true;
            }
        }

        return max;
    }

答案 5 :(得分:-1)

使用String.Split和所有数字字符来获取所有子字符串,然后检查哪些“包含1个大写字母”并获得最大长度:

        string S = "a02caa3ThisIsValid1bC2a";
        S.Split("0123456789".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
            .Where(u => u.ToCharArray().Count(c => Char.IsUpper(c)) == 1)
            .Max(s => s.Length);