English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (queue)

Java Map collection

Java Set collection

Java entrée/sortie (I)/O)

Java Reader/Writer

Autres sujets Java

Programme Java pour vérifier si une chaîne est une réorganisation valide des deux autres chaînes

Java examples

Dans cet exemple, nous vérifierons si une chaîne est une réorganisation valide des deux autres chaînes dans Java (mélange).

Pour comprendre cet exemple, vous devriez comprendre ce qui suitProgrammation JavaThème :

Exemple : vérifier si une chaîne est une réorganisation valide des deux autres chaînes

class	Main	{
  //Vérifier si la chaîne de résultat est un mélange valide des deux premières chaînes
  static	boolean	shuffleCheck(String	first,	String	second,	String	result)	{
    //Vérifier si la longueur du résultat correspond à
    //La somme totale des résultats de la première et de la deuxième
    if(first.length() + second.length()	!=	result.length())	{
      return	false;
    }
    //Suivre3Variables pour chaque caractère des chaînes
    int	i	=	0,	j	=	0,	k	=	0;
    //Parcourir tous les caractères du résultat
    while	(k	!=	result.length())	{
      //Vérifier si le premier caractère du résultat correspond au premier caractère de la première chaîne
      if	(i	<	first.length()	&&	first.charAt(i)	==	result.charAt(k))	{
        i++;
      //Vérifier si le premier caractère du résultat correspond au premier caractère de la deuxième chaîne
      }	else	if	(j	<	second.length()	&&	second.charAt(j)	==	result.charAt(k))	{
        j++;
      //Si les caractères ne correspondent pas
      }	else	{
        return	false;
      }
      //Accéder au prochain caractère du résultat
      k++;
    }
    //Après avoir accédé à tous les caractères du résultat
     //Si la première ou la deuxième a des caractères laissés
    if(i	<	first.length()	||	j	<	second.length())	{
      return	false;
    }
    return	true;
  }
  public	static	void	main(String[]	args)	{
    String	first	=	"XY";
    String	second	=	"12";
    String[]	results	=	{"1XY2",	"Y12X"};
    //Appel de méthode pour vérifier si la chaîne de résultat est
    //a mixture of first and second
    for (String result : results) {
      if (shuffleCheck(first, second, result) == true) {
        System.out.println(result + " is" + first + " and" + second + "Valid reorganization");
      }
      else {
        System.out.println(result + " is not" + first + " and" + second+ "Valid reorganization");
      }
    }
  }
}

Output result

1XY2 is XY and 12 Valid reorganization
Y12X is not XY and 12 Valid reorganization

In the above example, we have a string array named results. It contains two strings:1XY2and Y12X. We are checking if the two strings are valid strings first(XY) and second(12)。

Here, the program says1XY2is a valid shuffle XY and12. But, Y12X This is not a valid shuffle.

This is because Y12X changed the order of the string XY. Here, Y is used for the previous X. Therefore, as an effective reorganization, the order of the string should be maintained.

Java examples