题解 | 查找兄弟单词

查找兄弟单词

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

#include <iostream>
#include <linux/limits.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

bool isBrother(const string& s, const string& x) {
    if (s.length() != x.length()) return false;
    if (s == x) return false;
    int count[26] = {0};
    for (char c : x) {
        count[c - 'a']++;
    }
    for (char c : s) {
        count[c - 'a']--;
        if (count[c - 'a'] < 0) {
            return false;
        }
    }
    return true;
}
int main()
{
    int n , k;
    string x;
    cin >> n;
    vector<string> str(n);
    for(int i = 0 ; i < n ; i++)
    {
        cin >> str[i];
    }
    cin>> x;
    cin>> k;
    vector<string> bro;
    for(const string& word : str)
    {
        if(isBrother(word , x))
        {
            bro.push_back(word);
        }
    }
    
    sort( bro.begin() , bro.end());
    cout << bro.size() <<endl;
    if( k <= bro.size())
    {
        cout << bro[k-1] << endl;
    }    
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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