stiwenparker
stiwenparker

Reputation: 73

How to check if string contains another string (but may have other letters in between)?

To explain it better lets say I get two strings as an input and I compare if they match. However it would still count as a match if some letters are doubled

Input: "alex", "aaleex" Output: true

Input: "saeed", "ssaaedd" Output: false

Input: "leelee", "lleeelee" Output: true

My C++ function

 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

}`

It is an assignment from one of a popular sites so I can check solutions too in there but I hope someone here could help me understand it better. Because I got kind of stuck on my idea and started thinking its wrong approach anyway.

Upvotes: 1

Views: 500

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311018

For starters the function parameters should have the constant referenced type const std::string & or the type std::string_view.

All what you need is the member function find.

The function isLongPressedName can look for example the following way as shown in the demonstrative program below.

#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;
}

The program output is

true
false
true

Or if the string typed shall not contain characters other than characters present in the string name then the function can look the following way

#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;
}

Again the program output is

true
false
true

Upvotes: 1

yashsh
yashsh

Reputation: 79

I think using sets here would be a better approach

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);
}

Upvotes: 0

Related Questions