0

Task: I have two dimational array of chars fullfill with random values [a-z] and need to find if there is word "ala" vertilac\horizontal\diagonal anywhere in this array.

What've i done: In my programm i go through all characters and trying to find word in all dimentionals.

What i want: I feel there is more intelegent way to do this task witch will:

  1. Work with words of different length without correcting program
  2. Replase bunch of if statements so it will be easyer

Here is my current code:

Random randomGenerator = new Random();
    char[][] arr = new char[50][50];
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            arr[i][j] = (char) (randomGenerator.nextInt(26)+97);
        }
    }
    System.out.println();
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            System.out.print(arr[i][j]+"  ");
        }
        System.out.println();
    }
    boolean ala = false;
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            if (j>1) {
                if (arr[i][j] == 'a' && arr[i][j - 1] == 'l' && arr[i][j - 2] == 'a') {//left
                    ala = true;
                    System.out.println(i + " " + j);
                } 
            }
            if (j<arr[i].length-2) {
                if (arr[i][j] == 'a' && arr[i][j + 1] == 'l' && arr[i][j + 2] == 'a') {//rigth
                    ala = true;
                    System.out.println(i + " " + j);
                } 
            }
            if (i<arr.length-2) {
                if (arr[i][j] == 'a' && arr[i + 1][j] == 'l' && arr[i + 2][j] == 'a') {//bot
                    ala = true;
                    System.out.println(i + " " + j);
                } 
            }
            if (i>1) {
                if (arr[i][j] == 'a' && arr[i-1][j] == 'l' && arr[i - 2][j] == 'a') {//top
                    ala = true;
                    System.out.println(i + " " + j);
                } 
            }
            if (i<arr.length-2 && j<arr[i].length-2) {
                if (arr[i][j] == 'a' && arr[i + 1][j + 1] == 'l' && arr[i + 2][j + 2] == 'a') {//bot&rigth
                    ala = true;
                    System.out.println(i + " " + j);
                } 
            }
            if (i>1 && j>1) {
                if (arr[i][j] == 'a' && arr[i - 1][j - 1] == 'l' && arr[i - 2][j - 2] == 'a') {//top&left
                    ala = true;
                    System.out.println(i + " " + j);
                } 
            }
            if (i>1 && j<arr[i].length-2) {
                if (arr[i][j] == 'a' && arr[i - 1][j + 1] == 'l' && arr[i - 2][j + 2] == 'a') {//top&rigth
                    ala = true;
                    System.out.println(i + " " + j);
                } 
            }
            if (i<arr.length-2 && j>1) {
                if (arr[i][j] == 'a' && arr[i + 1][j - 1] == 'l' && arr[i + 2][j - 2] == 'a') {//bot&left
                    ala = true;
                    System.out.println(i + " " + j);
                } 
            }
        }
    }
    System.out.println(ala);
davmac
  • 20,150
  • 1
  • 40
  • 68
Eugene Shymko
  • 173
  • 2
  • 11
  • Please format to make readable – Ed Heal Nov 22 '15 at 14:28
  • 1
    soo... just to clarify: @JorgeCampos this is **not** a "valid" close reason. Close reasons should state why a question is off topic on the current site. What you should write in this close reason (instead of "belongs to ...") is what your comment right now stated: "SO rules states that we help to solve specific problems" and that's enough :) – Vogel612 Nov 22 '15 at 14:43
  • @Vogel612 Good point! I will remember next time! Thanks! :) – Jorge Campos Nov 22 '15 at 14:50

1 Answers1

0

One way to optimize the code would be to use faster substring search algorithm. I am afraid you will have to reimplement those as you are not storing the data in any common data structure but using custom array access. Note, this is not going to make your code any simpler even though you can make it look better through refactoring.

To make your code simpler you can store the data in strings representing rows, columns and diagonals so you can use String.contains/String.indexOf or any sophisticated algorithm mentioned earlier. Note, the data footprint will be larger than with array.

Community
  • 1
  • 1
Oliver Gondža
  • 3,386
  • 4
  • 29
  • 49