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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:11:01+00:00 2026-06-17T15:11:01+00:00

I’m having a school project for Java and I’m assigned too. Now I’m having

  • 0

I’m having a school project for Java and I’m assigned too. Now I’m having an issue with a part of the project which I can’t figure out.
The application must generate all possible word combinations (can be verified via a dictionary) from a two-dimensional char array (char[][] board). The board is dynamic as the user can choose the scale: 4×4, 5×5, 4×5, 5×4, 4×6, … So I guess a nested loop wouldn’t be approriate here, correct me if I’m wrong. Words must be generated horizontally, verticaly and diagonally. Example of a 4×4 board:

| u | a | u | s |

| n | n | i | i |

| a | o | e | b |

| e | u | e | z |

    Code was completely wrong.

Another idea may be to brute force every possible path on the board and then try those saved paths to verify whether it’s a word or not.

Thanks in advance!

  • 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-17T15:11:02+00:00Added an answer on June 17, 2026 at 3:11 pm

    One way to solve this is:

    for each path on the board
        if corresponding word in dictionary
            print it
    

    To find all paths, you could adapt any graph traversal algorithm.

    Now this will be really slow, because there are a great many paths of a board that size (for a board with n cells, we can have at most n * 4 ^ (n - 1) paths, so for a 5 by 5 board, you’d have something like 25 * 2 ^ 50 ~= 10^16 paths.

    One way to improve on this is to interleave traversing the graph and checking the dictionary, aborting if the current path’s word is not a prefix of a dictionary word:

    class Board {
    
        char[][] ch;
        boolean[][] visited;
    
        Trie dictionary;
    
        void find() {
            StringBuilder prefix = new StringBuilder();
            for (int x = 0; x < maxx; x++) {
                for (int y = 0; y < maxy; y++) {
                    walk(x, y, prefix);
                }
            }
         }
    
        void walk(int x, int y, StringBuilder prefix) {
            if (!visited[x][y]) {
                visited[x][y] = true;
                prefix.append(ch[x][y]);
    
                if (dictionary.hasPrefix(prefix)) {
                    if (dictionary.contains(prefix)) {
                        System.out.println(prefix);
                    }
    
                    int firstX = Math.max(0, x - 1);
                    int lastX = Math.min(maxx, x + 1);
                    int firstY = Math.max(0, y - 1);
                    int lastY = Math.min(maxy, y + 1);
                    for (int ax = firstX; ax <= lastX; ax++) {
                        for (int ay = firstY; ay <= lastY; ay++) {
                            walk(ax, ay, prefix);
                        }
                    }
                }
    
                prefix.setLength(prefix.length() - 1);
                visited[x][y] = false;
            }
        }
    

    As you can see, the method walk invokes itself. This technique is known as recursion.

    That leaves the matter of finding a data structure for the dictionary that supports efficient prefix queries. The best such data structure is a Trie. Alas, the JDK does not contain an implementation, but fortunately, writing one isn’t hard.

    Note: The code in this answer has not been tested.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am writing an app for my school newspaper, which is run completely online
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.