Anagrams and Palindromes

More Java homework:

First, I remove all the spaces. Then I make sure the two words are the same length. Then I loop through all the letters in the first word, find them in the second word, and replace them with a plus sign. If the second word is all plus signs at the end, then it’s an anagram. My first thought was to put the letters in an array and sort them, but that’s too much looping.

Nothing too fancy here. Again, I remove all the spaces. Then I compare the first letter to the last letter, the second letter to the penultimate letter, and so on. If there’s every not a match, set the return value to False and quit looking.

22 thoughts on “Anagrams and Palindromes

  1. Why not ?

  2. And

  3. Just to follow up on my previous message, I think the following modification to snb’s anagram function will make it work correctly…

    Function F_ana_snb(c00, c01)
    For j = 1 To Len(c00)
    If InStr(c01, Mid(c00, j, 1)) Then
    c01 = Replace(c01, Mid(c00, j, 1), “”, , 1, 1)
    Mid(c00, j) = ” ”
    End If
    F_ana_snb = Trim(c01) = Trim(c00)
    End Function

  4. @Rick
    Quite right !

  5. @Rick

    Now it does:

  6. Here is a one-liner, albeit an extremely long one, in which I used line continuation characters in order to (hopefully) layout neatly in the code display box for this blog’s comment box (you can remove the line continuation characters after copy/pasting it into VBA module if you want to see it as a physical one-liner)…

  7. Here’s my ugly palindrome formula:


  8. Anagram in formula-form:


  9. @snb,

    I don’t think that formula works all the time. For example, it reports TRUE if A1 equals “my bad” and B1 equals “am bad”.

    Here is my attempt which appears to work correctly… it is an array-entered** formula,


    **Commit this formula using CTRL+SHIFT+ENTER and not just Enter by itself

  10. Let’s try that again, this time putting the formula inside code tags to see if the quote marks remain as normal quote marks this time…

    Remember to array-enter the formula (CTRL+SHIFT+ENTER and not Enter by itself)

  11. Well, that attempt screwed up the ampersands.

    Does anyone know of a way to enter code and/or formulas where the quote marks remain as “normal” quote marks and the ampersand does not get posted with that extra text???

  12. From this SO post,, code to find the longest palindrome in a string

  13. @Rick. The plugin I use to code color comments doesn’t work right. It’s nothing you’re doing, it just a crappy plugin that I haven’t replaced yet.

  14. Is Palindrome is too easy: the native VBA.Strings.ReverseString spoils the fun:

    However, your algorithm for detecting anagrams is surprisingly efficient: I looked for ways of using the Byte Array / String type identity, and there’s nothing except a crude Byte Sum check as a second test to eliminate anything that passed the len() comparison.

    You can’t do a simple sort-and-string-compare of the byte arrays, because there are two-byte encoded unicodes that would sort as combinations of Latin ‘ASCII’ chars that only use a single byte.

    A native Sort() function for arrays would make it worth your while to turn the cleaned-up strings into arrays of chars, but we don’t have that; and I agree that you’re right not to bother with the complexity of a hand-rolled sort.

    CountIf would be nice if we had it for VBA arrays instead of ranges, but we don’t… But we do have an obscure VBA.Strings function which does the same thing, and it’s an interesting alternative.

    Note that the only possible source of performance improvement is that we don’t grind our way to the bitter end and return a final comparison: the first failed character count bails out with


    On a more general note, VBA.Strings.Filter() deserves to be better known: if you’re smart the Split() function, it’s a useful counting and comparison tool.

  15. Looks like the cc-vb codes eat < and > characters.

    You might want to check these two lines in my code:

    And here, another ‘not equals’ comparison:

Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.