Search a string in Matrix Using Split function in Java - GeeksforGeeks

21 Nov, 2018
Given a string str and a matrix mat[][] of lowercase English alphabets, the task is to find whether the string str appears in the matrix (either row-wise or column-wise).
Examples:
Input: str = "GFG"
            mat[][] = {{'G', 'E', 'E', 'K', 'S'}, 
                             {'F', 'O', 'R', 'A', 'N'}, 
                             {'G', 'E', 'E', 'K', 'S'}}
Output: Yes
GFG is present in the first column.

Input: str = "SSS"
            mat[][] = {{'G', 'E', 'E', 'K', 'S'}, 
                              {'F', 'O', 'R', 'A', 'N'}, 
                              {'G', 'E', 'E', 'K', 'S'}}
Output: No

Approach:If the search string is present in any row of the matrix the following results will be produced by the split function:
If the string occupies the whole row then the split function will return an array of length zero.If the string is present in between characters then the array length will be greater than one.If the array length is one then three possible cases can be there-The string occurs in first half.The string occurs in second half.The string is not present in that row.To search the string column wise transpose the matrix and repeat step one.Print Yes if the string is found else print No.
If the string occupies the whole row then the split function will return an array of length zero.
If the string is present in between characters then the array length will be greater than one.
If the array length is one then three possible cases can be there-The string occurs in first half.The string occurs in second half.The string is not present in that row.
The string occurs in first half.
The string occurs in second half.
The string is not present in that row.
To search the string column wise transpose the matrix and repeat step one.
Print Yes if the string is found else print No.
Below is the implementation of the above approach:
// Java program to search a string in// the matrix using split functionimport java.util.*;public class GFG {      // Function to check the occurrence of string in the matrix    public static int check(String[] matrix, String string)    {        // Looping the contents in the matrix        for (String input : matrix) {              // using split operator            String[] value = input.split(string);              if (value.length >= 2 || value.length == 0) {                return 1;            }            else if (value.length == 1                     && input.length() != value[0].length()) {                return 1;            }        }        return 0;    }      // Function to transpose the given matrix    public static String[] vertical(String[] matrix)    {        String[] vertical_value = new String[matrix[0].length()];        for (int i = 0; i < matrix[0].length(); i++) {            String temp = "";            for (int j = 0; j < matrix.length; j++)                temp += matrix[j].charAt(i);            vertical_value[i] = temp;        }          // returning the transposed matrix        return vertical_value;    }      // Driver code    public static void main(String[] args)    {          // Input matrix of characters        String[] matrix = { "GEEKS", "FORAN", "GEEKS" };          // element to be searched        String search = "GFG";          // Transpose of the matrix        String[] verticalMatrix = vertical(matrix);          // Searching process        int horizontal_search = check(matrix, search);        int vertical_search = check(verticalMatrix, search);          // If found        if (horizontal_search == 1 || vertical_search == 1)            System.out.println("Yes");        else            System.out.println("No");    }}
Yes

Searching Quiz
Arrays
Java Programs
Matrix
Arrays
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Next Greater Element
Window Sliding Technique
Count pairs with given sum
Program to find sum of elements in a given array
Reversal algorithm for array rotation
Convert a String to Character array in Java
Initializing a List in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class