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]
.
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.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
temp.f95:27:39: 27 | function solve(s, goal) result(res) | 1 Error: Function result βresβ at (1) has no IMPLICIT type temp.f95:10:10: 10 | res = solve(s, goal) | 1 Error: Cannot convert UNKNOWN to LOGICAL(4) at (1) temp.f95:16:10: 16 | res = solve(s, goal) | 1 Error: Cannot convert UNKNOWN to LOGICAL(4) at (1) temp.f95:22:10: 22 | res = solve(s, goal) | 1 Error: Cannot convert UNKNOWN to LOGICAL(4) at (1)
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
temp.f95:17:9: 17 | s = "ab " | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:18:12: 18 | goal = "ba " | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:20:35: 20 | if (is_swap_possible(s, goal)) then | 1 Error: Unexpected block IF statement in MODULE at (1) temp.f95:21:19: 21 | print *, "true" | 1 Error: Unexpected WRITE statement in MODULE at (1) temp.f95:22:4: 22 | else | 1 Error: Unexpected ELSE statement in MODULE at (1) temp.f95:23:20: 23 | print *, "false" | 1 Error: Unexpected WRITE statement in MODULE at (1) temp.f95:24:3: 24 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:26:9: 26 | s = "ab " | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:27:12: 27 | goal = "ab " | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:29:35: 29 | if (is_swap_possible(s, goal)) then | 1 Error: Unexpected block IF statement in MODULE at (1) temp.f95:30:19: 30 | print *, "true" | 1 Error: Unexpected WRITE statement in MODULE at (1) temp.f95:31:4: 31 | else | 1 Error: Unexpected ELSE statement in MODULE at (1) temp.f95:32:20: 32 | print *, "false" | 1 Error: Unexpected WRITE statement in MODULE at (1) temp.f95:33:3: 33 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:35:9: 35 | s = "aa " | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:36:12: 36 | goal = "aa " | 1 Error: Unexpected assignment statement in MODULE at (1) temp.f95:38:35: 38 | if (is_swap_possible(s, goal)) then | 1 Error: Unexpected block IF statement in MODULE at (1) temp.f95:39:19: 39 | print *, "true" | 1 Error: Unexpected WRITE statement in MODULE at (1) temp.f95:40:4: 40 | else | 1 Error: Unexpected ELSE statement in MODULE at (1) temp.f95:41:20: 41 | print *, "false" | 1 Error: Unexpected WRITE statement in MODULE at (1) temp.f95:42:3: 42 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:46:26: 7 | function is_swap_possible(s, goal) result(res) | 2 ...... 46 | function is_swap_possible(s, goal) result(res) | 1 Error: Procedure βis_swap_possibleβ at (1) is already defined at (2) temp.f95:47:17: 47 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:48:43: 48 | character(len=*), intent(in) :: s, goal | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:49:19: 49 | integer :: i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:50:18: 50 | logical :: res | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:52:17: 52 | res = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:54:24: 54 | do i = 1, len(s) - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:55:28: 55 | do j = i + 1, len(s) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:56:67: 56 | if (s(i:i) == goal(j:j) .and. s(j:j) == goal(i:i)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:57:28: 57 | res = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:58:20: 58 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:59:15: 59 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:60:11: 60 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:61:21: 61 | if (res) exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:62:7: 62 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:64:3: 64 | end function is_swap_possible | 1 Error: Expecting END MODULE statement at (1)
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:
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: