Given two strings first and second, consider occurrences in some text of the form "first second third ", where second comes immediately after first, and third comes immediately after second.

Return an array of all the words third for each occurrence of "first second third ".

Example 1:

Input: text = "alice is a good girl she is a good student", first = "a", second = "good" Output: ["girl","student"]

Example 2:

Input: text = "we will we will rock you", first = "we", second = "will" Output: ["we","rock"]

Constraints:

  • 1 <= text.length <= 1000
  • text consists of lowercase English letters and spaces.
  • All the words in text a separated by a single space.
  • 1 <= first.length, second.length <= 10
  • first and second consist of lowercase English letters.

Note: This problem is from LeetCode.
โŒ Compiled
โŒ Executed
โŒ Correct
program main
    implicit none

    character(len=:), allocatable :: text
    character(len=:), allocatable :: first
    character(len=:), allocatable :: second
    character(len=:), allocatable :: third
    integer :: i

    call get_command_argument(1, text)
    call get_command_argument(2, first)
    call get_command_argument(3, second)

    do i = 1, size(text)
        if (index(text(i:), first // ' ' // second // ' ') > 0) then
            third = trim(adjustl(text(i+size(first)+size(second):)))
            print *, third
        end if
    end do

end program main
โŒ Compiled
โŒ Executed
โŒ Correct
! Declare variables
integer :: i, j, k, l, m, n
character(len=1000) :: text, first, second, third
character(len=1000) :: temp

! Read input
read(*,*) text
read(*,*) first
read(*,*) second

! Initialize variables
i = 1
j = 1
k = 1
l = 1
m = 1
n = 1

! Loop through the text
do while (i <= len(text))
    ! Check if the current character is a space
    if (text(i:i) == " ") then
        ! Increment the index
        i = i + 1
    ! Check if the current character is the first string
    else if (text(i:i+len(first)-1) == first) then
        ! Increment the index
        i = i + len(first)
        ! Check if the next character is a space
        if (text(i:i) == " ") then
            ! Increment the index
            i = i + 1
            ! Check if the next character is the second string
            if (text(i:i+len(second)-1) == second) then
                ! Increment the index
                i = i + len(second)
                ! Check if the next character is a space
                if (text(i:i) == " ") then
                    ! Increment the index
                    i = i + 1
                    ! Check if the next character is the third string
                    if (text(i:i+len(third)-1) == third) then
                        ! Increment the index
                        i = i + len(third)
                        ! Check if the next character is a space
                        if (text(i:i) == " ") then
                            ! Increment the index
                            i = i + 1
                            ! Print the third string
                            write(*,*) third
                        end if
                    end if
                end if
            end if
        end if
    end if

    ! Increment the index
    i = i + 1
end do

end [FORTRAN]
๐ŸŒ Data from online sources
def remove_outer_parentheses(s: str) -> str:
    result = []
    opened = 0

    for c in s:
        if c == '(':
            if opened > 0:
                result.append(c)
            opened += 1
        elif c == ')':
            if opened > 1:
                result.append(c)
            opened -= 1

    return ''.join(result)
The algorithm is fairly simple. We will iterate through the given string `s`. For each character in the string, we will keep track of the count of open-parentheses encountered so far using the variable `opened`.
  • If we find an open-parenthesis (, and the value of opened is more than 0, we append it to the resulting string. Then, we increment the value of opened.
  • If we find a close-parenthesis ), and the value of opened is more than 1, we append it to the resulting string. Then, we decrement the value of opened.

By doing this, we effectively skip the outermost parentheses of each primitive valid parentheses string, as required by the problem while constructing the result string.

๐ŸŒ Data from online sources
#include <string>
#include <stack>

std::string removeOuterParentheses(std::string s) {
    std::string result;
    int opened = 0;

    for (char c : s) {
        if (c == '(' && opened++ > 0) {
            result += c;
        }
        if (c == ')' && opened-- > 1) {
            result += c;
        }
    }
    return result;
}
The algorithm is fairly simple. We will iterate through the given string `s`. For each character in the string, we will keep track of the count of open-parentheses encountered so far using the variable `opened`.
  • If we find an open-parenthesis (, and the value of opened is more than 0, we append it to the resulting string. Then, we increment the value of opened.
  • If we find a close-parenthesis ), and the value of opened is more than 1, we append it to the resulting string. Then, we decrement the value of opened.

By doing this, we effectively skip the outermost parentheses of each primitive valid parentheses string, as required by the problem while constructing the result string.