Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false.

Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j].

  • For example, swapping at indices 0 and 2 in "abcd " results in "cbad ".

Example 1:

Input: s = "ab ", goal = "ba " Output: true Explanation: You can swap s[0] = 'a' and s[1] = 'b' to get "ba ", which is equal to goal.

Example 2:

Input: s = "ab ", goal = "ab " Output: false Explanation: The only letters you can swap are s[0] = 'a' and s[1] = 'b', which results in "ba " != goal.

Example 3:

Input: s = "aa ", goal = "aa " Output: true Explanation: You can swap s[0] = 'a' and s[1] = 'a' to get "aa ", which is equal to goal.

Constraints:

  • 1 <= s.length, goal.length <= 2 * 104
  • s and goal consist of lowercase letters.

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    character(len=:), allocatable :: s, goal
    logical :: res

    ! Example 1
    s = "ab "
    goal = "ba "
    res = solve(s, goal)
    print "(L1)", res

    ! Example 2
    s = "ab "
    goal = "ab "
    res = solve(s, goal)
    print "(L1)", res

    ! Example 3
    s = "aa "
    goal = "aa "
    res = solve(s, goal)
    print "(L1)", res

contains

    function solve(s, goal) result(res)
        implicit none
        character(len=*), intent(in) :: s, goal
        integer :: i, j
        logical :: found

        res = .false.
        do i = 1, len(s)-1
            do j = i+1, len(s)
                if (s(i:i) /= s(j:j)) then
                    if (s(i:i) == goal(j:j)) then
                        res = .true.
                        exit
                    else if (s(j:j) == goal(i:i)) then
                        res = .true.
                        exit
                    end if
                end if
            end do
            if (res) exit
        end do
    end function solve
end program main
❌ Compiled
❌ Executed
❌ Correct
module main

implicit none

interface

    function is_swap_possible(s, goal) result(res)
        implicit none
        character(len=*), intent(in) :: s, goal
        logical :: res
    end function is_swap_possible

end interface

character(len=200000) :: s, goal

s = "ab "
goal = "ba "

if (is_swap_possible(s, goal)) then
    print *, "true"
else
    print *, "false"
end if

s = "ab "
goal = "ab "

if (is_swap_possible(s, goal)) then
    print *, "true"
else
    print *, "false"
end if

s = "aa "
goal = "aa "

if (is_swap_possible(s, goal)) then
    print *, "true"
else
    print *, "false"
end if

contains

function is_swap_possible(s, goal) result(res)
    implicit none
    character(len=*), intent(in) :: s, goal
    integer :: i, j
    logical :: res

    res = .false.

    do i = 1, len(s) - 1
        do j = i + 1, len(s)
            if (s(i:i) == goal(j:j) .and. s(j:j) == goal(i:i)) then
                res = .true.
                exit
            end if
        end do
        if (res) exit
    end do

end function is_swap_possible

end module main
🌐 Data from online sources

class MyCircularDeque {
    constructor(k) {
        this.buffer = new Array(k);
        this.front = 0;
        this.rear = 0;
        this.size = 0;
    }

    insertFront(value) {
        if (this.size == this.buffer.length) return false;
        this.front = (this.front - 1 + this.buffer.length) % this.buffer.length;
        this.buffer[this.front] = value;
        this.size++;
        return true;
    }

    insertLast(value) {
        if (this.size == this.buffer.length) return false;
        this.buffer[this.rear] = value;
        this.rear = (this.rear + 1) % this.buffer.length;
        this.size++;
        return true;
    }

    deleteFront() {
        if (this.size == 0) return false;
        this.front = (this.front + 1) % this.buffer.length;
        this.size--;
        return true;
    }

    deleteLast() {
        if (this.size == 0) return false;
        this.rear = (this.rear - 1 + this.buffer.length) % this.buffer.length;
        this.size--;
        return true;
    }

    getFront() {
        if (this.size == 0) return -1;
        return this.buffer[this.front];
    }

    getRear() {
        if (this.size == 0) return -1;
        return this.buffer[(this.rear - 1 + this.buffer.length) % this.buffer.length];
    }

    isEmpty() {
        return this.size == 0;
    }

    isFull() {
        return this.size == this.buffer.length;
    }
}

#

The circular double-ended queue (deque) is implemented with an array (or vector in C++), two pointers, 'front' and 'rear', and an integer 'size', which represents the current number of elements in the deque. The array has a fixed capacity, 'k'. Front represents the index of the first element in the deque, and rear points to the index after the last element in the deque.

Insertion or deletion is done by manipulating these pointers, front and rear accordingly. When the deque is full, the front and rear pointers wrap around, creating a circular structure.

The main operations are:

  1. insertFront(): inserts an element at the front of the deque. To do this, the front pointer is first decremented and then the value is inserted.
  2. insertLast(): inserts an element at the rear of the deque. The value is directly inserted at the rear and the rear pointer is incremented.
  3. deleteFront(): deletes an element from the front of the deque by incrementing the front pointer.
  4. deleteLast(): deletes an element from the rear of the deque by decrementing the rear pointer.
  5. getFront(): returns the front element of the deque.
  6. getRear(): returns the rear element of the deque.
  7. isEmpty(): returns true if the deque is empty.
  8. isFull(): returns true if the deque is full.

🌐 Data from online sources
class MyCircularDeque {
public:
    vector<int> buffer;
    int front, rear, size;

    MyCircularDeque(int k) {
        buffer.resize(k);
        front = 0;
        rear = 0;
        size = 0;
    }

    bool insertFront(int value) {
        if (size == buffer.size()) return false;
        front = (front - 1 + buffer.size()) % buffer.size();
        buffer[front] = value;
        size++;
        return true;
    }

    bool insertLast(int value) {
        if (size == buffer.size()) return false;
        buffer[rear] = value;
        rear = (rear + 1) % buffer.size();
        size++;
        return true;
    }

    bool deleteFront() {
        if (size == 0) return false;
        front = (front + 1) % buffer.size();
        size--;
        return true;
    }

    bool deleteLast() {
        if (size == 0) return false;
        rear = (rear - 1 + buffer.size()) % buffer.size();
        size--;
        return true;
    }

    int getFront() {
        if (size == 0) return -1;
        return buffer[front];
    }

    int getRear() {
        if (size == 0) return -1;
        return buffer[(rear - 1 + buffer.size()) % buffer.size()];
    }

    bool isEmpty() {
        return size == 0;
    }

    bool isFull() {
        return size == buffer.size();
    }
};

#

The circular double-ended queue (deque) is implemented with an array (or vector in C++), two pointers, 'front' and 'rear', and an integer 'size', which represents the current number of elements in the deque. The array has a fixed capacity, 'k'. Front represents the index of the first element in the deque, and rear points to the index after the last element in the deque.

Insertion or deletion is done by manipulating these pointers, front and rear accordingly. When the deque is full, the front and rear pointers wrap around, creating a circular structure.

The main operations are:

  1. insertFront(): inserts an element at the front of the deque. To do this, the front pointer is first decremented and then the value is inserted.
  2. insertLast(): inserts an element at the rear of the deque. The value is directly inserted at the rear and the rear pointer is incremented.
  3. deleteFront(): deletes an element from the front of the deque by incrementing the front pointer.
  4. deleteLast(): deletes an element from the rear of the deque by decrementing the rear pointer.
  5. getFront(): returns the front element of the deque.
  6. getRear(): returns the rear element of the deque.
  7. isEmpty(): returns true if the deque is empty.
  8. isFull(): returns true if the deque is full.