A fancy string is a string where no three consecutive characters are equal.
Given a string s
, delete the minimum possible number of characters from s
to make it fancy.
Return the final string after the deletion. It can be shown that the answer will always be unique.
Example 1:
Input: s = "leeetcode " Output: "leetcode " Explanation: Remove an 'e' from the first group of 'e's to create "leetcode ". No three consecutive characters are equal, so return "leetcode ".
Example 2:
Input: s = "aaabaaaa " Output: "aabaa " Explanation: Remove an 'a' from the first group of 'a's to create "aabaaaa ". Remove two 'a's from the second group of 'a's to create "aabaa ". No three consecutive characters are equal, so return "aabaa ".
Example 3:
Input: s = "aab " Output: "aab " Explanation: No three consecutive characters are equal, so return "aab ".
Constraints:
1 <= s.length <= 105
s
consists only of lowercase English letters.program main
implicit none
character(len=:), allocatable :: s
integer :: i, j, k, n
! Example 1
s = "leeetcode "
call solve(s)
print *, s
! Example 2
s = "aaabaaaa "
call solve(s)
print *, s
! Example 3
s = "aab "
call solve(s)
print *, s
contains
subroutine solve(s)
implicit none
character(len=*), intent(inout) :: s
logical :: found
integer :: i, j, k
do while (any(s == "ee"))
found = .false.
do i = 1, len_trim(s)-2
if (s(i:i+2) == "ee") then
found = .true.
exit
end if
end do
if (.not. found) exit
s(i:i+2) = ""
end do
do while (any(s == "aa"))
found = .false.
do i = 1, len_trim(s)-2
if (s(i:i+2) == "aa") then
found = .true.
exit
end if
end do
if (.not. found) exit
s(i:i+2) = ""
end do
do while (any(s == "bb"))
found = .false.
do i = 1, len_trim(s)-2
if (s(i:i+2) == "bb") then
found = .true.
exit
end if
end do
if (.not. found) exit
s(i:i+2) = ""
end do
do while (any(s == "cc"))
found = .false.
do i = 1, len_trim(s)-2
if (s(i:i+2) == "cc") then
found = .true.
exit
end if
end do
if (.not. found) exit
s(i:i+2) = ""
end do
do while (any(s == "dd"))
found = .false.
do i = 1, len_trim(s)-2
if (s(i:i+2) == "dd") then
found = .true.
exit
end if
end do
if (.not. found) exit
s(i:i+2) = ""
end do
do while (any(s == "ff"))
found = .false.
do i = 1, len_trim(s)-2
if (s(i:i+2) == "ff") then
found = .true.
exit
end if
end do
if (.not. found) exit
s(i:i+2) = ""
end do
do while (any(s == "gg"))
found = .false.
do i = 1, len_trim(s)-2
if (s(i:i+2) == "gg") then
found = .true.
exit
end if
end do
if (.not. found) exit
s(i:i+2) = ""
end do
do while (any(s == "hh"))
found = .false.
do i = 1, len_trim(s)-2
if (s(i:i+2) == "hh") then
found = .true.
exit
end if
end do
if (.not. found) exit
s(i:i+2) = ""
end do
do while (any(s == "ii"))
found = .false.
do i = 1, len_trim(s)-2
if (s(i:i+2) == "ii") then
found = .true.
exit
end if
end do
if (.not. found) exit
s(
temp.f95:135:14: 135 | s( | 1 Error: Invalid character in name at (1) f951: Error: Unexpected end of file in βtemp.f95β
program main
implicit none
integer :: i, j, k
character(len=100) :: s
character(len=100) :: result
read(*,*) s
i = 1
j = 2
k = 3
do while (i < len(s) .and. j < len(s) .and. k < len(s))
if (s(i:i) == s(j:j) .and. s(j:j) == s(k:k)) then
result = result // s(i:i)
i = i + 1
j = j + 1
k = k + 1
else
j = j + 1
k = k + 1
end if
end do
if (i < len(s)) then
result = result // s(i:)
end if
write(*,*) result
end program main
At line 8 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7901ae486960 in ??? #1 0x7901ae4874d9 in ??? #2 0x7901ae6db17b in ??? #3 0x7901ae6d4684 in ??? #4 0x7901ae6d52aa in ??? #5 0x5ae34d0fa25f in MAIN__ #6 0x5ae34d0fa597 in main
def smallest_difference_room(rooms, queries):
n = len(rooms)
k = len(queries)
rooms.sort(key=lambda x: x[0])
results = []
for i in range(k):
preferred, minSize = queries[i]
diff = float("inf")
room_idx = -1
for j in range(n):
if rooms[j][1] >= minSize:
tmp_diff = abs(preferred - rooms[j][0])
if tmp_diff < diff:
diff = tmp_diff
room_idx = rooms[j][0]
results.append(room_idx)
return results
rooms
based on the roomId
.queries
array.diff
as the maximum possible value and room_idx
as -1.rooms
array, and for each room, check if its size is greater or equal to the minSize of the current query.diff
, update diff
and room_idx
accordingly.room_idx
to the results
array.results
array. The result array will have the same length as the number of queries and contains the answers for all queries as required.#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
bool compare(const vector<int>& a, const vector<int>& b) {
return a[0] < b[0];
}
vector<int> smallest_difference_room(vector<vector<int>>& rooms, vector<vector<int>>& queries) {
int n = rooms.size();
int k = queries.size();
sort(rooms.begin(), rooms.end(), compare);
vector<int> results;
for (int i = 0; i < k; i++) {
int preferred = queries[i][0];
int minSize = queries[i][1];
int diff = INT_MAX;
int room_idx = -1;
for (int j = 0; j < n; j++) {
if (rooms[j][1] >= minSize) {
int tmp_diff = abs(preferred - rooms[j][0]);
if (tmp_diff < diff) {
diff = tmp_diff;
room_idx = rooms[j][0];
}
}
}
results.push_back(room_idx);
}
return results;
}
rooms
based on the roomId
.queries
array.diff
as the maximum possible value and room_idx
as -1.rooms
array, and for each room, check if its size is greater or equal to the minSize of the current query.diff
, update diff
and room_idx
accordingly.room_idx
to the results
array.results
array. The result array will have the same length as the number of queries and contains the answers for all queries as required.