1 条题解

  • 0
    @ 2025-8-6 16:00:27

    O(s.size())O(s.size())

    先对两个字符串转换比较相等

    转换:用数组标记使用过的字符,使得每种字符只保留第一次出现的 。

    #include <bits/stdc++.h>
    
    using namespace std;
    
    string convert(string s) {
        vector<bool> st(26, false);
        string res;
        for (char c : s) {
            int idx = c - 'a';
            if (!st[idx]) {
                res.push_back(c);
                st[idx] = true;
            }
        }
        return res;
    }
    
    int main() {
        string s1, s2;
        cin >> s1 >> s2;
    
        string t1 = convert(s1);
        string t2 = convert(s2);
    
        if (t1 == t2) {
            cout << t1 << endl;
        } else {
            cout << "NO" << endl;
        }
    
        return 0;
    }
    
    • 1

    信息

    ID
    504
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    7
    已通过
    4
    上传者