最长的指定瑕疵度的元音子串

解题思路

其实这道题就是典型的双指针问题,左边指针从字符串的左边0位置开始找,右边指针从字符串位置字符串长度减1的位置开始找。

1.以aabuuba为例,进入while循环,判断左边位置是否为元音,如果不是就是left++,进入下一次循环。

2.判断右边位置是否为元音,如果不是就right--,进入下一个循环。

3.如果左右都是元音,案例刚进入循环都是元音,这个时候就要注意了,这个时候不能单纯的left++,right--处理,因为如果left++。那么结果可能就是uuba,长度为4.这个结果是不对的,正确的结果是aabuu。

解决思路在下面具体代码中

代码


std::vector<char> VowelChars = { 'a','o','e','i','u' };

//判断当前字符是否为元音
bool IsVowel(char chr)
{
    //转换为小写字母
    chr=std::tolower(chr);
    if (std::find(VowelChars.begin(), VowelChars.end(), chr) == VowelChars.end())
        return false;
    return true;
}
//获取瑕疵度
int GetDefectDeep(std::string& str)
{
    int deep = 0;
    for (auto c : str)
    {
        if (!IsVowel(c))
            deep++;
    }
    return deep;
}

//获取最长指定瑕疵度的元音字符串长度 
//str:原始字符串
//deep:指定的瑕疵度
int GetMaxVowelStringLenght(std::string& str, int deep)
{
    //采用双指针的做法,左边从0开始,右边从尾部开始
    int left = 0;
    int right = str.length() - 1;
    int max = 0;
    while (right >= left)
    {
        if (!IsVowel(str[right]))
        {
            right--;
            continue;
        }
        if (!IsVowel(str[left]))
        {
            left++;
            continue;
        }
        //查询瑕疵度是否正确
        std::string newString = str.substr(left, right - left+1);
        if (GetDefectDeep(newString) != deep)
        {
            //从左往右
            for (int i = left; i <= right; i++)
            {
                newString = str.substr(i, right - i + 1);
                if (IsVowel(newString[0]) &&  GetDefectDeep(newString) == deep && right - i +1> max)
                {
                    max = right - i+1;
                }
            }
            //从右往左
            for (int i = right; i >= left; i--)
            {
                newString = str.substr(left, i - left + 1);
                if (IsVowel(newString[0]) &&  GetDefectDeep(newString) == deep && i - left +1 > max)
                {
                    max = i - left+1;
                }
            }
            right--;
            left++;
        }
        else
        {
            if (right - left > max)
            {
                max = right - left;
            }
            break;
        }
    }
    return max;
}

测试代码

写了两个测试函数,一个用来调试单个案例,另一个全面验证

//单个测试,一般用来调试代码
void DebugTest()
{
    std::string str = "aabeebuu";
    int deep = 1;

    int result=GetMaxVowelStringLenght(str, deep);
    std::cout << result << std::endl;     //预期结果为   aabee  eebuu
}

//全量测试,输入不同的案例,测试代码质量
void FullTest()
{
    std::string str1 = "aabeebuu";
    int deep1 = 1;
    int result = GetMaxVowelStringLenght(str1, deep1);
    std::cout << result << std::endl;     //预期结果为5  aabee eebuu

    str1= "asdbuiodevauufgh";
    deep1 = 0;
    result = GetMaxVowelStringLenght(str1, deep1);   //满足最长元音字符子串有2个  uio   auu 长度为3
    std::cout << result << std::endl;

    str1 = "aeueo";
    deep1 = 2;
    result = GetMaxVowelStringLenght(str1, deep1);   //0
    std::cout << result << std::endl;
}

主函数

void main()
{
    //DebugTest();
    FullTest();
}

结果

华为OD2024 E 文章被收录于专栏

实时更新华为2024 E卷答案

全部评论

相关推荐

仁者伍敌:难怪小公司那么挑剔,让你们这些大佬把位置拿了
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务