2

I'm trying to write a recursive program: to compute all strings of length n that can be formed from all the characters given in string, but none of the strings listed in sub are allowed to appear as substrings.

This is the program I have written so far, but it doesn't yet implement the restriction of sub, it only computes the permutations of string.

    public static void method(String string)
    {
        method(string, "");
    }
    public static void method(String string, String soFar)
    {
        if (string.isEmpty())
        {
            System.err.println(soFar + string);
        }
        else
        {
            for (int i = 0; i < string.length(); i++)
            {
                method(string.substring(0, i) + string.substring(i + 1, string.length()), soFar + string.charAt(i));
            }
        }
    }

1 Answer 1

2

From your example I see that you want all permutations with repetition for n characters, but your code generates all permutations without repetition for all characters.

This should solve your problem as stated in the example:

    public static List<String> method(String string, int n, List<String> sub)
    {
        List<String> results = new ArrayList<>();
        method(string, n, "", sub, results);
        return results;
    }
    private static void method(String string, int n,  String soFar, List<String> sub, List<String> results)
    {
        for (String s: sub)
        {
            if(soFar.length() >= s.length() && soFar.substring(soFar.length() - s.length()).equals(s))
            {
                return;
            }
        }
        if (soFar.length() == n)
        {
            results.add(soFar);
        }
        else
        {
            for (int i = 0; i < string.length(); i++)
            {
                method(string, n, soFar + string.charAt(i), sub, results);
            }
        }
    }

Also, it's redundant to append string when string is empty.

Sign up to request clarification or add additional context in comments.

5 Comments

i'm trying to run the program, but the first method could only have the three variables String string int n and List<String> sub. I took out result as a variable, but I'm not quite sure how to pass an empty list (for result) when calling method in the second line. (may be a stupid question, sorry, still learning!)
You could declare it as private static List<String> results = new ArrayList<>(); outside the method, but I do not recommend it in this case unless you want to store all results for multiple method calls.
i'm also getting an error in the tester that says void cannot be converted to java.util.List<java.lang.String>
I'm still getting the same error, I added the tester in so you can see
oh, I just recopied and pasted it, and it's working. it passed the tester, thank you so much :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.