Three. Anagram detection problem for string (palindrome Word detection problem in string)

Source: Internet
Author: User

Anagram the same letter in different order. Heart vs Earth

1.our first solution to the anagram problem would check To see the character in the first string actually occurs in the second. If It is a possible to "checkoff" for each character and then the both strings must be anagrams. Checking off a character (letter) would be accomplished by replacing it with the special Python value none . However, since strings in Python is immutable, the first step in the process would be-to-convert the second string to a Li St. Each character from the first string can is checked against the characters in the list and if found, checked off by Replac Ement. activecode 1  shows this function.

Detect anagram string: The first method : mainly to see whether each word in the first string appears in the second string, the same letter is replaced with none, the string must be converted to list, because, Because the string cannot be modified (immutable).

1 defAnagramSolution1 (S1,S2):2alist=list (s2)3     "Compare the letters in S1 with the S2, as long as there are no letters in the S2 in S1, break"4pos1=0;stillok=True5     6      whilePos1<len (S1) andStillok:7Pos2=08Found=False9          whilePos2<len (alist) and  notfound:Ten             ifs1[pos1]==Alist[pos2]: OneFound=True A             Else: -Pos2=pos2+1 -                  the         iffound: -alist[pos2]=None -         Else: -stillok=False +Pos1=pos1+1 -          +     returnStillok A         
1 defAnagramSolution1 (S1,S2):2alist=list (s2)3     "Compare the letters in S1 with the S2, as long as there are no letters in the S2 in S1, break"4pos1=0;stillok=True5     6      whilePos1<len (S1) andStillok:7         8         ifS1[POS1]inchalist:9             PrintS1[POS1],"In alist"TenPos1=pos1+1 One             Continue A         Else: -stillok=False -         the     returnStillok

In the next calculation, using different algorithms, the time complexity of the first algorithm is O (n^2), the second type: first to sort in the comparison O (Nlogn), and the third: Brute brute force hack: O (n! )

For the last algorithm to be accurate to O (n):

We'll first count the number of times each character occurs. Since There is possible characters, we can use a list of the counters, one for each possible character. Each time we see a particular character, we'll increment the counter at that position. In the end, if the lists of counters is identical, the strings must be anagrams. Activecode 3 shows this solution.

This algorithm builds a list based on the 26 letter character of the word, traverses a string of 26 letters S1, String S2, and finally compares the same transformation trend.

1 defAnagramSolution2 (S1,S2):2Alist1=[0]*263Alist2=[0]*264     5      forIinchRange (len (S1)):6         "ord c int, cast ascall to Integer"7Posi=ord (S1[i])-ord ('a')8Alist1[posi]+=19     Ten      forIinchRange (len (s2)): One         "ord c int, cast ascall to Integer" APosi=ord (S2[i])-ord ('a') -Alist2[posi]+=1 -Matched=True theI=0 -      whilei< 26 andmatched: -         ifalist1[i]==Alist2[i]: -I+=1 +         Else: -Matched=False +              A     returnMatched

In all the complexities it is best to reduce the time complexity to O (n) and log (n), and algorithms that exceed O (n^2) time complexity must be modified to

Three. Anagram detection problem for string (palindrome Word detection problem in string)

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.