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

  • Home
  • SEARCH
  • 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 8298231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:41:36+00:00 2026-06-08T15:41:36+00:00

I wonder wether it is feasible to implement an optimal string generator Class meeting

  • 0

I wonder wether it is feasible to implement an optimal string generator Class meeting the following second thought requirements:


  • Generation criteria using regex
  • Lexicographical order enumeration.
  • Count propetry
  • Indexed access

I don’t feel comfortable with regular expression: I cannot come up with a starting piece of code but I just think of a naive implementation using a TList as a base class and use a filter (Regex) against “brute force” generated string.

What are the other optimal alternatives ?


  • Ordering: First by length (shortest first), then lexicographically.
  • Specification of the range of characters to be used in the generation: All printable or any possible combination of [A-Z], [a-z], numbers, special symbols, and eventually space (regex ?).
  • String Length bounded with a given Min/Max.
  • Space of search constrained with bounds: Start string an End string with possibility of filtering (regex ?)

Last Edit

To begin with, I rephrased the header using regex like instead of regex.

I am considering to revise the first requirement as it is an open door which may lead to untractable issue.

I need suggestions and help for the correct wording.

Second thought requirements edit done. Still open to suggestion for refinement.

  • 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-06-08T15:41:38+00:00Added an answer on June 8, 2026 at 3:41 pm

    I’d do this by constructing the minimum Deterministic Finite Automaton for the language. If you are starting with a regex, this can be done automatically by Thompson’s Construction followed by the Subset Construction and minimization. See this description for example.

    With a DFA in hand, you can use something like this algorithm:

    Let P = { < START, [""] > } be a set of pairs <State, list of strings>
    for n = 0, 1, ... Max
      Let P' = {} be a new set 
      while P is not empty 
        Remove the pair <s, L> from P 
        For each transition s -- c --> t in alpha order of c
          if t is an accepting state,
              output l + c for each string l in L
          put <t, L + c> in P' (** i.e. append c to each string in L)
      end
      Set P = P'
    end
    

    Note that the step marked ** needs to be true set insertion, as duplicates can easily crop up.

    This is a core algorithm. P can grow exponentially with output length, but this is just the price of tracking all possibilities for a future output string. The order/size/space constraints you mentioned can be ensured by maintaining sorted order in the lists L and by cutting off the search when resource limits are reached.

    Edit

    Here is a toy Java example where I’ve hard coded the DFA for simple binary floating point literals with optional minus sign. This uses a slightly different scheme than the pseudocode above to get strict sorted order of output and to accomodate character ranges.

    import java.util.Comparator;
    import java.util.TreeSet;
    
    public class Test{
    
        public static class DFA {
    
            public static class Transition  {
    
                final int to;
                final char lo, hi; // Character range.
    
                public Transition(int to, char lo, char hi) {
                    this.to = to;
                    this.lo = lo;
                    this.hi = hi;
                }
    
                public Transition(int to, char ch) {
                    this(to, ch, ch);
                }
            }
    
            // transitions[i] is a vector of transitions from state i.
            final Transition [] [] transitions;
    
            // accepting[i] is true iff state i is accepting
            final boolean [] accepting;
    
            // Make a fresh immutable DFA.
            public DFA(Transition [] [] transitions, boolean [] accepting) {
                this.transitions = transitions;
                this.accepting = accepting;
            }
    
            // A pair is a DFA state number and the input string read to get there.
            private static class Pair {
                final int at;
                final String s;
    
                Pair(int at, String s) {
                    this.at = at;
                    this.s = s;
                }
            }
    
            // Compare pairs ignoring `at` states, since 
            // they are equal iff the strings are equal.
            private Comparator<Pair> emitOrder = new Comparator<Pair>() {
                @Override
                public int compare(Pair a, Pair b) {
                    return a.s.compareTo(b.s);
                }
            };
    
            // Emit all strings accepted by the DFA of given max length.
            // Output is in sorted order.
            void emit(int maxLength) {
                TreeSet<Pair> pairs = new TreeSet<Pair>(emitOrder);
                pairs.add(new Pair(0, ""));
                for (int len = 0; len <= maxLength; ++len) {
                    TreeSet<Pair> newPairs = new TreeSet<Pair>(emitOrder);
                    while (!pairs.isEmpty()) {
                        Pair pair = pairs.pollFirst();
                        for (Transition x : transitions[pair.at]) {
                            for (char ch = x.lo; ch <= x.hi; ch++) {
                                String s = pair.s + ch;
                                if (newPairs.add(new Pair(x.to, s)) && accepting[x.to]) {
                                    System.out.println(s);
                                }
                            }
                        }
                    }
                    pairs = newPairs;
                }
            }
        }
    
        // Emit with a little DFA for floating point numbers.
        public void run() {
            DFA.Transition [] [] transitions = {
                {   // From 0
                    new DFA.Transition(1, '-'),
                    new DFA.Transition(2, '.'),
                    new DFA.Transition(3, '0', '1'),
                },
                {   // From 1
                    new DFA.Transition(2, '.'),
                    new DFA.Transition(3, '0', '1'),
                },
                {   // From 2
                    new DFA.Transition(4, '0', '1'),
                },
                {   // From 3
                    new DFA.Transition(3, '0', '1'),
                    new DFA.Transition(4, '.'),
                },
                {   // From 4
                    new DFA.Transition(4, '0', '1'),
                }  
            };
            boolean [] accepting = { false, false, false, true, true };
            new DFA(transitions, accepting).emit(4);
        }
    
        public static void main (String [] args) {
            new Test().run();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

wonder whether someone can help me with the following one... I have a struct
I wonder whether someone can help me please. I'm trying to implement the mysql_insert_id()
I wonder whether it's possible to access a method declared in a parent class,
I wonder whether someone could help me please. I'm put together the following form
I wonder wether there's a port for gae-sessions (https://github.com/dound/gae-sessions) for the new Python (2.7)
I wonder whether it would be possible to implement something similar to http://bit.ly/MEdfiq using
I wonder whether someone may be able to help me please. Firstly apologies as
I wonder whether someone has an idea for how to count combinations like the
I wonder whether someone may be able to help me please. I'm using the
I wonder whether someone may be able to help me please Firstly, my apologies

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.