Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 357123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:10:34+00:00 2026-05-12T12:10:34+00:00

Hey. I know this is not a ‘refactor my code’ site but I made

  • 0

Hey. I know this is not a ‘refactor my code’ site but I made this little piece of code which works perfectly fine with moderately sized input but it’s problematic with string of size, say, over 2000.

What it does – it takes a string of numbers as a parameter, and it returns the number of ways it can be interpreted as a string of letters, where each letter in the English alphabet is assigned a numeric value according to its lexical position: A -> 1, B-> 2, Z-> 26 etc.

Since some letters are represented as two numbers the suffix tree is not unique so there can be multiple interpretations. For example ‘111’ could be both ‘AAA’, ‘KA’ and ‘AK’.

This is my code. It’s fairly readable and straightforward but it’s problematic because:

  1. It has to copy part of the string every time to call it as argument to the recursive part.
  2. It has to store huge strings in the cache so it’s very expensive, memory-wise.
  3. … it’s recursive.

Help much appreciated 🙂

cache = dict()
def alpha_code(numbers):
    """
    Returns the number of ways a string of numbers
    can be interpreted as an alphabetic sequence.
    """
    global cache
    if numbers in cache: return cache[numbers]

    ## check the basic cases
    if numbers.startswith('0'): return 0
    if len(numbers) <= 1: return 1

    ## dynamic programming part

    ## obviously we can treat the first (non-zero)
    ## digit as a single letter and continue -
    ## '342...' -> C + '42...'
    total = alpha_code(numbers[1:])

    ## the first two digits make for a legal letter
    ## iff this condition holds
    ## '2511...' -> Y + '11...'
    ## '3711...' -> illegal
    if numbers[:2] <= '26':
        total += alpha_code(numbers[2:])

    cache[numbers] = total
    return total
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:10:34+00:00Added an answer on May 12, 2026 at 12:10 pm

    Try using a dynamic programming approach instead:

    1. Create an array (call it ‘P’) with 1 element per character in the string.
    2. Initialize P[0] = 1 (unless the first character is 0, in which case just return 0 for the result).
    3. Initialize P[1] = 2 if the first two characters can be interpreted as a letter as can the current; otherwise 1 if the current character is non-zero, otherwise return 0 for the result).
    4. Fill in the rest of the array from left to right, via the following rule (pseudocode):

      P[x] =
      (if current character is ‘0’ then 0, else P[x-1])
      +
      (if previous character + current character can be interpreted as a letter
      then P[x-2] else 0)

    (Note that if P[x] is ever 0 you should return zero, since that means there were two 0’s in a row which your rules don’t seem to allow.)

    The first portion of the sum is to deal with the case where the current character is interpreted as a letter; the second part of the sum is to deal with the case where the 2 most recent characters are interpreted as a letter.

    Essentially, P[x] will be equal to the number of ways that the entirety of the string from the start up to position x can be interpreted as letters. Since you can determine this from looking at previous results, you only need to loop through the contents of the string once – an O(N) time instead of a O(2N) which is a huge improvement. Your final result is simply P[len(input)-1] since “everything from the start up to the end” is the same as just “the entire string”.

    Example run for your very basic input case of ‘111’:

    • P[0] = 1 (Since 1 is non-zero)
    • P[1] = 2 (Since 11 is a valid letter, and 1 is also a valid letter)
    • P[2] = 3 (Since the most recent two characters together are a valid letter, and the current character is nonzero, so P[0]+P[1] = 1+2 = 3)

    Since P[2] is our last result, and it’s 3, our answer is 3.

    If the string were ‘1111’ instead, we’d continue another step:

    • P[3] = 5 (Since the most recent two characters are a valid letter, and current character is non-zero, so P[1]+P[2] = 2+3 = 5)

    The answer is indeed 5 – valid interpretations being AAAA, KK, AKA, AAK, KAA. Notice how those 5 potential answers are built up from the potential interpretations of ’11’ and ‘111’:

    ’11’: AA or K
    ‘111’: AAA or KA or AK

    ‘111’+A: AAA+A or KA+A or AK+A
    ’11’+K: AA+K or K+K

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys - I know this is not a strictly programming question but I'm
hey. friends i dont know this question is really asked before or not ?
hey guys , i know this is a stupid question but i hanged in
I googled this a little but couldn't find a good result. Right now I'm
I'm having this problem while writing my own HashTable. It all works, but when
hey I know I may seem like a complete fool for asking this question
Hey gang, I need to know if i need to restart my computer to
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey, in the Programming Pearls book, there is a source code for setting, clearing
Hey! Maybe I am getting the idea of a subclass wrong, but I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.