ARRS-P5

Nearly a palindrome

Practice 5 of 5Arrays and strings9 tests

The problem

A station name is written as a string s of lower-case letters. Return True if it reads the same forwards and backwards after you delete at most one character. Deleting nothing is allowed.

almost_palindrome("abca")      # True: delete "b" (or "c")
almost_palindrome("racecar")   # True: nothing to delete
almost_palindrome("abcda")     # False: needs two deletions

Limits

  • Up to 200,000 letters.

Builds on ARRS-2 Two pointers and ARRS-6 Strings as arrays.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

9 tests, not run yet
Test results
#TestResultReads · time
1Delete oneNot run
2Already a palindromeNot run
3Needs two deletionsNot run
4Only the right deletion worksNot run
5Only the left deletion worksNot run
6Short stringsNot run
7Delete at the very endNot run
8200,000 letters, TrueNot run
9200,000 letters, FalseNot run