Javatpoint Logo
Javatpoint Logo

String Sort Custom in Java

Two strings a1 and a2 are given to us. All characters of the string a1 are unique and are sorted in a particular order. Our job is to permute the characters of the string a2, such that the order in which the characters are coming in the string a1 is coming in a2 too. There can be many valid permutations of the string a2. Return any one of the valid permutations of the string a2.

Example 1:

Input

String a1 = "ktrmps" String a2 = "stkmoorp"

Output: "ktrmpsoo"

Explanation: In string a1, we see that character 'k' is occurring first. After that, the character 't', then 'r', then 'm', then 'p', and then 's'. However, the same order is not present in the string a2. Therefore, we permute the string a2, and we get "ktrmpsoo". In this string, the order is the same as in string a1. Hence, "ktrmpsoo" is our answer.

Example 2:

Input

String a1 = "erhdqo" String a2 = "wwdybr"

Output: "rdwwyb"

Explanation: In string a1, we see that character 'e' is occurring first. After that, the character 'r', then 'h', then 'd', then 'q', and then 'o'. However, the same order is not present in the string a2. Therefore, we permute the string a2, and we get "rdwwyb". In this string, the order is the same as in string a1. Hence, "rdwwyb" is our answer. Note that "rdybww" or "rdbwwy" are also some of the valid permutations.

Example 3:

Input

String a1 = "abcd" String a2 = "efgh"

Output: "efgh"

Explanation: No character of string a2 matches with the characters present in string a1. Therefore, no permutation is required. Thus, we can treat the string a2 as our answer.

Naïve Approach

The naïve approach is to use the two nested loops. The outer loop iterates over the string a1, and the inner loop iterates over the string a2. The inner loop checks whether the character pointed by the outer loop variable is present in the string a2 or not. If present, then it appends that character in a string called answer. Note that all the occurrences of that string are appended and removed from the string. In the end, we return answer.

FileName: CustomSortString.java

Output:

For the strings "trmps" and "stkmoorp", the permuted string is: "trmpskoo"

For the strings "erhdqo" and "wwdybr", the permuted string is: "rdwwyb"

For the strings "abcd" and "efgh", the permuted string is: "efgh"

Complexity Analysis: In the worst-case scenario, no character will be removed from the string a2. Therefore, the size of the string a2 remains constant in each iteration of the outer loop. It is because of this reason the time complexity of the program is O(M x N), where M is the total number of characters present in the string a1, and N is the total number of characters present in the string a2. The program is not using any extra space, thus making the space complexity of the program O(1).

Now, we will do some optimization to reduce the time complexity of the program.

Approach: Using Character Count

In order to have the linear time complexity, we will be counting the number of times a character occurs in the string a2 and will store it in an array (tempArr[] in our case). After that, traverse the string a1, and construct the resultant string using the tempArr[] as per the order in which characters are arranged in the string a1. Traverse the tempArr[] to append the remaining characters of the string a2 in the resultant string. See the following program.

FileName: CustomSortString1.java

Output:

For the strings "trmps" and "stkmoorp", the permuted string is: "trmpskoo"

For the strings "erhdqo" and "wwdybr", the permuted string is: "rdbwwy"

For the strings "abcd" and "efgh", the permuted string is: "efgh"

Complexity Analysis: The program uses nested for-loops. However, it will not make the time complexity of the program O(M x N). It is because the inner loop will not work if a character in string a1 is not present in the string a2. The will loop is, at most going to traverse O(N) times. Thus, the time complexity of the program is O(M + N), where M is the size of the string a1 and N is the size of the string a2. T

The program uses the array bucketArr[]. However, the size of the bucketArr[] is fixed. Thus, making the space complexity of the program constant, i.e., O(1).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA