[算法]判断兄弟单词

一个单词单词字母交换,可得另一个单词,如army->mary,成为兄弟单词。提供一个单词,在字典中找到它的兄弟。描述数据结构和查询过程。

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void add(unsigned int count[],char c){
    if(c <= 'Z'){
        ++count[c - 'A'];
    }
    else{
        ++count[c - 'a'];
    }
    
}
bool isBrother(const string& wa,const string& wb){
    if(wa.length() != wb.length()){
        return false;
    }
    unsigned int counta[26],countb[26];
    memset(counta,0,26*4);
    memset(countb,0,26*4);
    for(int i = 0; i < wa.length(); ++i){
        add(counta,wa.at(i));
        add(countb,wb.at(i));
    }
    for(int i = 0; i < 26; ++i){
        if(counta[i]!=countb[i]){
            return false;
        }
    }
    return true;
} 

 

你可能感兴趣的:(算法)