/*
 * Copyright (C) 2014 Pedro Vicente Gómez Sánchez.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.pedrovgs.problem48;

/**
 * Given a matrix representing a word search, can you write a method to return true if a word
 * passed
 * as argument is inside the word search?
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class WordSearch {

}

@@ -869,10 +869,2507 @@
 arch %7B%0A%0A
+  /**%0A   * Combination of iterative or recursive algorithm to solve this problem.The complexity order of%0A   * this algorithm is O(N*N*M) where N is the number of elements in every row of the matrix and M%0A   * is the number of chars in the word to search.%0A   */%0A  public boolean contains(char%5B%5D%5B%5D wordSearch, String word) %7B%0A    validateInputData(wordSearch, word);%0A%0A    for (int i = 0; i %3C wordSearch.length; i++) %7B%0A      for (int j = 0; j %3C wordSearch%5Bi%5D.length; j++) %7B%0A        if (matches(wordSearch, i, j, word.charAt(0))) %7B%0A          if (isWordInWordSearchInner(wordSearch, word, i, j)) %7B%0A            return true;%0A          %7D%0A        %7D%0A      %7D%0A    %7D%0A%0A    return false;%0A  %7D%0A%0A  /**%0A   * This could be better if you put every check if the same line with the return because%0A   * you are going to check in different directions until you find it instead of check the 8%0A   * different possibilities.%0A   */%0A  private static boolean isWordInWordSearchInner(char%5B%5D%5B%5D wordSearch, String word, int x, int y) %7B%0A    if (word.isEmpty()) %7B%0A      return true;%0A    %7D else %7B%0A      boolean firstLetter = matches(wordSearch, x, y, word.charAt(0));%0A      if (firstLetter) %7B%0A        boolean left = isWordInWordSearchInner(wordSearch, word.substring(1), x - 1, y);%0A        boolean right = isWordInWordSearchInner(wordSearch, word.substring(1), x + 1, y);%0A        boolean bottom = isWordInWordSearchInner(wordSearch, word.substring(1), x, y + 1);%0A        boolean top = isWordInWordSearchInner(wordSearch, word.substring(1), x, y - 1);%0A        boolean topLeft = isWordInWordSearchInner(wordSearch, word.substring(1), x - 1, y - 1);%0A        boolean topRight = isWordInWordSearchInner(wordSearch, word.substring(1), x + 1, y - 1);%0A        boolean bottomLeft = isWordInWordSearchInner(wordSearch, word.substring(1), x - 1, y + 1);%0A        boolean bottomRight = isWordInWordSearchInner(wordSearch, word.substring(1), x + 1, y + 1);%0A%0A        return left %7C%7C right %7C%7C bottom %7C%7C top %7C%7C topLeft %7C%7C topRight %7C%7C bottomLeft %7C%7C bottomRight;%0A      %7D%0A      return false;%0A    %7D%0A  %7D%0A%0A  private static boolean matches(char%5B%5D%5B%5D wordSearch, int x, int y, char c) %7B%0A    if (x %3C 0 %7C%7C x %3E= wordSearch.length %7C%7C y %3C 0 %7C%7C y %3E= wordSearch%5Bx%5D.length) %7B%0A      return false;%0A    %7D else %7B%0A      return wordSearch%5Bx%5D%5By%5D == c;%0A    %7D%0A  %7D%0A%0A  private void validateInputData(char%5B%5D%5B%5D wordSearch, String word) %7B%0A    if (word == null %7C%7C wordSearch == null) %7B%0A      throw new IllegalArgumentException(%22You can't pass null instances as parameter.%22);%0A    %7D%0A  %7D%0A
 %7D%0A

Add solution to problem 48