LeetCode 2839 | Check if Strings Can be Made Equal With Operations I | Java | Easy | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 2839 β Check if Strings Can be Made Equal With Operations I using Parity Invariance and String Manipulation in Java.
We are given two lowercase English strings s1 and s2, both of length 4. We can apply an operation any number of times where we choose two indices i and j with a distance of 2 (j - i = 2) and swap their characters. We need to determine if we can make s1 equal to s2.
Because allowable swaps are strictly at distance 2, characters at even indices (0 and 2) can never swap with characters at odd indices (1 and 3). Parity is conserved throughout all operations. We verify that the multiset of characters at even positions in s1 matches that of s2, and the multiset of characters at odd positions in s1 matches that of s2, delivering an optimal O(1) time and O(1) auxiliary space solution.
π₯ Problem Highlights
β Conservation of index parity during distance-2 character swaps
β Separation of string into independent even and odd index subsets
β Complete coverage of all 4 swap permutations in O(1) checks
β Clean logical comparisons eliminating array allocations
β Handling identical characters and edge cases gracefully
β Optimal O(1) time complexity and O(1) auxiliary space
β±οΈ TIMESTAMPS
00:00 Intro & Daily Challenge Overview
01:27 Problem Understanding & Fixed Length 4 Constraints
03:23 Key Observations: Parity Invariance & Decoupled Swapping
05:29 Algorithmic Strategy: Constant-Time Multiset Validation
07:06 Clean Java Solution Walkthrough Line by Line
08:31 Dry Run & Step-by-Step Example Walkthrough
10:29 Time Complexity Analysis (O(1) - 0 ms, Beats 100%)
11:24 Space Complexity Analysis (O(1) Auxiliary Space)
12:03 Outro & Resources
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/March/29-March/2839-Check-if-Strings-Can-be-Made-Equal-With-Operations-I
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/March/29-March/2839-Check-if-Strings-Can-be-Made-Equal-With-Operations-I/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/March/29-March/2839-Check-if-Strings-Can-be-Made-Equal-With-Operations-I/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/March/29-March/2839-Check-if-Strings-Can-be-Made-Equal-With-Operations-I/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/March/29-March/2839-Check-if-Strings-Can-be-Made-Equal-With-Operations-I/solution.js
π LeetCode Source Code
π Admin type the link
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Easy Problems
Java DSA Preparation
String Manipulation Patterns
Parity Invariance Techniques
Array Swapping Invariants
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why can a character at index 0 never end up at index 1 or 3 under distance-2 swaps?
1οΈβ£ Because any swap of indices differing by 2 preserves the parity of the index
2οΈβ£ Because characters can only be swapped from left to right
3οΈβ£ Because the operation only permits odd-numbered swaps
4οΈβ£ Because strings must only contain distinct characters
#LeetCode2839 #LeetCode #CheckIfStringsCanBeMadeEqualWithOperationsI #Java #JavaDSA #String #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 2839 β Check if Strings Can be Made Equal With Operations I using Parity Invariance and String Manipulation in Java.
We are given two lowercase English strings s1 and s2, both of length 4. We can apply an operation any number of times where we choose two indices i and j with a distance of 2 (j - i = 2) and swap their characters. We need to determine if we can make s1 equal to s2.
Because allowable swaps are strictly at distance 2, characters at even indices (0 and 2) can never swap with characters at odd indices (1 and 3). Parity is conserved throughout all operations. We verify that the multiset of characters at even positions in s1 matches that of s2, and the multiset of characters at odd positions in s1 matches that of s2, delivering an optimal O(1) time and O(1) auxiliary space solution.
π₯ Problem Highlights
β Conservation of index parity during distance-2 character swaps
β Separation of string into independent even and odd index subsets
β Complete coverage of all 4 swap permutations in O(1) checks
β Clean logical comparisons eliminating array allocations
β Handling identical characters and edge cases gracefully
β Optimal O(1) time complexity and O(1) auxiliary space
β±οΈ TIMESTAMPS
00:00 Intro & Daily Challenge Overview
01:27 Problem Understanding & Fixed Length 4 Constraints
03:23 Key Observations: Parity Invariance & Decoupled Swapping
05:29 Algorithmic Strategy: Constant-Time Multiset Validation
07:06 Clean Java Solution Walkthrough Line by Line
08:31 Dry Run & Step-by-Step Example Walkthrough
10:29 Time Complexity Analysis (O(1) - 0 ms, Beats 100%)
11:24 Space Complexity Analysis (O(1) Auxiliary Space)
12:03 Outro & Resources
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/March/29-March/2839-Check-if-Strings-Can-be-Made-Equal-With-Operations-I
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/March/29-March/2839-Check-if-Strings-Can-be-Made-Equal-With-Operations-I/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/March/29-March/2839-Check-if-Strings-Can-be-Made-Equal-With-Operations-I/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/March/29-March/2839-Check-if-Strings-Can-be-Made-Equal-With-Operations-I/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/March/29-March/2839-Check-if-Strings-Can-be-Made-Equal-With-Operations-I/solution.js
π LeetCode Source Code
π Admin type the link
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Easy Problems
Java DSA Preparation
String Manipulation Patterns
Parity Invariance Techniques
Array Swapping Invariants
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why can a character at index 0 never end up at index 1 or 3 under distance-2 swaps?
1οΈβ£ Because any swap of indices differing by 2 preserves the parity of the index
2οΈβ£ Because characters can only be swapped from left to right
3οΈβ£ Because the operation only permits odd-numbered swaps
4οΈβ£ Because strings must only contain distinct characters
#LeetCode2839 #LeetCode #CheckIfStringsCanBeMadeEqualWithOperationsI #Java #JavaDSA #String #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
