You are given a string s
, where every two consecutive vertical bars '|'
are grouped into a pair. In other words, the 1st and 2nd '|'
make a pair, the 3rd and 4th '|'
make a pair, and so forth.
Return the number of '*'
in s
, excluding the '*'
between each pair of '|'
.
Note that each '|'
will belong to exactly one pair.
Example 1:
Input: s = "l|*e*et|c**o|*de| " Output: 2 Explanation: The considered characters are underlined: "l|*e*et|c**o|*de| ". The characters between the first and second '|' are excluded from the answer. Also, the characters between the third and fourth '|' are excluded from the answer. There are 2 asterisks considered. Therefore, we return 2.
Example 2:
Input: s = "iamprogrammer " Output: 0 Explanation: In this example, there are no asterisks in s. Therefore, we return 0.
Example 3:
Input: s = "yo|uar|e**|b|e***au|tifu|l " Output: 5 Explanation: The considered characters are underlined: "yo|uar|e**|b|e***au|tifu|l ". There are 5 asterisks considered. Therefore, we return 5.
Constraints:
1 <= s.length <= 1000
s
consists of lowercase English letters, vertical bars '|'
, and asterisks '*'
.s
contains an even number of vertical bars '|'
.program main
implicit none
character(len=:), allocatable :: s
integer :: n
s = "l|\*e\*et|c\*\*o|\*de|"
write(*,*) "Example 1:"
write(*,*) "Input: ", s
n = count_asterisk(s)
write(*,*) "Output: ", n
write(*,*) ""
s = "iamprogrammer "
write(*,*) "Example 2:"
write(*,*) "Input: ", s
n = count_asterisk(s)
write(*,*) "Output: ", n
write(*,*) ""
s = "yo|uar|e\*\*|b|e\*\*\*au|tifu|l "
write(*,*) "Example 3:"
write(*,*) "Input: ", s
n = count_asterisk(s)
write(*,*) "Output: ", n
write(*,*) ""
contains
function count_asterisk(str) result(n)
implicit none
character(len=*), intent(in) :: str
integer :: i, j, n
n = 0
do i = 1, len(str)-1, 2
if (str(i:i+1) == "|") then
cycle
end if
do j = i+2, len(str)-1, 2
if (str(j:j+1) == "|") then
exit
end if
if (str(j:j+1) == "*") then
n = n + 1
end if
end do
end do
end function count_asterisk
end program main
Example 1: Input: l|\*e\*et|c\*\*o|\*de| Output: 0 Example 2: Input: iamprogrammer Output: 0 Example 3: Input: yo|uar|e\*\*|b|e\*\*\*au|tifu|l Output: 0
! Declare variables
integer :: i, j, k, n, m
character(len=1000) :: s
! Read input
read(*,*) s
! Initialize variables
n = 0
m = 0
! Loop through the string
do i = 1, len_trim(s)
! Check if the current character is an asterisk
if (s(i:i) == '*') then
! Increment the number of asterisks
n = n + 1
end if
! Check if the current character is a vertical bar
if (s(i:i) == '|') then
! Increment the number of vertical bars
m = m + 1
! Check if the current character is a vertical bar and the previous character is an asterisk
if (s(i-1:i-1) == '*' .and. s(i+1:i+1) == '|') then
! Decrement the number of asterisks
n = n - 1
end if
end if
end do
! Print output
write(*,*) n
end
At line 6 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7a88dc953960 in ??? #1 0x7a88dc9544d9 in ??? #2 0x7a88dcba817b in ??? #3 0x7a88dcba1684 in ??? #4 0x7a88dcba22aa in ??? #5 0x57137752621c in MAIN__ #6 0x57137752637e in main
def count_stars_except_between_pair(s: str) -> int:
count = 0
pipe_count = 0
for c in s:
if c == '|':
pipe_count += 1
elif c == '*' and pipe_count % 2 == 0:
count += 1
return count
The algorithm loops through the string with a simple for loop iterating on characters. It keeps track of the number of pipes encountered (pipeCount) and the total count of stars. If a pipe is encountered, increment pipeCount. If a star is encountered and pipeCount is even, increment the count. This condition (pipeCount % 2 == 0) ensures we are not considering stars between a pair of vertical bars. Finally, return the count of stars after processing the entire string.
int countStarsExceptBetweenPair(const std::string& s) {
int count = 0;
int pipeCount = 0;
for (char c : s) {
if (c == '|') {
pipeCount++;
} else if (c == '*' && pipeCount % 2 == 0) {
count++;
}
}
return count;
}
The algorithm loops through the string with a simple for loop iterating on characters. It keeps track of the number of pipes encountered (pipeCount) and the total count of stars. If a pipe is encountered, increment pipeCount. If a star is encountered and pipeCount is even, increment the count. This condition (pipeCount % 2 == 0) ensures we are not considering stars between a pair of vertical bars. Finally, return the count of stars after processing the entire string.