Twitter OA Prepare:anagram is A palindrome

Source: Internet
Author: User

A string is a palindromeifIt has exactly the same sequence of characters when traversed Left-to-right as right-to-Left . For example, the following strings is palindromes:"Kayak" "Codilitytilidoc" "Neveroddoreven"A string A is an anagram of a string BifIt consists of exactly the same characters, but possibly in another order. For example, the following strings is each of the other ' s anagrams:A= "Mary" b= "army" a= "Rocketboys" b= "Octobersky" a= "codility" b= "codility"Write a functionintisanagramofpalindrome (String S); which returns1ifThe string S is a anagram of some palindrome, or returns 0otherwise. For example your function shouldreturn1 forThe argument "DOOERNEDEEVRVN", because it is an anagram of a palindrome "Neveroddoreven". For argument "AABCBA", your function shouldreturn0.

Algorithm:

    1. Count the number of occurrence of each character.

    2. Only one character with odd occurrence are allowed since in a palindrome maximum number of character with odd occurrence CA n be ' 1 '.

    3. All and character should occur in even number of times.

    4. If (2) and (3) fail, then the given string was not a palindrome.

1  Public intCheck (String str) {2Hashmap<character, integer> map =NewHashmap<character, integer>();3      for(inti=0; I<str.length (); i++) {4         Charc =Str.charat (i);5         if(Map.containskey (c)) {6Map.put (c, Map.get (c) +1);7         }8         Else {9Map.put (c, 1);Ten         } One     } A    -     intcntodd = 0; -      for(intval:map.values ()) { the         if(val% 2 = = 1) { -cntodd++; -         } -     } +     returnCntodd>1? 0:1; -}

Twitter OA Prepare:anagram is A palindrome

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.