Check if the Sentence Is Pangram

🏠 ⬅️ ➡️

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.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
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
Compiled
Executed
Correct
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
🌐 Data from online sources
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.

🌐 Data from online sources
#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.