You are given an integer array nums
with the following properties:
nums.length == 2 * n
.nums
contains n + 1
unique elements.nums
is repeated n
times.Return the element that is repeated n
times.
Example 1:
Input: nums = [1,2,3,3] Output: 3
Example 2:
Input: nums = [2,1,2,5,3,2] Output: 2
Example 3:
Input: nums = [5,1,5,2,5,3,5,4] Output: 5
Constraints:
2 <= n <= 5000
nums.length == 2 * n
0 <= nums[i] <= 104
nums
contains n + 1
unique elements and one of them is repeated exactly n
times.program main
implicit none
integer, parameter :: n = 2
integer, dimension(2*n) :: nums
integer :: i, j, count
logical :: found
! Example 1
nums = [1, 2, 3, 3]
write (*, '(A, I0)') 'Example 1: ', find_repeated(nums)
! Example 2
nums = [2, 1, 2, 5, 3, 2]
write (*, '(A, I0)') 'Example 2: ', find_repeated(nums)
! Example 3
nums = [5, 1, 5, 2, 5, 3, 5, 4]
write (*, '(A, I0)') 'Example 3: ', find_repeated(nums)
contains
function find_repeated(nums) result(repeated)
implicit none
integer, dimension(:), intent(in) :: nums
integer :: repeated
integer :: i, j, count
logical :: found
repeated = -1
do i = 1, size(nums) - 1
count = 0
found = .false.
do j = i + 1, size(nums)
if (nums(j) == nums(i)) then
count = count + 1
found = .true.
end if
end do
if (found .and. count == n) then
repeated = nums(i)
exit
end if
end do
end function find_repeated
end program main
temp.f95:14:4: 14 | nums = [2, 1, 2, 5, 3, 2] | 1 Error: Different shape for array assignment at (1) on dimension 1 (4 and 6) temp.f95:18:4: 18 | nums = [5, 1, 5, 2, 5, 3, 5, 4] | 1 Error: Different shape for array assignment at (1) on dimension 1 (4 and 8)
module RepeatedNTimes
implicit none
private
public :: repeatedNTimes
contains
function repeatedNTimes(nums) result(repeatedElement)
integer, intent(in) :: nums(:)
integer :: repeatedElement
integer :: count, i
count = 0
repeatedElement = 0
do i = 1, size(nums)
if (count == 0) then
repeatedElement = nums(i)
end if
if (nums(i) == repeatedElement) then
count = count + 1
else
count = 0
end if
end do
if (count == 0) then
write (*,*) "No repeated element found"
else
write (*,*) "Repeated element: ", repeatedElement
end if
end function repeatedNTimes
end module RepeatedNTimes
program test_repeatedNTimes
use RepeatedNTimes
implicit none
integer, parameter :: n = 4
integer :: nums(2*n)
integer :: i
! Example 1
nums = [1, 2, 3, 3]
call repeatedNTimes(nums)
! Example 2
nums = [2, 1, 2, 5, 3, 2]
call repeatedNTimes(nums)
! Example 3
nums = [5, 1, 5, 2, 5, 3, 5, 4]
call repeatedNTimes(nums)
! Example 4 (no repeated element)
nums = [1, 2, 3, 4, 5, 6]
call repeatedNTimes(nums)
end program test_repeatedNTimes
temp.f95:4:28: 4 | public :: repeatedNTimes | 1 Error: PUBLIC attribute applied to MODULE repeatedntimes at (1) temp.f95:6:27: 6 | function repeatedNTimes(nums) result(repeatedElement) | 1 Error: MODULE attribute of ‘repeatedntimes’ conflicts with PROCEDURE attribute at (1) temp.f95:7:38: 7 | integer, intent(in) :: nums(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:8:34: 8 | integer :: repeatedElement | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:27: 9 | integer :: count, i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:17: 11 | count = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:12:27: 12 | repeatedElement = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:14:28: 14 | do i = 1, size(nums) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:15:32: 15 | if (count == 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:16:41: 16 | repeatedElement = nums(i) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:17:15: 17 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:18:48: 18 | if (nums(i) == repeatedElement) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:19:33: 19 | count = count + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:16: 20 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:21:25: 21 | count = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:22:15: 22 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:23:11: 23 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:28: 25 | if (count == 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:26:51: 26 | write (*,*) "No repeated element found" | 1 Error: Unexpected WRITE statement in CONTAINS section at (1) temp.f95:27:12: 27 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:28:61: 28 | write (*,*) "Repeated element: ", repeatedElement | 1 Error: Unexpected WRITE statement in CONTAINS section at (1) temp.f95:29:11: 29 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:30:7: 30 | end function repeatedNTimes | 1 Error: Expecting END MODULE statement at (1) temp.f95:34:9: 34 | use RepeatedNTimes | 1 Fatal Error: Cannot open module file ‘repeatedntimes.mod’ for reading at (1): No such file or directory compilation terminated.
def isLongPressedName(name: str, typed: str) -> bool:
i, j = 0, 0
while j < len(typed):
if i < len(name) and name[i] == typed[j]:
i += 1
elif j == 0 or typed[j] != typed[j - 1]:
return False
j += 1
return i == len(name)
We initialize two pointers i and j, pointing to the current characters in the name
and typed
strings. We iterate through the typed
string using the pointer j, and check the following conditions in each iteration:
typed
string (j) does not match the character at position j - 1, it means that there is no long press, and we return False since it cannot be a valid typing.After the loop, we check if the pointer i reached the end of the name
string. If it did, it means that all characters in the name
string were found at the typed
string with some characters possibly being long pressed. If not, it implies that the typed
string is not a valid typing of the name
string, and we return False.
bool isLongPressedName(string name, string typed) {
int i = 0, j = 0;
while (j < typed.length()) {
if (i < name.length() && name[i] == typed[j]) {
i++;
} else if (j == 0 || typed[j] != typed[j - 1]) {
return false;
}
j++;
}
return i == name.length();
}
We initialize two pointers i and j, pointing to the current characters in the name
and typed
strings. We iterate through the typed
string using the pointer j, and check the following conditions in each iteration:
typed
string (j) does not match the character at position j - 1, it means that there is no long press, and we return False since it cannot be a valid typing.After the loop, we check if the pointer i reached the end of the name
string. If it did, it means that all characters in the name
string were found at the typed
string with some characters possibly being long pressed. If not, it implies that the typed
string is not a valid typing of the name
string, and we return False.