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.text
a separated by a single space.1 <= first.length, second.length <= 10
first
and second
consist of lowercase English letters.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
temp.f95:16:57: 16 | third = trim(adjustl(text(i+size(first)+size(second):))) | 1 Error: โarrayโ argument of โsizeโ intrinsic at (1) must be an array temp.f95:14:19: 14 | do i = 1, size(text) | 1 Error: โarrayโ argument of โsizeโ intrinsic at (1) must be an array
! 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]
temp.f95:62:3: 62 | end [FORTRAN] | 1 Error: Expecting END PROGRAM statement at (1) f951: Error: Unexpected end of file in โtemp.f95โ
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`.
(
, and the value of opened
is more than 0, we append it to the resulting string. Then, we increment the value of opened
.)
, 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.
#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`.
(
, and the value of opened
is more than 0, we append it to the resulting string. Then, we increment the value of opened
.)
, 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.