Leetcode 2839: Check if Strings Can be Made Equal With Operations I

grid47
grid47
Exploring patterns and algorithms
Jan 28, 2024 4 min read

You are given two strings s1 and s2, each consisting of 4 lowercase English letters. You can perform a specific operation multiple times, where you swap two characters at indices i and j such that j - i = 2. The task is to determine if it’s possible to make s1 equal to s2 after performing any number of such operations.
Problem
Approach
Steps
Complexity
Input: The input consists of two strings, s1 and s2, each containing 4 lowercase English letters.
Example: s1 = 'abcd', s2 = 'cdab'
Constraints:
• Both s1 and s2 have a length of 4.
• Both strings consist only of lowercase English letters.
Output: Return true if it's possible to make the two strings equal using the described operations, otherwise return false.
Example: true
Constraints:
• The output should be a boolean value indicating whether the strings can be made equal or not.
Goal: The goal is to check if it's possible to transform s1 into s2 using the allowed operations.
Steps:
• Check if s1 is already equal to s2.
• If not, attempt to swap characters at indices (0, 2) and (1, 3).
• If the resulting string matches s2 after any number of swaps, return true.
• If no such transformation is possible, return false.
Goal: The constraints on the input are as follows:
Steps:
• s1 and s2 are of length 4.
• Both strings consist only of lowercase English letters.
Assumptions:
• Only the swap operation between indices (i, j) where j - i = 2 is allowed.
• Both input strings are of the same length (4).
Input: s1 = 'abcd', s2 = 'cdab'
Explanation: We can swap characters at indices (0, 2) and (1, 3) to transform s1 into s2.

Input: s1 = 'abcd', s2 = 'dacb'
Explanation: No number of swaps can make s1 equal to s2.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus