题目描述
实现一个算法来识别一个字符串 ���2str2 是否是另一个字符串 ���1str1 的排列。排列的解释如下:如果将 ���1str1 的字符拆分开,重新排列后再拼接起来,能够得到 ���2str2 ,那么就说字符串 ���2str2 是字符串 ���1str1 的排列。(不忽略大小写)
如果 ���2str2 字符串是 ���1str1 字符串的排列,则输出 YES;如果 ���2str2 字符串不是 ���1str1 字符串的排列,则输出 NO;
输入描述
第一行为字符串���1str1;
第二行为字符串���2str2;
字符串长度均不超过 100。
输出描述;
输出一行,如果 ���2str2 字符串是 ���1str1 字符串的排列,则输出 YES;
如果 ���2str2 字符串不是 ���1str1 字符串的排列,则输出 NO;
输入输出样例
示例
输入
acb
bac
输出
YES
解题过程:
解题思路:一个巧妙的想法:假设 s1 = aabb, s2 = baab 我们只需要一对对的去消除掉,如果能全部消除掉就一定是另一个的排列,此时我们可以想到排序,然后再调用equalsIgnoreCase这个方法(因为题目说了可能会有大小写混合)如果相等的话就是另一个的排列
![](https://img-blog.csdnimg.cn/img_convert/0f4bddbadd1546179548775ee0df7b63.png)
代码如下:
package 第一章;
import java.util.Arrays;
import java.util.Scanner;
public class 确定字符串是否是另一个的排列 {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
char s1[] = sc.next().toCharArray();
char s2[] = sc.next().toCharArray();
Arrays.sort(s2);
Arrays.sort(s1);
String s11 = new String(s1);
String s22 = new String(s2);
System.out.println(s11.equalsIgnoreCase(s22) ? "YES" : "NO");
}
}
![](https://img-blog.csdnimg.cn/img_convert/13bfa451b8c14fa193035f5d343a5a60.png)