📅  最后修改于: 2023-12-03 14:43:03.797000             🧑  作者: Mango
In this markdown, I will provide an introduction to a Java program that checks if a given string is a valid permutation of two other strings.
Given three strings - str1
, str2
, and targetStr
, we need to determine if targetStr
is a valid permutation of str1
and str2
.
A valid permutation means that the characters in targetStr
can be rearranged to form str1
and str2
. The length of targetStr
will always be the sum of lengths of str1
and str2
.
For example:
str1="abc"
and str2="def"
, a valid permutation of str1
and str2
can be targetStr="abcdef"
, as all characters from str1
(a
, b
, c
) and str2
(d
, e
, f
) are present in targetStr
.str1="abc"
and str2="def"
, a string like targetStr="abdecf"
would not be a valid permutation, as the characters d
and e
are not in their original positions.Our task is to write a Java program to solve this problem efficiently.
To solve this problem, we can use a frequency count approach. We iterate over each character in targetStr
and maintain a frequency count for each character encountered.
count
of size 128 (assuming ASCII characters) and set all counts to 0.str1
and increment the count of that character in count
.str2
.targetStr
and decrement the count of that character in count
.false
as it means targetStr
is not a valid permutation.true
, indicating that targetStr
is a valid permutation of str1
and str2
.public class PermutationChecker {
public static boolean isPermutation(String str1, String str2, String targetStr) {
if (str1.length() + str2.length() != targetStr.length()) {
return false; // Length of targetStr is not equal to the sum of lengths of str1 and str2
}
int[] count = new int[128];
// Increment count for characters in str1
for (int i = 0; i < str1.length(); i++) {
count[str1.charAt(i)]++;
}
// Increment count for characters in str2
for (int i = 0; i < str2.length(); i++) {
count[str2.charAt(i)]++;
}
// Decrement count for characters in targetStr
for (int i = 0; i < targetStr.length(); i++) {
count[targetStr.charAt(i)]--;
if (count[targetStr.charAt(i)] < 0) {
return false; // targetStr has extra characters or characters not present in str1 and str2
}
}
// Check if all characters have count 0
for (int i = 0; i < 128; i++) {
if (count[i] != 0) {
return false; // targetStr missing characters from str1 or str2
}
}
return true; // targetStr is a valid permutation of str1 and str2
}
public static void main(String[] args) {
String str1 = "abc";
String str2 = "def";
String targetStr = "abcdef";
boolean isValidPermutation = isPermutation(str1, str2, targetStr);
System.out.println("Is " + targetStr + " a valid permutation of " + str1 + " and " + str2 + "?");
System.out.println(isValidPermutation);
}
}
The above code snippets define a Java class PermutationChecker
with a static method isPermutation
that checks if targetStr
is a valid permutation of str1
and str2
. The main
method provides an example usage by checking if "abcdef"
is a valid permutation of "abc"
and "def"
. The result is printed to the console.
To format the code snippets as markdown, we use the three backticks (```) to denote the beginning and end of a code block. The code block is marked with the language name java
.
Feel free to modify and use this code snippet according to your requirements.