【问题标题】:How to check if string contains another string (but may have other letters in between)?如何检查字符串是否包含另一个字符串(但中间可能有其他字母)?
【发布时间】:2021-11-05 06:25:58
【问题描述】:

为了更好地解释它,假设我得到两个字符串作为输入,然后比较它们是否匹配。但是,如果某些字母被加倍,它仍然会被视为匹配

输入:“alex”,“aaleex” 输出:真

输入:“saeed”、“ssaaedd” 输出:假

输入:“leelee”,“lleeelee” 输出:真

我的 C++ 函数

 bool isLongPressedName(string name, string typed) {
    
    if(name == typed) return true;
    
    size_t lenName = name.length();
    for(size_t i = 0; i < lenName; i){
        //tried to check if typed contains all letters from name
    }
       
    //so if contains all letters inside even if its bigger and contains 
    //long pressed letter return true

    return false

}`

这是一个热门网站的作业,所以我也可以在那里查看解决方案,但我希望这里的人可以帮助我更好地理解它。因为我有点坚持自己的想法,并开始思考它的错误方法。

【问题讨论】:

  • 纸和笔你会怎么做?
  • 用你的纸和笔,试着寻找第一个字符。然后想想在哪里你要搜索下一个字符。重复。
  • 这是一个合理的问题要问别人,但我不确定“给我提示,以便我自己解决这个问题”是on-topic for SO
  • 您应该考虑您的先决条件,然后考虑在每个迭代步骤中可能发生的情况。 1. 前提条件:您的正确字符串必须等于或短于您比较的字符串 2. 在每次迭代中会做什么:在每次迭代中,您将移动一个在可能有重复的字符串中进一步添加字符,直到出现无效字符,或者您到达末尾。在您的参考字符串中,如果当前字符和下一个字符不同,您可能会暂停到下一个字符的移动(以防另一个字符串在那里有重复)
  • @DrewDormann 编辑所以它更合理

标签: c++ string loops for-loop function-definition


【解决方案1】:

对于初学者来说,函数参数应该具有常量引用类型const std::string &amp; 或类型std::string_view

您所需要的只是成员函数find

函数isLongPressedName可以如下面的演示程序所示,例如如下所示。

#include <iostream>
#include <iomanip>
#include <string>

bool isLongPressedName( const std::string &name, const std::string &typed )
{
    bool present = not ( typed.size() < name.size() );
    
    for ( std::string::size_type i = 0, pos = 0; present && i < name.size(); i++ )
    {
        pos = typed.find( name[i], pos );
        
        if ( ( present = pos  != std::string::npos ) ) ++pos;
    }
    
    return present;
}

int main() 
{
    std::cout << std::boolalpha << isLongPressedName( "alex" , "aaleex" ) <<'\n';

    std::cout << std::boolalpha << isLongPressedName( "saeed" , "ssaaedd" ) << '\n';

    std::cout << std::boolalpha << isLongPressedName( "leelee", "lleeelee"  ) << '\n'; 

    return 0;
}

程序输出是

true
false
true

或者,如果字符串typed 不应包含字符串name 中存在的字符以外的字符,则函数可以如下所示

#include <iostream>
#include <iomanip>
#include <string>

bool isLongPressedName( const std::string &name, const std::string &typed )
{
    bool present = not ( typed.size() < name.size() );
    
    for ( std::string::size_type i = 0, pos = 0; present && i < name.size(); i++ )
    {
        if ( ( present = pos < typed.size() ) )
        {
            if ( i != 0 && name[i] != typed[pos] )
            {
                pos = typed.find_first_not_of( name[i - 1], pos );
            }
        
            if ( ( present = pos != std::string::npos && name[i] == typed[pos] ) )
            {
                ++pos;
            }
        }           
    }
    
    return present;
}


int main() 
{
    std::cout << std::boolalpha << isLongPressedName( "alex" , "aaleex" ) <<'\n';

    std::cout << std::boolalpha << isLongPressedName( "saeed" , "ssaaedd" ) << '\n';

    std::cout << std::boolalpha << isLongPressedName( "leelee", "lleeelee"  ) << '\n'; 

    return 0;
}

程序输出又是

true
false
true

【讨论】:

    【解决方案2】:

    我认为在这里使用集合会更好

    bool isLongPressedName(string name, string typed) {
        set<char> s;
       
        for(auto i : typed) {
            s.insert(i);
        }    
    
        typed = "";
        for(auto j : s) {
            typed += j;
        }
    
        return (name == typed);
    }
    

    【讨论】:

    • std::set 不处理重复项(根据 OP 示例的要求)。顺序也应该很重要。
    • 嘿@Jarod42,感谢您的编辑建议。我明白你要说的关于集合中元素顺序的内容,如果你知道以给定顺序将元素插入集合的任何方法,请描述一下
    • std::includes of both std::multiset 没有顺序,否则,没有设置用法
    猜你喜欢
    • 2019-01-04
    • 2020-07-19
    • 2018-08-27
    • 2021-05-06
    • 2013-03-13
    • 1970-01-01
    • 2015-01-04
    • 2020-12-12
    相关资源
    最近更新 更多