Algo.Java.DetectPatternInArray

// https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/description/

    public static boolean containsPattern(int[] arr, int m, int k) {
        // m*k maxLength of repetitions
        int n = arr.length;
        for (int i = 0; i <= n - m * k; i++) {
            boolean isPattern = true; // consider pattern is ok

            for (int j = 0; j < m * (k -1); j++) {
                if (arr[i + j] != arr[i + j + m]) {
                    isPattern = false;
                    break;
                }
            }

            if (isPattern) {
                return true;
            }
        }

        return false;
    }
This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.