Menu
Coddy logo textTech

Pattern Finder

Part of the Fundamentals section of Coddy's Java journey — lesson 72 of 73.

challenge icon

Challenge

Easy

Create a program that receives two arrays of strings as input and determines if the second array appears as a pattern within the first array (in consecutive order).

A pattern exists when all elements of the second array appear together, in the same order, somewhere within the first array, like finding a substring within a string.

For example:

String str1 = {"1", "2", "3", "4", "5", "6", "7"};
String str2 = {"3", "4", "5"};
// Should return true
String str1 = {"5", "6", "7", "8", "9"};
String str2 = {"6", "8"};
// Should return false (not consecutive)

Try it yourself

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String arrString1 = scanner.nextLine();
        String arrString2 = scanner.nextLine();
        String[] str1 = arrString1.split(",");
        String[] str2 = arrString2.split(",");
        // Write your code below
        
    }
}

All lessons in Fundamentals