LeetCode's 25th bi-weekly contest 5386. Check whether one string can break another string

Topic link

Insert picture description here

  • I personally think this question is easy, because the judgment conditions are too straightforward. The idea at the time was to find the maximum values ​​of s1 and s2 in lexicographical order, and then compare them.
    code show as below:
class Solution {
    
    
public:
    bool checkIfCanBreak(string s1, string s2) {
    
    
        vector<char> a(s1.size()),b(s1.size());
        bool flag1=true,flag2=true;
        for(int i=0;i<s1.size();i++)
        {
    
    
            a.push_back(s1[i]);
            b.push_back(s2[i]);
        }
        sort(a.begin(), a.end(), greater<char>());
        sort(b.begin(), b.end(), greater<char>());
       for(int i=0;i<s1.size();i++)
        {
    
    
           if(!(a[i]>=b[i]))
           {
    
    
               flag1=false;
               break;
           }
        }
        for(int i=0;i<s1.size();i++)
        {
    
    
           if(!(b[i]>=a[i]))
           {
    
    
               flag2=false;
               break;
           }
        }
        if(!flag1&&!flag2)
            return false;
        else
            return true;
        
    }
};

Guess you like

Origin blog.csdn.net/qq_43663263/article/details/105902809