package com.leetcode.leetcode.licm;
/**
* @description: 面试题 01.02. 判定是否互为字符重排
* 给定两个字符串 s1 和 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
* <p>
* 示例 1:
* <p>
* 输入: s1 = "abc", s2 = "bca"
* 输出: true
* 示例 2:
* <p>
* 输入: s1 = "abc", s2 = "bad"
* 输出: false
* 说明:
* <p>
* 0 <= len(s1) <= 100
* 0 <= len(s2) <= 100
* @author: licm
* @create: 2021-07-14 09:28
**/
public class Lc_面试题01_02_判定是否互为字符重排 {
/**
* 哈希表-数组
* <p>
* 由于测试案例都是小写字母,就申请26位长度
*
* @param s1
* @param s2
* @return
*/
public static boolean CheckPermutation(String s1, String s2) {
int[] res = new int[26];
for (int i = 0; i < s1.length(); i++) {
char c = s1.charAt(i);
res[c - 'a'] += 1;
}
for (int i = 0; i < s2.length(); i++) {
char c = s2.charAt(i);
res[c - 'a'] -= 1;
}
for (int i = 0; i < res.length; i++) {
if (res[i] < 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
String s1 = "abc";
String s2 = "bcaa";
System.out.println(CheckPermutation(s1, s2));
}
}