Check if One String Swap Can Make Strings Equal in Java27 Mar 2025 | 2 min read Two strings, str1 and str2, of the same length are given to us. Selecting two indices in a string, which need not be distinct, and switching the characters at these indices is known as a string swap. If, at most, one string swap can be done on precisely one of the strings to make both strings equal, then return true. Return false in the other case. Example 1: Input: String str1 = "bank" String str2 = "kanb" Output: true Explanation: The easiest way to create a "bank" is to replace the first character with the last character of str2. Example 2: Input: String str1 = "attack" String str2 = "defend" Output: false Explanation: A single-string switch will never be enough to equalize them. Example 3: Input: String str1 = "kelb" String str2 = "kelb" Output: true Explanation: There is no need to perform a string swap because the two strings are already equal. Naïve ApproachThe approach determines if two strings can be equalized by exchanging a maximum of one pair of characters. It detects mismatched characters by looping across both strings. The characters are stored if there is precisely one mismatch; the function returns false if there are more than two mismatches or if the mismatched characters cannot be switched to make the strings equal. To ensure that the swap is valid, the procedure only returns true if there are precisely two or zero mismatches. The method provides an effective resolution with a linear time complexity of O(n), where n represents the string length. Implementation:FileName: SwapingStrings.java Output: true Complexity Analysis: The time complexity of the above code is O(n), where n is the length of the strings str1 and str2. The space complexity of the above code is O(1). Next TopicThread Concept in Java |
Java ObjectInputStream Class
The java.io.ObjectInputStream class is used to deserialize objects and primitive data iously serialized using the ObjectOutputStream. It allows the reconstruction of object graphs and ensures compatibility between the serialized object's class and the current JVM (Java Virtual Machine) class definitions. ObjectOutputStream and ObjectInputStream collaborate to save and...
22 min read
Partition Equal Subset Sum Problem in Java
The Partition Equal Subset Sum Problem is a significant problem in the algorithms and occurs frequently in algorithm interviews. The simplest problem in this category of problems is to decide if a set of positive integers can be divided into two groups with equal sums. The problem...
5 min read
DecimalStyle getPositiveSign() method in Java with Examples
The java.time.format.DecimalStyle class have the getPositiveSign() method. The Java DecimalStyle class is used to retrieve the character used to indicate a positive sign for this DecimalStyle's Locale. The character for the positive sign of that locale is returned by this method. Syntax: public char getPositiveSign() Parameter:...
3 min read
Java Queue
A queue is another kind of linear data structure that is used to store elements just like any other data structure but in a particular manner. In simple words, we can say that the queue is a type of data structure in the Java programming language...
10 min read
Binary to Gray Code Using Recursion in Java
Named for Frank Grey, Grey Code is a binary numeral system in which there is just a single bit difference between two successive values. It is also known as the "Reflected Binary Code" because the (n-1)-bit form of it may be reflected and appended to itself...
4 min read
Longest Harmonious Subsequence in Java
An array of n integers is given to us. The task is to find the size of the longest harmonious subsequence of the array. A subsequence is said to be harmonious if the difference between the maximum value element and the minimum value element in the...
10 min read
Business Consumer Problem in Java
In the rapidly evolving landscape of business, Java has emerged as one of the most widely used programming languages. Its versatility, platform independence, and extensive libraries make it a top choice for developing robust and scalable enterprise applications. However, like any technology, Java is not without...
4 min read
What is Core Java?
The word Core describes the basic concept of something, and here, the phrase 'Core Java' defines the basic Java that covers the basic concept of Java programming language. We all are aware that Java is one of the well-known and widely used programming languages, and to...
5 min read
Java 12
release after the LTS version 11 of Java. JDK 12 is a part of the 6-month release cycle. On 19 March 2019, was released, which is a Non-LTS version and would not have long term support. An open-source reference implementation of SE Platform is...
5 min read
Java 10 Collectors Methods
Java collectors play a crucial role in the Stream API, providing a convenient way to transform elements of a stream into various data structures such as lists, Sets, or Maps. In this section, we will explore some of the commonly used collector methods in Java 10. toList()...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India