原题:
力扣https://leetcode-cn.com/problems/check-permutation-lcci/
一、题目要求
给定两个字符串 s1 和 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
示例 1:
输入: s1 = "abc", s2 = "bca"
输出: true
示例 2:
输入: s1 = "abc", s2 = "bad"
输出: false
说明:
0 <= len(s1) <= 100
0 <= len(s2) <= 100
二、解题
package com.leetcode.hash;
import java.util.Arrays;
public class Solution22 {
public static void main(String[] args) {
String s1 = "abc", s2 = "bca";
System.out.println(CheckPermutation(s1, s2));
}
public static boolean CheckPermutation(String s1, String s2) {
char[] arr1 = s1.toCharArray();
char[] arr2 = s2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
String a1 = new String(arr1);
String a2 = new String(arr2);
return a1.equals(a2);
}
}
三、运行结果
四、提交结果