-1

I wrote the following function and the output for the program is correct. However, the program discovers all possible states while doing the recursion, which means the program can be done more efficient. Basically, I need the recursion to terminate when output is True and not to discover other states. Any ideas are appreciated!

checked_strings = []

# idx - current index of string a
# input string a
# output string b
def abbreviation(idx, a, b):
    if a in checked_strings:
        return False
    if idx == len(a):
        return a == b
    flag1 = flag2 = False
    if a[idx].islower():
        new_string1 = a[:idx] + a[idx+1:]
        flag1 = abbreviation(idx, new_string1, b)
        if not flag1:
            checked_strings.append(new_string1)
        new_string2 = a[:idx] + a[idx].upper() + a[idx+1:]
        flag2 = abbreviation(idx + 1, new_string2, b)
        if not flag2:
            checked_strings.append(new_string2)
    return flag1 or flag2 or abbreviation(idx + 1, a, b)

The description of the problem is as follows:

Given two strings a and b (b in upper case). Find if it is possible to obtain string b from string a using the following rules:

  1. If the character of the string is lower case, then you allowed to remove it.

  2. If the character of the string is lower case, then you are allowed to make this character upper case.

  3. The character can be skipped.

The input is as follows:

1
daBcd
ABC

While the output should be:

true 
stackoverload
  • 119
  • 1
  • 6
  • 1
    Maybe you could share some example input and expected outputs? What does `abbreviation` actually do? – Paul M. Jun 19 '20 at 19:32
  • I am gonna add them now – stackoverload Jun 19 '20 at 19:36
  • Please supply the expected [minimal, reproducible example]( https://stackoverflow.com/help/minimal-reproducible-example). We cannot effectively help you until you post your MCVE code and accurately specify the problem. Also, please repeat [how to ask]( https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). You haven't explained your algorithm, nor traced its current operation. Since you've used meaningless variable names, we can't readily reconstruct your logic -- and we shouldn't have to do that work at all. – Prune Jun 19 '20 at 19:36
  • I think the problem is that you're calling abbreviation for flag2 even if flag1 succeded. If flag1 succeded, you should skip the second abbreviation call so you won't keep finding states. (This is a guess, since as Prune said, it's unclear what's going on.) – Ken Shirriff Jun 19 '20 at 19:50
  • @KenShirriff thanks! It works now. – stackoverload Jun 19 '20 at 19:53

1 Answers1

0
checked_strings = []

# idx - current index of string a
# input string a
# output string b
def abbreviation(idx, a, b):
    if a in checked_strings:
        return False
    if idx == len(a):
        return a == b
    flag1 = flag2 = False
    if a[idx].islower():
        new_string1 = a[:idx] + a[idx+1:]
        flag1 = abbreviation(idx, new_string1, b)
        if flag1:
            return True
        else
            checked_strings.append(new_string1)
        new_string2 = a[:idx] + a[idx].upper() + a[idx+1:]
        flag2 = abbreviation(idx + 1, new_string2, b)
        if flag2:
            return True
        else
            checked_strings.append(new_string2)
    return abbreviation(idx + 1, a, b)

When one of the flags is True, you can immediately return it.