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 6850405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:07:13+00:00 2026-05-27T01:07:13+00:00

I am trying to create a program which outputs all permutations of a string

  • 0

I am trying to create a program which outputs all permutations of a string of length n whilst avoiding a defined substring, of length k. For example:

Derive all possible strings, up to a length of 5 characters, that can be generated from an initial empty set, which can either go to A or B, but the string cannot contain the substring “AAB” which is not allowed.

i.e. base case of [""] is the empty set.
The dictionary would be – A:{A}, B:{A,B}

From the empty set we can go to A, and we can go to B. We can not go to a B after an A but we can go to an A after a B. And both A and B can access themselves

example output: a,b,aa,bb,ba,aaa,bbb,baa,bba … etc

How would I go about prompting a user to define a substring to avoid, and from that generate a dictionary which abides to these rules?

Any help or clarification would be greatly received.

Regards,
rkhad

  • 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-27T01:07:14+00:00Added an answer on May 27, 2026 at 1:07 am

    The itertools module has a useful method called permutations():

    (from http://docs.python.org/library/itertools.html#itertools.permutations)

    itertools.permutations(iterable[, r])

    Return successive r length permutations of elements in the iterable.

    If r is not specified or is None, then r defaults to the length of the
    iterable and all possible full-length permutations are generated.

    Permutations are emitted in lexicographic sort order. So, if the input
    iterable is sorted, the permutation tuples will be produced in sorted
    order.

    List comprehensions provide an easy way to filter generated permutations like this, but beware that if you are storing permutations of a large string that you will quickly get a very large list. You may want to therefore use a set to whittle down your list to non-duplicates. Also, you may find the function sorted to be useful if you intend to iterate through your “paths” in lexicographic order. Lastly, the in operator, when applied to strings, checks for a substring (x in y checks if x is a substring of y).

    >>> from itertools import permutations
    >>> perms = [''.join(p) for p in permutations('AAAABBBB', 4)]
    >>> len(perms)
    1680
    >>> len(set(perms))
    16
    >>> filtered = [p for p in sorted(set(perms)) if 'AB' not in p]
    >>> filtered
    ['AAAA', 'BAAA', 'BBAA', 'BBBA', 'BBBB']
    

    I’m working on my dissertation right now too, in the area of Formal Languages. The concept of substring membership can be represented by a very simple regular grammar which corresponds to a deterministic finite automaton. To jog your memory:

    http://en.wikipedia.org/wiki/Regular_grammar

    http://en.wikipedia.org/wiki/Finite-state_machine

    When you look into these you will find that you need to somehow keep track of the current “state” of your computation if you want it to have different “dictionaries” at different phases. I encourage you to read the wikipedia articles, and ask me some follow-up questions as I’d be happy to help you work through this.

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

Sidebar

Related Questions

I am trying to create a program similar to Folder Lock which prevents users
I have been trying to create a simple program with Python which uses OpenCV
I'm trying to create a program which takes in a set number of strings
I'm trying to make a program which creates a spaceship and I'm using the
I'm trying to create a program that takes a text file of c++ code
I am trying to create a program that will do some simple calculations, but
I'm currently trying to create a program that estimates location based on signal strength.
I have been trying to create a Ruby program that will be running online
I'm trying to create a python program (using pyUNO ) to make some changes
My program is trying to create an key on the HKLM\Software\Microsoft\Shared Tools\MSCONFIG\startupreg\test\ but instead

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.