If however you get though the entire ransom note without running out of

If however you get though the entire ransom note

This preview shows page 8 out of 8 pages.

ransom note, so you immediately return false. If however you get though the entire ransom note without running out of available letters, you return true. Even better answer: Because the magazine string may be very large, we want to reduce the time we spend going through the magazine string. We use the same idea as above, except we go through the ransom note and the magazine string at the same time. Keep one pointer for our current character in the ransom note and another pointer for our current character in our magazine string. First, check to see if the count in our data structure for our current ransom note character is greater than 0. If it is, decrement it and advance the pointer in our ransom note. If it isn't, start going through the characters in the magazine string, updating the counts in the data structure for each character encountered, until we reach the character we need for our ransom note. Then stop advancing the magazine string pointer and start advancing the ransom note pointer again. If we get to the end of the ransom note, we return true. If we get to the end of the magazine string (meaning we didn't find enough letters for our ransom note), we return false.
Image of page 8

You've reached the end of your free preview.

Want to read all 8 pages?

  • Fall '16
  • Áa
  • Binary numeral system, Bitwise operation, binary
search
tree, binary
tree, Ransom
Note

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes