Posted on

Description

Submission

class Solution {
public:
    int minCharacters(string a, string b) {
        vector<int> countA(26, 0);
        vector<int> countB(26, 0);
        for(auto ch: a) {
            ++countA[ch-'a'];
        }

        for(auto ch: b) {
            ++countB[ch-'a'];
        }

        int ret = INT_MAX;
        for(int i = 0; i < 26; ++i) {
            int count = 0;

            if(i > 0) {
                // make a less
                for(int j = i; j < 26; ++j) {
                    count += countA[j];
                }
                for(int j = 0; j < i; ++j) {
                    count += countB[j];
                }

                ret = min(count, ret);

                count = 0;
                // make b less
                for(int j = i; j < 26; ++j) {
                    count += countB[j];
                }
                for(int j = 0; j < i; ++j) {
                    count += countA[j];
                }
                ret = min(count, ret);
            }
            
            count = 0;
            for(int j = 0; j < 26; ++j) {
                if(i != j) {
                    count += countA[j];
                    count += countB[j];
                }
            }
            ret = min(count, ret);
        }
        return ret;
    }
};


// set and traverse the boundary

Leave a Reply

Your email address will not be published. Required fields are marked *