@Test public void wordMatch() { char[][] charArray = {{'a', 'e', 'f'}, {'d', 'x', 'j'}, {'m', 'q', 'h'}}; int arrayWidth = 3; String testStr1 = "adx"; String testStr2 = "fjh"; String testStr3 = "ada"; String testStr4 = "axh"; assert StringRelated.wordMatch(charArray, arrayWidth, testStr1); assert StringRelated.wordMatch(charArray, arrayWidth, testStr2); assert StringRelated.wordMatch(charArray, arrayWidth, testStr3); assert !StringRelated.wordMatch(charArray, arrayWidth, testStr4); }
public static boolean wordMatch(char[][] chars, int arrayWidth, String word) { boolean result = false; if (chars == null || word == null || word.isEmpty() || chars.length + arrayWidth < word.length()) { return result; } int arrayLength = chars.length; char wordHead = word.charAt(0); final int defaultIndex = -1; int arrayIndexI = defaultIndex; int arrayIndexJ = defaultIndex; for (int i = 0; i < arrayLength; i++) { for (int j = 0; j < arrayWidth; j++) { if (chars[i][j] == wordHead) { arrayIndexI = i; arrayIndexJ = j; break; } } if (arrayIndexI != defaultIndex) { break; } } if (arrayIndexI != defaultIndex) { int wordSearchIndex = 0; boolean eachSearchRes = false; while (wordSearchIndex < word.length()) { if (wordSearchIndex != 0) { if (eachSearchRes) { eachSearchRes = false; } else { break; } } wordSearchIndex++; if (wordSearchIndex == word.length()) { break; } char nextChar = word.charAt(wordSearchIndex); if (arrayIndexI > 0 && nextChar == chars[arrayIndexI - 1][arrayIndexJ]) { arrayIndexI--; eachSearchRes = true; continue; } if (arrayIndexI < arrayWidth - 1 && nextChar == chars[arrayIndexI + 1][arrayIndexJ]) { arrayIndexI++; eachSearchRes = true; continue; } if (arrayIndexJ > 0 && nextChar == chars[arrayIndexI][arrayIndexJ - 1]) { arrayIndexJ--; eachSearchRes = true; continue; } if (arrayIndexJ < arrayLength - 1 && nextChar == chars[arrayIndexI][arrayIndexJ + 1]) { arrayIndexJ++; eachSearchRes = true; continue; } } if (wordSearchIndex == word.length()) { result = true; } } return result; }
StringRelated { public static boolean wordMatch(char[][] chars, int arrayWidth, String word) { boolean result = false; if (chars == null || word == null || word.isEmpty() || chars.length + arrayWidth < word.length()) { return result; } int arrayLength = chars.length; char wordHead = word.charAt(0); final int defaultIndex = -1; int arrayIndexI = defaultIndex; int arrayIndexJ = defaultIndex; for (int i = 0; i < arrayLength; i++) { for (int j = 0; j < arrayWidth; j++) { if (chars[i][j] == wordHead) { arrayIndexI = i; arrayIndexJ = j; break; } } if (arrayIndexI != defaultIndex) { break; } } if (arrayIndexI != defaultIndex) { int wordSearchIndex = 0; boolean eachSearchRes = false; while (wordSearchIndex < word.length()) { if (wordSearchIndex != 0) { if (eachSearchRes) { eachSearchRes = false; } else { break; } } wordSearchIndex++; if (wordSearchIndex == word.length()) { break; } char nextChar = word.charAt(wordSearchIndex); if (arrayIndexI > 0 && nextChar == chars[arrayIndexI - 1][arrayIndexJ]) { arrayIndexI--; eachSearchRes = true; continue; } if (arrayIndexI < arrayWidth - 1 && nextChar == chars[arrayIndexI + 1][arrayIndexJ]) { arrayIndexI++; eachSearchRes = true; continue; } if (arrayIndexJ > 0 && nextChar == chars[arrayIndexI][arrayIndexJ - 1]) { arrayIndexJ--; eachSearchRes = true; continue; } if (arrayIndexJ < arrayLength - 1 && nextChar == chars[arrayIndexI][arrayIndexJ + 1]) { arrayIndexJ++; eachSearchRes = true; continue; } } if (wordSearchIndex == word.length()) { result = true; } } return result; } }
StringRelated { public static boolean wordMatch(char[][] chars, int arrayWidth, String word) { boolean result = false; if (chars == null || word == null || word.isEmpty() || chars.length + arrayWidth < word.length()) { return result; } int arrayLength = chars.length; char wordHead = word.charAt(0); final int defaultIndex = -1; int arrayIndexI = defaultIndex; int arrayIndexJ = defaultIndex; for (int i = 0; i < arrayLength; i++) { for (int j = 0; j < arrayWidth; j++) { if (chars[i][j] == wordHead) { arrayIndexI = i; arrayIndexJ = j; break; } } if (arrayIndexI != defaultIndex) { break; } } if (arrayIndexI != defaultIndex) { int wordSearchIndex = 0; boolean eachSearchRes = false; while (wordSearchIndex < word.length()) { if (wordSearchIndex != 0) { if (eachSearchRes) { eachSearchRes = false; } else { break; } } wordSearchIndex++; if (wordSearchIndex == word.length()) { break; } char nextChar = word.charAt(wordSearchIndex); if (arrayIndexI > 0 && nextChar == chars[arrayIndexI - 1][arrayIndexJ]) { arrayIndexI--; eachSearchRes = true; continue; } if (arrayIndexI < arrayWidth - 1 && nextChar == chars[arrayIndexI + 1][arrayIndexJ]) { arrayIndexI++; eachSearchRes = true; continue; } if (arrayIndexJ > 0 && nextChar == chars[arrayIndexI][arrayIndexJ - 1]) { arrayIndexJ--; eachSearchRes = true; continue; } if (arrayIndexJ < arrayLength - 1 && nextChar == chars[arrayIndexI][arrayIndexJ + 1]) { arrayIndexJ++; eachSearchRes = true; continue; } } if (wordSearchIndex == word.length()) { result = true; } } return result; }  }
StringRelated { public static boolean wordMatch(char[][] chars, int arrayWidth, String word) { boolean result = false; if (chars == null || word == null || word.isEmpty() || chars.length + arrayWidth < word.length()) { return result; } int arrayLength = chars.length; char wordHead = word.charAt(0); final int defaultIndex = -1; int arrayIndexI = defaultIndex; int arrayIndexJ = defaultIndex; for (int i = 0; i < arrayLength; i++) { for (int j = 0; j < arrayWidth; j++) { if (chars[i][j] == wordHead) { arrayIndexI = i; arrayIndexJ = j; break; } } if (arrayIndexI != defaultIndex) { break; } } if (arrayIndexI != defaultIndex) { int wordSearchIndex = 0; boolean eachSearchRes = false; while (wordSearchIndex < word.length()) { if (wordSearchIndex != 0) { if (eachSearchRes) { eachSearchRes = false; } else { break; } } wordSearchIndex++; if (wordSearchIndex == word.length()) { break; } char nextChar = word.charAt(wordSearchIndex); if (arrayIndexI > 0 && nextChar == chars[arrayIndexI - 1][arrayIndexJ]) { arrayIndexI--; eachSearchRes = true; continue; } if (arrayIndexI < arrayWidth - 1 && nextChar == chars[arrayIndexI + 1][arrayIndexJ]) { arrayIndexI++; eachSearchRes = true; continue; } if (arrayIndexJ > 0 && nextChar == chars[arrayIndexI][arrayIndexJ - 1]) { arrayIndexJ--; eachSearchRes = true; continue; } if (arrayIndexJ < arrayLength - 1 && nextChar == chars[arrayIndexI][arrayIndexJ + 1]) { arrayIndexJ++; eachSearchRes = true; continue; } } if (wordSearchIndex == word.length()) { result = true; } } return result; }  static void main(String[] args); static void printOddAndEvenInorderByTwoThread(); static int searchSubstring(String str1, String str2); static int searchMethod1(String str1, String str2); static int searchMethod2(String str1, String str2); static void charFrequency(String[] strArray); static int searchMaxLengthMethod1(String s, int k); static int searchMaxLengthMethod2(String s, int k); static void searchLongestRepeatedSubstringMethod1(String originStr); static String searchLongestRepeatedSubstringMethod2(String originStr); static boolean wordMatch(char[][] chars, int arrayWidth, String word); }
StringRelated { public static boolean wordMatch(char[][] chars, int arrayWidth, String word) { boolean result = false; if (chars == null || word == null || word.isEmpty() || chars.length + arrayWidth < word.length()) { return result; } int arrayLength = chars.length; char wordHead = word.charAt(0); final int defaultIndex = -1; int arrayIndexI = defaultIndex; int arrayIndexJ = defaultIndex; for (int i = 0; i < arrayLength; i++) { for (int j = 0; j < arrayWidth; j++) { if (chars[i][j] == wordHead) { arrayIndexI = i; arrayIndexJ = j; break; } } if (arrayIndexI != defaultIndex) { break; } } if (arrayIndexI != defaultIndex) { int wordSearchIndex = 0; boolean eachSearchRes = false; while (wordSearchIndex < word.length()) { if (wordSearchIndex != 0) { if (eachSearchRes) { eachSearchRes = false; } else { break; } } wordSearchIndex++; if (wordSearchIndex == word.length()) { break; } char nextChar = word.charAt(wordSearchIndex); if (arrayIndexI > 0 && nextChar == chars[arrayIndexI - 1][arrayIndexJ]) { arrayIndexI--; eachSearchRes = true; continue; } if (arrayIndexI < arrayWidth - 1 && nextChar == chars[arrayIndexI + 1][arrayIndexJ]) { arrayIndexI++; eachSearchRes = true; continue; } if (arrayIndexJ > 0 && nextChar == chars[arrayIndexI][arrayIndexJ - 1]) { arrayIndexJ--; eachSearchRes = true; continue; } if (arrayIndexJ < arrayLength - 1 && nextChar == chars[arrayIndexI][arrayIndexJ + 1]) { arrayIndexJ++; eachSearchRes = true; continue; } } if (wordSearchIndex == word.length()) { result = true; } } return result; }  static void main(String[] args); static void printOddAndEvenInorderByTwoThread(); static int searchSubstring(String str1, String str2); static int searchMethod1(String str1, String str2); static int searchMethod2(String str1, String str2); static void charFrequency(String[] strArray); static int searchMaxLengthMethod1(String s, int k); static int searchMaxLengthMethod2(String s, int k); static void searchLongestRepeatedSubstringMethod1(String originStr); static String searchLongestRepeatedSubstringMethod2(String originStr); static boolean wordMatch(char[][] chars, int arrayWidth, String word);  }