You are given a string s
consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.
We repeatedly make duplicate removals on s
until we no longer can.
Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
Example 1:
Input: s = "abbaca " Output: "ca " Explanation: For example, in "abbaca " we could remove "bb " since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca ", of which only "aa " is possible, so the final string is "ca ".
Example 2:
Input: s = "azxxzy " Output: "ay "
Constraints:
1 <= s.length <= 105
s
consists of lowercase English letters.program main
implicit none
character(len=:), allocatable :: s
integer :: i, j, n
! Example 1
s = 'abbaca'
call solve(s)
print *, s
! Example 2
s = 'azxxzy'
call solve(s)
print *, s
contains
subroutine solve(s)
implicit none
character(len=*), intent(inout) :: s
logical :: done
do while (.not. done)
done = .true.
do i = 1, len_trim(s)-1
if (s(i:i+1) == s(i+1:i+2)) then
s = s(:i-1) // s(i+2:)
done = .false.
exit
end if
end do
end do
end subroutine solve
end program main
abbaca azxxzy
module main
implicit none
interface
function remove_duplicates(s) result(res)
implicit none
character(len=*), intent(in) :: s
character(len=len(s)) :: res
end function remove_duplicates
end interface
character(len=100000) :: s
character(len=100000) :: res
s = "abbaca "
res = remove_duplicates(s)
write (*,*) res
s = "azxxzy "
res = remove_duplicates(s)
write (*,*) res
contains
function remove_duplicates(s) result(res)
implicit none
character(len=*), intent(in) :: s
character(len=len(s)) :: res
integer :: i, j
res = s
do while (any(res(2:len(res)) == res(1:len(res)-1)))
do i = 1, len(res) - 1
if (res(i) == res(i+1)) then
res(i:i+1) = ""
exit
end if
end do
end do
end function remove_duplicates
end module main
temp.f95:18:13: 18 | s = "abbaca " | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:19:26: 19 | res = remove_duplicates(s) | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:20:15: 20 | write (*,*) res | 1 Error: Unexpected WRITE statement in MODULE at (1) temp.f95:22:13: 22 | s = "azxxzy " | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:23:26: 23 | res = remove_duplicates(s) | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:24:15: 24 | write (*,*) res | 1 Error: Unexpected WRITE statement in MODULE at (1) temp.f95:28:27: 7 | function remove_duplicates(s) result(res) | 2 ...... 28 | function remove_duplicates(s) result(res) | 1 Error: Procedure ‘remove_duplicates’ at (1) is already defined at (2) temp.f95:29:17: 29 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:30:37: 30 | character(len=*), intent(in) :: s | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:31:32: 31 | character(len=len(s)) :: res | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:32:19: 32 | integer :: i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:34:11: 34 | res = s | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:36:56: 36 | do while (any(res(2:len(res)) == res(1:len(res)-1))) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:37:30: 37 | do i = 1, len(res) - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:38:20: 38 | if (res(i) == res(i+1)) then | 1 Error: Syntax error in IF-expression at (1) temp.f95:40:20: 40 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:41:15: 41 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:42:11: 42 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:43:7: 43 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:45:3: 45 | end function remove_duplicates | 1 Error: Expecting END MODULE statement at (1)
import heapq
def maxSumAfterKOperations(nums, k):
heapq.heapify(nums)
for _ in range(k):
current = heapq.heappop(nums)
heapq.heappush(nums, -current)
return sum(nums)
Note: In JavaScript, we have also created a PriorityQueue class to achieve the functionality of a priority queue like in other languages.
int maxSumAfterKOperations(vector<int>& nums, int k) {
priority_queue<int, vector<int>, greater<int>> pq(nums.begin(), nums.end());
while(k--){
int current = pq.top();
pq.pop();
pq.push(-current);
}
return accumulate(nums.begin(), nums.end(), 0);
}
Note: In JavaScript, we have also created a PriorityQueue class to achieve the functionality of a priority queue like in other languages.