Delete Characters to Make Fancy String

🏠 ⬅️ ➑️

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.

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
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(
βœ… Compiled
❌ Executed
❌ Correct
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
🌐 Data from online sources
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
  1. Sort the input rooms based on the roomId.
  2. Iterate through the queries array.
  3. For each query, initialize diff as the maximum possible value and room_idx as -1.
  4. Iterate through the rooms array, and for each room, check if its size is greater or equal to the minSize of the current query.
  5. If it is, calculate the absolute difference between the preferred room number and the current room number.
  6. If this absolute difference is less than the current minimum diff, update diff and room_idx accordingly.
  7. Once all rooms have been processed for a given query, append room_idx to the results array.
  8. Repeat this process for all queries and return the results array. The result array will have the same length as the number of queries and contains the answers for all queries as required.
🌐 Data from online sources
#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;
}
  1. Sort the input rooms based on the roomId.
  2. Iterate through the queries array.
  3. For each query, initialize diff as the maximum possible value and room_idx as -1.
  4. Iterate through the rooms array, and for each room, check if its size is greater or equal to the minSize of the current query.
  5. If it is, calculate the absolute difference between the preferred room number and the current room number.
  6. If this absolute difference is less than the current minimum diff, update diff and room_idx accordingly.
  7. Once all rooms have been processed for a given query, append room_idx to the results array.
  8. Repeat this process for all queries and return the results array. The result array will have the same length as the number of queries and contains the answers for all queries as required.