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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:55:34+00:00 2026-05-15T17:55:34+00:00

Ok, the real problem is slightly more complicated because it uses some classes that

  • 0

Ok, the real problem is slightly more complicated because it uses some classes that I’ve written instead of Strings, but it can be imagined like this: If you have a list of 700ish 3-letter words, how can I find every combination that can be formed by linking 5 of these words together by their first and last letters, i.e., CAT TOW WAR RAD DOG would be a success but so would CAT TOW WAR RAG GOD, and so on.

So far I have something like this:

for(int i = 0; i < list.size(); i++){
    String temp = chainMaker(list.get(i), list, used, temp);
}

public String chainMaker(String toAdd, ArrayList<String> unused,
    ArrayList<String> used, String result){

    if(result.size() >= 15)
        return result;
    else{
        if(result.canAdd(toAdd))
            result.add(toAdd);
        used.add(toAdd);

        return chainMaker(unused.remove(0), unused, used, result);
    }

    return result;
}

But this just keeps adding the producing rubbish. I’m terrible at recursive backtracking and I think that if I can just make this example work, I’ll finally understand this concept.
Please help me smart people!

  • 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-15T17:55:34+00:00Added an answer on May 15, 2026 at 5:55 pm

    Here’s a C++ implementation, but I think the logic is obvious. Just ask if you don’t understand something.

    void letterChain(string stack[5], int k, const vector<string> &words)
    {
        if ( k == 5 ) // have 5 words, print solution
        {
            for ( int i = 0; i < 5; ++i )
                cout << stack[i] << " ";
            cout << endl;
    
            return;
        }
    
        for ( int i = 0; i < words.size(); ++i ) // try to put a word at the current level of the stack
        {
            // if words[i] is already used, we cant use it again (if we can, just remove this part)
            bool used = false;
            for ( int j = 0; j < k; ++j )
                if ( stack[j] == words[i] )
                    used = true;
    
            if ( !used ) // remove this if you can use a word multiple times
            {
                if ( k == 0 || words[i][0] == stack[k - 1][2] ) // if the letters match
                {
                    stack[k] = words[i]; // add it to the stack
                    letterChain(stack, k + 1, words); // recursive call
                }
            }
        }
    }
    
    int main()
    {
        vector<string> words;
        words.push_back("CAT");
        words.push_back("TOW");
        words.push_back("WAR");
        words.push_back("RAD");
        words.push_back("DOG");
        words.push_back("GOD");
        words.push_back("RAG");
        words.push_back("RAR");
    
        string stack[5];
    
        letterChain(stack, 0, words);
    }
    

    CAT TOW WAR RAD DOG
    CAT TOW WAR RAG GOD
    CAT TOW WAR RAR RAD
    CAT TOW WAR RAR RAG
    TOW WAR RAD DOG GOD
    TOW WAR RAG GOD DOG
    TOW WAR RAR RAD DOG
    TOW WAR RAR RAG GOD
    WAR RAR RAD DOG GOD
    WAR RAR RAG GOD DOG

    If you can only use a word as many times as it appears in the list, then just use a counting vector subtract the count of that word every time you use it.

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

Sidebar

Related Questions

I want to make an app that can display images. But the real problem
I'm having a real problem with this because I just can't get my head
This example is a simplification of the real problem, but how can I get
We're having a real problem with people checking in code that doesn't work because
I have hit a real problem. I need to do some Kmeans clustering for
Hi guys i've never written a comparator b4 and im having a real problem.
[Updated, sorry about the change but now to the real problem] I cannot include
I would like solve the problem (now hypothetical but propably real in future) of
I've got a real problem (it's not homework, you can check my profile). I
Well, I have a problem (ok, no real problem, but I wanna try out

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.