Given an array of positive integers arr
, find a pattern of length m
that is repeated k
or more times.
A pattern is a subarray (consecutive sub-sequence) that consists of one or more values, repeated multiple times consecutively without overlapping. A pattern is defined by its length and the number of repetitions.
Return true
if there exists a pattern of length m
that is repeated k
or more times, otherwise return false
.
Example 1:
Input: arr = [1,2,4,4,4,4], m = 1, k = 3 Output: true Explanation: The pattern (4) of length 1 is repeated 4 consecutive times. Notice that pattern can be repeated k or more times but not less.
Example 2:
Input: arr = [1,2,1,2,1,1,1,3], m = 2, k = 2 Output: true Explanation: The pattern (1,2) of length 2 is repeated 2 consecutive times. Another valid pattern (2,1) is also repeated 2 times.
Example 3:
Input: arr = [1,2,1,2,1,3], m = 2, k = 3 Output: false Explanation: The pattern (1,2) is of length 2 but is repeated only 2 times. There is no pattern of length 2 that is repeated 3 or more times.
Constraints:
2 <= arr.length <= 100
1 <= arr[i] <= 100
1 <= m <= 100
2 <= k <= 100
program main
implicit none
integer, parameter :: n = 7
integer, parameter :: m = 1
integer, parameter :: k = 3
integer, dimension(n) :: arr = (/ 1, 2, 4, 4, 4, 4, 3 /)
logical :: result
result = repeatedPattern(arr, m, k)
if (result) then
print *, "True"
else
print *, "False"
end if
contains
function repeatedPattern(arr, m, k) result(repeated)
implicit none
integer, intent(in) :: arr(:), m, k
logical :: repeated
integer :: i, j, count
repeated = .false.
do i = 1, size(arr)-m+1
count = 0
do j = i, i+m-1
if (arr(j) == arr(i)) then
count = count + 1
else
exit
end if
end do
if (count >= k) then
repeated = .true.
exit
end if
end do
end function repeatedPattern
end program main
False
module repeated_pattern
implicit none
contains
function repeated_pattern(arr, m, k) result(output)
integer, intent(in) :: arr(:)
integer, intent(in) :: m
integer, intent(in) :: k
logical :: output
integer :: i, j, count
output = .false.
! loop through the array
do i = 1, size(arr) - m + 1
! check if the pattern is repeated k or more times
count = 0
do j = i, i + m - 1
if (arr(j) == arr(j + 1)) then
count = count + 1
else
exit
end if
end do
if (count >= k) then
output = .true.
exit
end if
end do
end function repeated_pattern
end module repeated_pattern
program test
use repeated_pattern
implicit none
integer, parameter :: arr = [1, 2, 4, 4, 4, 4]
integer, parameter :: m = 1
integer, parameter :: k = 3
write (*, *) repeated_pattern(arr, m, k)
end program test
temp.f95:7:25: 7 | function repeated_pattern(arr, m, k) result(output) | 1 Error: MODULE attribute of ‘repeated_pattern’ conflicts with PROCEDURE attribute at (1) temp.f95:9:29: 9 | integer, intent(in) :: arr(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:24: 10 | integer, intent(in) :: m | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:24: 11 | integer, intent(in) :: k | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:17: 12 | logical :: output | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:14:22: 14 | integer :: i, j, count | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:16:16: 16 | output = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:19:27: 19 | do i = 1, size(arr) - m + 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:22:9: 22 | count = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:19: 23 | do j = i, i + m - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:24:30: 24 | if (arr(j) == arr(j + 1)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:25:17: 25 | count = count + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:26:4: 26 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:27:4: 27 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:28:3: 28 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:29:3: 29 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:31:20: 31 | if (count >= k) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:32:15: 32 | output = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:33:4: 33 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:34:3: 34 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:36:3: 36 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:38:3: 38 | end function repeated_pattern | 1 Error: Expecting END MODULE statement at (1) temp.f95:44:5: 44 | use repeated_pattern | 1 Fatal Error: Cannot open module file ‘repeated_pattern.mod’ for reading at (1): No such file or directory compilation terminated.
def is_prefix_of_word(sentence: str, search_word: str) -> int:
words = sentence.split(' ')
for index, word in enumerate(words, start=1):
if word.startswith(search_word):
return index
return -1
The algorithm for solving this problem consists of the following steps:
1. Split the given sentence into words based on the space delimiter.
2. Iterate over the list of words.
3. For each word, check if the searchWord is a prefix (using substring in C++, startsWith()
in Java and JavaScript, and startswith()
in Python).
4. If the searchWord is a prefix of the current word, return the current index (1-indexed).
5. If no word has searchWord as a prefix, return -1.
#include <string>
#include <sstream>
int isPrefixOfWord(std::string sentence, std::string searchWord) {
std::istringstream iss(sentence);
std::string word;
int index = 1;
while (iss >> word) {
if (word.substr(0, searchWord.size()) == searchWord) {
return index;
}
index++;
}
return -1;
}
The algorithm for solving this problem consists of the following steps:
1. Split the given sentence into words based on the space delimiter.
2. Iterate over the list of words.
3. For each word, check if the searchWord is a prefix (using substring in C++, startsWith()
in Java and JavaScript, and startswith()
in Python).
4. If the searchWord is a prefix of the current word, return the current index (1-indexed).
5. If no word has searchWord as a prefix, return -1.