Is the answer to (2) just something like this?
If len(word) is even, check that all the characters in it appear an even number of times.
If len(word) is odd, check that all the characters in it appear an even number of times except 1.
If len(word) is even, check that all the characters in it appear an even number of times.
If len(word) is odd, check that all the characters in it appear an even number of times except 1.
has_palindrome(word)
s = empty_set
for c in word
if s.contains(c)
then s.remove(c)
else s.insert(c)
return s.size() <= 1
If I'm not allowed to depend on an existing set implementation, I would likely fall back to a quadratic solution that counts each character. If there are special performance concerns I may sort the word before hand, or implement a special kind of set. a -> not in set -> add a (a)
b -> not in set -> add b (ab)
c -> not in set -> add c (abc)
b -> in set -> remove b (ac)
a -> in set -> remove a (c)
Set has 1 <= 1 element -> return OK
(Of course, "aabcb" would return yes as well, but that's the whole point: we're not asking whether a string is a palindrome, but whether any one of its permutations is.)(This type of stuff might fail the "overengineering" test that the grandparent comment talked about though.)
Personally, I'd use the simplest implementation as a reference, and use it to test more optimised solutions if I ever need them.
Emphasis on "may", though: for small enough words, sorting may very well slow us down, so it really depends on the data: are we processing long words, or lots of small words?