Ransom letter

Title:
Given a ransom channel (ransom) and a string magazine (magazine) string, the first string is determined ransom can consists of a second character string inside the magazines. If you can constitute, return true; otherwise false.

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        if(ransomNote.size() > magazine.size()){
            return false;
        }
        int i, tmp;
        for(i = 0; i < ransomNote.size(); i++){
            tmp = 0;
            while(ransomNote[i] != magazine[tmp]){
                if(tmp == magazine.size() - 1){
                    return false;
                }
                tmp++;
            }
            swap(magazine[tmp], magazine[magazine.size() -1]);
            magazine.pop_back();
        }
        return true;
    }
};

If the string length is greater than the ransom length of the string magazine, it returns false, circulation ransom string, if the string magazine ransom characters no, false is returned, if found, then it is the last character of the character exchange, and then tail-deleted. If after traversal ransom string, it returns true.

Published 77 original articles · won praise 23 · views 7479

Guess you like

Origin blog.csdn.net/Hots3y/article/details/104033043