Run Code  | API  | Code Wall  | Misc  | Feedback  | Login  | Theme  | Privacy  | Patreon 

substr

//Title of this code

#include <iostream>

using namespace std;


/*

hellobello


valid: lb - one missing o
valid: elo - one missing l


valid: loob - one more plus o



abcdefghij


cdeghij


*/



int search(const string& str, const string& pattern, int maxError) {

    
    
    for (int i = 0; i < str.length(); ++i) {
        
        int error = 0;
        int missing = 0;
        int plus = 0;
        
        for (int j = 0; j < pattern.length(); ++j) {
            
            if(i + j >= str.length()) {
                ++error;
            } else if ((str[i + j] != pattern[j]) &&  && (str[i + j] != pattern[j + error])) {
                ++error;
                
                
                (str[i + j] != pattern[j - error])
                
                //++missing;
                //++plus;
            } 
            
            if (error > maxError)
                break;
            
            /*
            if (str[i + j] == pattern[j - missing])
                cout << str[i + j] << "-" << pattern[j - missing] << "  ";
            else
                cout << str[i + j] << "-" << pattern[j] << "  ";
                */
            
        }
        //cout << endl;
        
        if (error <= maxError)
            return i + 1;
    }
                
    return -1;
}


int main()
{
    
    //cout << search("hellobello", "elobell", 1);
    
    cout << search("baabbaab", "baabcbaab", 1);
    
    //cout << endl;
    
    //cout << search("abcdefghij", "cdeghij", 1);

}
 run  | edit  | history  | help 0