📌  相关文章
📜  Java程序来检查字符串是否是两个字符串的有效改组(1)

📅  最后修改于: 2023-12-03 14:43:03.797000             🧑  作者: Mango

Introducing Java program to check if a string is a valid permutation of two strings

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.

Problem Statement

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:

  • If 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.
  • However, if 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.

Approach

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.

  1. First, we initialize a frequency array count of size 128 (assuming ASCII characters) and set all counts to 0.
  2. We iterate over each character in str1 and increment the count of that character in count.
  3. We do the same for str2.
  4. Finally, we iterate over each character in targetStr and decrement the count of that character in count.
    • If the count becomes negative or the count for any character is not zero, we return false as it means targetStr is not a valid permutation.
  5. If all characters have a count of zero, we return true, indicating that targetStr is a valid permutation of str1 and str2.
Java Code
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);
    }
}
Markdown Explanation

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.