Brother word Query

Source: Internet
Author: User

One word, one word, and one letter exchange, can get another word, such as army-> mary, to become a brother word. Provide a word and find its brother in the dictionary. Describes the data structure and query process. Analysis: There are many solutions to this problem on the Internet. I used the Count sorting method to solve this problem. The ASCII value of a letter in any word (which is assumed to be in lowercase) ranges from 97 to (97 + 26 ). The English letters of sibling words are the same, but the order is different. We set two arrays, for example, A [26] and B [26], to traverse the letters in A word. The letter ASCII code of the word is set to variable I, the ASCII code of the letter of the brother word is set to variable j, then, A [i-97] ++, B [j-97] ++. Finally traverse the letter of the word, the traversal variable is set to I, if for all I, A [i-97] = B [i-97], then the two words are brother words. Through this method, we can obtain an algorithm with the complexity of O (n. Code:

Bool IsBrotherWords (const std: string & str1, const std: string & str2) {int nLength1 = str1.length (); int nlengh2 = str2.length (); // if (nLength1! = Nleng2) return false; // set two Arrays: int a [26], B [26]; // initialize for (int I = 0; I <26; ++ I) {a [I] = B [I] = 0 ;}// start counting the letters in the word for (int I = 0; I <nLength1; ++ I) {a [str1.at (I)-97] ++; B [str2.at (I)-97] ++;} // checks whether the count is the same, if not, this is not the sibling word for (int I = 0; I <nLength1; I ++) {if (a [str1.at (I)-97]! = B [str1.at (I)-97]) return false;} return true ;}

 

The code above can be optimized. An array is used to add 1 to each letter count of a word, and 1 to each letter count of a sibling word. If the count of the last array is 0, the two words are brother words.
Bool IsBrotherWords2 (const std: string & str1, const std: string & str2) {int nLength1 = str1.length (); int nlengh2 = str2.length (); // if (nLength1! = Nleng2) return false; // design number array int a [26]; for (int I = 0; I <26; ++ I) {a [I] = 0 ;}// start counting for (int I = 0; I <nLength1; ++ I) {a [str1.at (I) -97] ++; a [str2.at (I)-97] --;} // if the last count is not 0, it is not a sibling word for (int I = 0; I <nLength1; I ++) {if (a [str1.at (I)-97]! = 0) return false;} return true ;}

 

The algorithm time complexity is O (2 * Length + 26), and Length is the word Length. Test code:
int _tmain(int argc, _TCHAR* argv[])  {        std::string str1("army");      std::string str2("mary");    bool b= IsBrotherWords(str1,str2);  bool b2=IsBrotherWords2(str1,str2);         return 0;  }  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.