Detect Pattern of Length M Repeated K or More Times - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Sunday 30 August 2020

Detect Pattern of Length M Repeated K or More Times




Detect Pattern of Length M Repeated K or More Times


Detect Pattern of Length M Repeated K or More Times 

 
C++ 

class Solution {

public:

    bool containsPattern(vector<int>& arr, int m, int k) {

        int cnt=0;

        for(int i=0;m+i<arr.size();++i){

            if(arr[i]!=arr[i+m])

                cnt=0;

            cnt+=(arr[i]==arr[i+m]);

                if(cnt==(k-1)*m)return true;

        }

        return false;

    }

};



-----------------------------------------------------------------------------------------------------------------------

JAVA

public boolean containsPattern(int[] arr, int m, int k) {
        for(int i = 0, n = arr.length, j = i + m, count = 0; j < n; ++i, ++j) {
            if (arr[i] != arr[j]) {
                count = 0;
            } else if (++count >= (k - 1) * m) {
                return true;
            }
        }
        return false

    }

---------------------------------------------------------------------------------------------------------------------- 

PYTHON

def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
	i = 0
	while i <= len(arr)-1:
		p = arr[i:i+m]
		if p * k == arr[i:i+m*k]:
			return True

		i += 1

	return False

--------------------------------------------------------------------------------------