A pangram is a sentence where every letter of the English alphabet appears at least once.
Given a string sentence
containing only lowercase English letters, return true
if sentence
is a pangram, or false
otherwise.
Example 1:
Input: sentence = "thequickbrownfoxjumpsoverthelazydog " Output: true Explanation: sentence contains at least one of every letter of the English alphabet.
Example 2:
Input: sentence = "leetcode " Output: false
Constraints:
1 <= sentence.length <= 1000
sentence
consists of lowercase English letters.program pangrams
implicit none
character(len=100) :: sentence
logical :: is_pangram
! Example 1
sentence = 'thequickbrownfoxjumpsoverthelazydog'
if (is_pangram(sentence)) then
print *, 'True'
else
print *, 'False'
end if
! Example 2
sentence = 'leetcode'
if (is_pangram(sentence)) then
print *, 'True'
else
print *, 'False'
end if
contains
function is_pangram(sentence) result(is_pangram_)
implicit none
character(len=*), intent(in) :: sentence
integer :: i
logical :: is_pangram_
is_pangram_ = .true.
do i = 97, 122
if (index(sentence, char(i)) == 0) then
is_pangram_ = .false.
exit
end if
end do
end function is_pangram
end program pangrams
temp.f95:25:24: 25 | function is_pangram(sentence) result(is_pangram_) | 1 Error: Procedure ‘is_pangram’ at (1) has an explicit interface from a previous declaration temp.f95:26:21: 26 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:27:48: 27 | character(len=*), intent(in) :: sentence | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:28:20: 28 | integer :: i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:29:30: 29 | logical :: is_pangram_ | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:31:28: 31 | is_pangram_ = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:32:22: 32 | do i = 97, 122 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:33:51: 33 | if (index(sentence, char(i)) == 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:34:37: 34 | is_pangram_ = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:35:20: 35 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:36:15: 36 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:37:11: 37 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:38:7: 38 | end function is_pangram | 1 Error: Expecting END PROGRAM statement at (1)
module pangram
implicit none
contains
function is_pangram(sentence) result(is_pangram)
character(len=*), intent(in) :: sentence
logical :: is_pangram
character(len=26) :: alphabet
integer :: i
alphabet = 'abcdefghijklmnopqrstuvwxyz'
is_pangram = .true.
do i = 1, 26
if (index(sentence, alphabet(i:i)) == 0) then
is_pangram = .false.
exit
end if
end do
end function is_pangram
end module pangram
program main
use pangram
implicit none
character(len=1000) :: sentence
logical :: is_pangram
sentence = "thequickbrownfoxjumpsoverthelazydog "
is_pangram = is_pangram(sentence)
if (is_pangram) then
print *, "True"
else
print *, "False"
end if
sentence = "leetcode "
is_pangram = is_pangram(sentence)
if (is_pangram) then
print *, "True"
else
print *, "False"
end if
end program main
temp.f95:7:48: 7 | function is_pangram(sentence) result(is_pangram) | 1 Error: RESULT variable at (1) must be different than function name temp.f95:9:40: 9 | character(len=*), intent(in) :: sentence | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:21: 10 | logical :: is_pangram | 1 Error: Symbol ‘is_pangram’ at (1) has already been host associated temp.f95:12:29: 12 | character(len=26) :: alphabet | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:12: 13 | integer :: i | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:15:39: 15 | alphabet = 'abcdefghijklmnopqrstuvwxyz' | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:11: 16 | is_pangram = .true. | 1 Error: Symbol ‘is_pangram’ at (1) has already been host associated temp.f95:18:12: 18 | do i = 1, 26 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:19:35: 19 | if (index(sentence, alphabet(i:i)) == 0) then | 1 Error: Syntax error in argument list at (1) temp.f95:20:19: 20 | is_pangram = .false. | 1 Error: Symbol ‘is_pangram’ at (1) has already been host associated temp.f95:21:12: 21 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:22:7: 22 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:23:3: 23 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:3: 25 | end function is_pangram | 1 Error: Expecting END MODULE statement at (1) temp.f95:31:5: 31 | use pangram | 1 Fatal Error: Cannot open module file ‘pangram.mod’ for reading at (1): No such file or directory compilation terminated.
from bisect import bisect_left
def min_operations(target, arr):
index_map = {num: i for i, num in enumerate(target)}
lis = []
for num in arr:
if num in index_map:
pos = bisect_left(lis, index_map[num])
if pos == len(lis):
lis.append(index_map[num])
else:
lis[pos] = index_map[num]
return len(target) - len(lis)
The algorithm first creates a mapping of the target integers to their index positions. This mapping will later be used to determine if a number in arr
is in the target sequence and its relative order. It then initializes an empty list called lis
(Longest Increasing Subsequence) and iterates through arr
.
For each number num
in arr
, if it's also in the target, we check if that number can be appended to the end of lis
. If not, it replaces the first number in lis
that is greater than or equal to it to maintain the sorted order. This is done using lowerBound
(C++, JavaScript) or bisect_left
(Python) or Arrays.binarySearch
(Java) functions - all of which perform binary search to find the position at which the new number should be inserted.
Finally, the algorithm returns the difference between the lengths of the target sequence and lis
, which represents the minimum number of operations needed to make target
a subsequence of arr
.
#include <vector>
#include <unordered_map>
int minOperations(std::vector<int>& target, std::vector<int>& arr) {
std::unordered_map<int, int> index_map;
for (int i = 0; i < target.size(); ++i) {
index_map[target[i]] = i;
}
std::vector<int> lis;
for (int num : arr) {
if (index_map.count(num)) {
int pos = std::lower_bound(lis.begin(), lis.end(), index_map[num]) - lis.begin();
if (pos == lis.size()) {
lis.push_back(index_map[num]);
} else {
lis[pos] = index_map[num];
}
}
}
return target.size() - lis.size();
}
The algorithm first creates a mapping of the target integers to their index positions. This mapping will later be used to determine if a number in arr
is in the target sequence and its relative order. It then initializes an empty list called lis
(Longest Increasing Subsequence) and iterates through arr
.
For each number num
in arr
, if it's also in the target, we check if that number can be appended to the end of lis
. If not, it replaces the first number in lis
that is greater than or equal to it to maintain the sorted order. This is done using lowerBound
(C++, JavaScript) or bisect_left
(Python) or Arrays.binarySearch
(Java) functions - all of which perform binary search to find the position at which the new number should be inserted.
Finally, the algorithm returns the difference between the lengths of the target sequence and lis
, which represents the minimum number of operations needed to make target
a subsequence of arr
.