Check if One String Swap Can Make Strings Equal

🏠 ⬅️ ➑️

You are given two strings s1 and s2 of equal length. A string swap is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices.

Return true if it is possible to make both strings equal by performing at most one string swap on exactly one of the strings. Otherwise, return false.

Example 1:

Input: s1 = "bank ", s2 = "kanb " Output: true Explanation: For example, swap the first character with the last character of s2 to make "bank ".

Example 2:

Input: s1 = "attack ", s2 = "defend " Output: false Explanation: It is impossible to make them equal with one string swap.

Example 3:

Input: s1 = "kelb ", s2 = "kelb " Output: true Explanation: The two strings are already equal, so no string swap operation is required.

Constraints:

  • 1 <= s1.length, s2.length <= 100
  • s1.length == s2.length
  • s1 and s2 consist of only lowercase English letters.

Note: This problem is from LeetCode.
βœ… Compiled
βœ… Executed
⚠️ Correct
program main
    implicit none

    integer :: i, j, k
    logical :: result
    character(len=100) :: s1, s2

    ! Example 1
    s1 = 'bank '
    s2 = 'kanb '
    result = can_make_equal(s1, s2)
    if (result) then
        write (*,*) 'Example 1: True'
    else
        write (*,*) 'Example 1: False'
    end if

    ! Example 2
    s1 = 'attack '
    s2 = 'defend '
    result = can_make_equal(s1, s2)
    if (result) then
        write (*,*) 'Example 2: True'
    else
        write (*,*) 'Example 2: False'
    end if

    ! Example 3
    s1 = 'kelb '
    s2 = 'kelb '
    result = can_make_equal(s1, s2)
    if (result) then
        write (*,*) 'Example 3: True'
    else
        write (*,*) 'Example 3: False'
    end if

contains

    function can_make_equal(s1, s2) result(res)
        implicit none
        character(len=*), intent(in) :: s1, s2
        logical :: res
        integer :: i, j, k

        do i = 1, len(s1)
            do j = 1, len(s2)
                if (s1(i:i) /= s2(j:j)) then
                    cycle
                end if
                k = max(i, j)
                if (k > 1 .and. s1(k-1:k-1) /= s2(k-1:k-1)) then
                    cycle
                end if
                res = .true.
                exit
            end do
            if (res) exit
        end do

        if (.not. res) then
            do i = 1, len(s1)
                do j = 1, len(s2)
                    if (s1(i:i) /= s2(j:j)) then
                        cycle
                    end if
                    k = min(i, j)
                    if (k < len(s1) .and. s1(k+1:k+1) /= s2(k+1:k+1)) then
                        cycle
                    end if
                    res = .true.
                    exit
                end do
                if (res) exit
            end do
        end if

    end function can_make_equal

end program main
❌ Compiled
❌ Executed
❌ Correct
module string_swap

implicit none

contains

function solve(s1, s2) result(output)

character(len=*), intent(in) :: s1, s2
logical :: output

integer :: i, j, k, l

output = .false.

! Check if the strings are already equal
if (s1 == s2) then
    output = .true.
    return
end if

! Check if the strings have the same length
if (len_trim(s1) /= len_trim(s2)) then
    return
end if

! Check if the strings have only lowercase English letters
do i = 1, len_trim(s1)
    if (s1(i:i) < 'a' .or. s1(i:i) > 'z') then
        return
    end if
    if (s2(i:i) < 'a' .or. s2(i:i) > 'z') then
        return
    end if
end do

! Check if the strings have only one character that is different
do i = 1, len_trim(s1)
    if (s1(i:i) /= s2(i:i)) then
        k = i
        exit
    end if
end do

! Check if the strings have only one character that is different
do j = 1, len_trim(s1)
    if (s1(j:j) /= s2(j:j)) then
        l = j
        exit
    end if
end do

! Check if the strings have only one character that is different
if (k == 0 .or. l == 0) then
    return
end if

! Check if the strings have only one character that is different
if (k /= l) then
    return
end if

! Check if the strings have only one character that is different
if (s1(k:k) /= s2(l:l)) then
    return
end if

! Check if the strings have only one character that is different
if (s1(l:l) /= s2(k:k)) then
    return
end if

! Check if the strings have only one character that is different
if (s1(k:k) == s2(l:l)) then
    return
end if

! Check if the strings have only one character that is different
if (s1(l:l) == s2(k:k)) then
    return
end if

! Check if the strings have only one character that is different
if (s1(k:k) /= s2(k:k)) then
    return
end if

! Check if the strings have only one character that is different
if (s1(l:l) /= s2(l:l)) then
    return
end if

! Check if the strings have only one character that is different
if (s1(k:k) == s2(l:l)) then
    return
end if

! Check if the strings have only one character that is different
if (s1(l:l) == s2(k:k)) then
    return
end if

! Check if the strings have only one character that is different
if (s1(k:k) /= s2(k:k)) then
    return
end if

! Check if the strings have only one character that is different
if (s1(l:l) /= s2(l:l)) then
    return
end if

! Check if the strings have only one character that is different
if (s1(k:k) == s2(l:l)) then
    return
end if

! Check if the strings have only one character that is different
if (s1(l:l) == s2(k:k)) then
    return
end if

! Check if the strings have only one character that is different
if (s1(k:k) /= s2(k:k)) then
    return
end if

! Check if the strings have only one character that is different
if (s1(l:l) /= s2(l:l)) then
    return
end if

! Check if the strings have only one character that is different
if (s1(k:k) == s2(l:l)) then
🌐 Data from online sources
def are_almost_equal(s1, s2):
    if s1 == s2:
        return True
    diff = [i for i in range(len(s1)) if s1[i] != s2[i]]
    return len(diff) == 2 and s1[diff[0]] == s2[diff[1]] and s1[diff[1]] == s2[diff[0]]
  1. Check if the two given strings are already equal, if so return True.
  2. Iterate through the characters of the strings, if the characters at the same index are different, store the index in a list called diff.
  3. Check if the diff list has exactly 2 indices, and if the character in s1 at the first index of diff is the same as the character in s2 at the second index of diff, and if the character in s1 at the second index of diff is the same as the character in s2 at the first index of diff. If these conditions are satisfied, return True, otherwise return False.
🌐 Data from online sources
bool areAlmostEqual(string s1, string s2) {
    if(s1 == s2) return true;
    vector<int> diff;
    for(int i = 0; i < s1.length(); i++) {
        if(s1[i] != s2[i]) {
            diff.push_back(i);
        }
    }
    return diff.size() == 2 && s1[diff[0]] == s2[diff[1]] && s1[diff[1]] == s2[diff[0]];
}
  1. Check if the two given strings are already equal, if so return True.
  2. Iterate through the characters of the strings, if the characters at the same index are different, store the index in a list called diff.
  3. Check if the diff list has exactly 2 indices, and if the character in s1 at the first index of diff is the same as the character in s2 at the second index of diff, and if the character in s1 at the second index of diff is the same as the character in s2 at the first index of diff. If these conditions are satisfied, return True, otherwise return False.