Given a string array words, return an array of all characters that show up in all strings within the words (including duplicates). You may return the answer in any order.

Example 1:

Input: words = ["bella","label","roller"] Output: ["e","l","l"]

Example 2:

Input: words = ["cool","lock","cook"] Output: ["c","o"]

Constraints:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 100
  • words[i] consists of lowercase English letters.

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    character(len=:), allocatable :: words(:)
    character(len=:), allocatable :: chars(:)
    integer :: i, j, k

    ! Example 1
    words = ["bella", "label", "roller"]
    call getChars(words, chars)
    print "(A)", chars

    ! Example 2
    words = ["cool", "lock", "cook"]
    call getChars(words, chars)
    print "(A)", chars

contains

    subroutine getChars(words, chars)
        implicit none
        character(len=*), intent(in) :: words(:)
        character(len=*), intent(out) :: chars(:)
        integer :: n, m
        logical :: found(26)

        n = size(words)
        m = 0
        do i = 1, n
            found = .false.
            do j = 1, n
                if (j /= i) then
                    do k = 1, len(words(j))
                        if (.not. found(ichar(words(j)(k:k)))) then
                            found(ichar(words(j)(k:k))) = .true.
                        end if
                    end do
                end if
            end do
            do k = 1, 26
                if (found(k)) then
                    m = m + 1
                end if
            end do
        end do

        allocate(chars(m))
        m = 0
        do i = 1, n
            do j = 1, len(words(i))
                if (.not. found(ichar(words(i)(j:j)))) then
                    found(ichar(words(i)(j:j))) = .true.
                    m = m + 1
                    chars(m) = words(i)(j:j)
                end if
            end do
        end do

    end subroutine getChars

end program main
❌ Compiled
❌ Executed
❌ Correct
! Declare the input and output variables
integer, parameter :: num_words = 100
integer, parameter :: max_word_length = 100
character(len=max_word_length), dimension(num_words) :: words
character, dimension(max_word_length) :: common_chars

! Declare the local variables
integer :: i, j, k
integer :: num_common_chars

! Initialize the common characters array
common_chars = ""

! Loop through each word in the input array
do i = 1, num_words
    ! Loop through each character in the current word
    do j = 1, len_trim(words(i))
        ! Check if the current character is already in the common characters array
        do k = 1, num_common_chars
            if (common_chars(k) == words(i)(j:j)) exit
        end do
        
        ! If the current character is not in the common characters array, add it
        if (k == num_common_chars + 1) then
            num_common_chars = num_common_chars + 1
            common_chars(num_common_chars) = words(i)(j:j)
        end if
    end do
end do

! Sort the common characters array
call sort(common_chars)

! Print the common characters array
do i = 1, num_common_chars
    write (*,*) common_chars(i)
end do

end

! Sort the input array of characters
subroutine sort(array)
implicit none

! Declare the input and output variables
character, dimension(:) :: array

! Declare the local variables
integer :: i, j
character :: temp

! Loop through each element in the array
do i = 1, size(array) - 1
    ! Loop through each element after the current one
    do j = i + 1, size(array)
        ! Check if the current element is greater than the next element
        if (array(i) > array(j)) then
            ! Swap the elements
            temp = array(i)
            array(i) = array(j)
            array(j) = temp
        end if
    end do
end do

end subroutine sort
🌐 Data from online sources
function maxWidthRamp(nums) {
    const indices = [...Array(nums.length).keys()].sort((i, j) => {
        return nums[i] === nums[j] ? i - j : nums[i] - nums[j];
    });

    let maxWidth = 0;
    let minIndex = Infinity;
    for (const index of indices) {
        maxWidth = Math.max(maxWidth, index - minIndex);
        minIndex = Math.min(minIndex, index);
    }

    return maxWidth;
}
  1. First, create an array of indices of the input array.
  2. Sort the indices based on its corresponding value in the input array. In case of equal values, sort based on the index itself.
  3. Initialize maxWidth (maximum width of ramp) with 0 and minIndex (smallest index encountered so far) with maximum integer value.
  4. Iterate through the sorted indices array. Each iteration, calculate the width between the current index and minIndex, and update maxWidth if the calculated width is greater than the current max width. Update minIndex with the smallest value between the current index and minIndex.
  5. At the end of the iteration, maxWidth will hold the maximum width of a ramp in the nums array, and this value is returned.
🌐 Data from online sources
#include <vector>
#include <algorithm>

int maxWidthRamp(std::vector<int>& nums) {
    std::vector<int> indices(nums.size());
    for (int i = 0; i < nums.size(); ++i) {
        indices[i] = i;
    }

    std::sort(indices.begin(), indices.end(), [&](int i, int j) {
        return nums[i] == nums[j] ? i < j : nums[i] < nums[j];
    });

    int maxWidth = 0;
    int minIndex = std::numeric_limits<int>::max();
    for (int index : indices) {
        maxWidth = std::max(maxWidth, index - minIndex);
        minIndex = std::min(minIndex, index);
    }

    return maxWidth;
}
  1. First, create an array of indices of the input array.
  2. Sort the indices based on its corresponding value in the input array. In case of equal values, sort based on the index itself.
  3. Initialize maxWidth (maximum width of ramp) with 0 and minIndex (smallest index encountered so far) with maximum integer value.
  4. Iterate through the sorted indices array. Each iteration, calculate the width between the current index and minIndex, and update maxWidth if the calculated width is greater than the current max width. Update minIndex with the smallest value between the current index and minIndex.
  5. At the end of the iteration, maxWidth will hold the maximum width of a ramp in the nums array, and this value is returned.