题解 | 查找兄弟单词

查找兄弟单词

https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

bool isBrother(const string & str, const string& x){
    if (str.size() != x.size() || str == x) 
        return false;
    vector<int> str_vec(26,0);
    vector<int> x_vec(26,0);
    for (const auto i : str) {
        ++str_vec[i-'a'];
    }
    for (const auto i : x) {
        ++x_vec[i-'a'];
    }
    return str_vec == x_vec;
}


int main() {
    int n, k; cin >> n;
    vector<string> strs, bros;
    string t, x;
    while(n-- > 0) {
        cin >> t;
        strs.push_back(t);
    }
    cin >> x >> k;
    for(const auto& it : strs){
        if (isBrother(it, x)) {
            bros.push_back(it);
        }
    }
    sort(bros.begin(), bros.end());
    cout << bros.size();
    if (bros.size() >= k) {
        cout << "\n" << bros.at(k-1);
    }
}

全部评论

相关推荐

不知道怎么取名字_:愚人节收到的吧,刚看到有人也是愚人节说收到offer的
腾讯求职进展汇总
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务