Keyboard shortcuts

Press ← or β†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🏠 Back to Blog

string algorithms

Anagram Detection

  • Two strings are anagrams if they contain the same letters, but not necessarily in the same order.
  • β€˜car’ and β€˜arc’ are anagrams
  • The key to determining if 2 strings are anagrams is to sort them. If the sorted strings are the same, they are anagrams.
  • Rules for creating an algorithm to determine if 2 strings are an anagram
    • remove spaces in the words
    • convert all letters to lowercase
    • trim spaces if necessary
    • sort the strings
    • compare the strings to see if they are the same

Palindrome Detection

  • A palindrome is a word that reads the same backword as forward
  • Hannah, mom, wow, and racecar are all examples of palindromes
  • A simple way to see if a string is a palindrome is to copy it and compare the copy to the original. If they are equal, the string is a palindrome.