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:
Count the number of occurrence of each character.
Only one character with odd occurrence are allowed since in a palindrome maximum number of character with odd occurrence CA n be ' 1 '.
All and character should occur in even number of times.
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