Skip to content

Latest commit

 

History

History
53 lines (40 loc) · 1.06 KB

21.md

File metadata and controls

53 lines (40 loc) · 1.06 KB

Method 1

int minSteps(string s, string t) {
        
        unordered_map<char,int>m;

        for(int i=0;i<s.size();i++)
            m[s[i]]++;

        int ans=0;

        for(int i=0;i<t.size();i++)
        {
            if(m.find(t[i])!=m.end())
            {
                m[t[i]]--;
                if(m[t[i]]==0)
                    m.erase(t[i]);
            }
            else
                ans++;
        }
        return ans;
    }

Method 2

int minSteps(std::string s, std::string t) {
        std::vector<int> count_s(26, 0);
        std::vector<int> count_t(26, 0);

        for (char ch : s) {
            count_s[ch - 'a']++;
        }

        for (char ch : t) {
            count_t[ch - 'a']++;
        }

        int steps = 0;
        for (int i = 0; i < 26; i++) {
            steps += std::abs(count_s[i] - count_t[i]);
        }

        return steps / 2;  
    }