
class Solution {
public boolean isAnagram(String s, String t) {
int[] record = new int[26];
for (int i = 0; i < s.length(); i++) {
record[s.charAt(i) - 'a']++;
}
for (int i = 0; i < t.length(); i++) {
record[t.charAt(i) - 'a']--;
}
for (int count: record) {
if (count != 0) {
return false;
}
}
return true;
}
}

import java.util.HashSet;
import java.util.Set;
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
return new int[0];
}
Set<Integer> set1 = new HashSet<>();
Set<Integer> resSet = new HashSet<>();
for (int i : nums1) {
set1.add(i);
}
for (int i : nums2) {
if (set1.contains(i)) {
resSet.add(i);
}
}
return resSet.stream().mapToInt(x -> x).toArray();
}
}

class Solution {
public boolean isHappy(int n) {
Set<Integer> record = new HashSet<>()
while (n != 1 && !record.contains(n)) {
record.add(n)
n = getNextNumber(n)
}
return n == 1
}
private int getNextNumber(int n) {
int res = 0
while (n > 0) {
int temp = n % 10
res += temp * temp
n = n / 10
}
return res
}
}

class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] record = new int[26];
for(char c : magazine.toCharArray()){
record[c - 'a'] += 1;
}
for(char c : ransomNote.toCharArray()){
record[c - 'a'] -= 1;
}
for(int i : record){
if(i < 0){
return false;
}
}
return true;
}
}

class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer, Integer> map = new HashMap<>();
int temp;
int res = 0;
for (int i : nums1) {
for (int j : nums2) {
temp = i + j;
if (map.containsKey(temp)) {
map.put(temp, map.get(temp) + 1);
} else {
map.put(temp, 1);
}
}
}
for (int i : nums3) {
for (int j : nums4) {
temp = i + j;
if (map.containsKey(0 - temp)) {
res += map.get(0 - temp);
}
}
}
return res;
}
}

class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>()
Arrays.sort(nums)
for (int i = 0
// nums[i] > target 直接返回, 剪枝操作
if (nums[i] > 0 && nums[i] > target) {
return result
}
if (i > 0 && nums[i - 1] == nums[i]) {
continue
}
for (int j = i + 1
if (j > i + 1 && nums[j - 1] == nums[j]) {
continue
}
int left = j + 1
int right = nums.length - 1
while (right > left) {
long sum = (long) nums[i] + nums[j] + nums[left] + nums[right]
if (sum > target) {
right--
} else if (sum < target) {
left++
} else {
result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]))
while (right > left && nums[right] == nums[right - 1]) right--
while (right > left && nums[left] == nums[left + 1]) left++
left++
right--
}
}
}
}
return result
}
}

class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Character, Character> map1 = new HashMap<>();
Map<Character, Character> map2 = new HashMap<>();
for (int i = 0, j = 0; i < s.length(); i++, j++) {
if (!map1.containsKey(s.charAt(i))) {
map1.put(s.charAt(i), t.charAt(j));
}
if (!map2.containsKey(t.charAt(j))) {
map2.put(t.charAt(j), s.charAt(i));
}
if (map1.get(s.charAt(i)) != t.charAt(j) || map2.get(t.charAt(j)) != s.charAt(i)) {
return false;
}
}
return true;
}
}

class Solution {
public List<String> commonChars(String[] A) {
List<String> result = new ArrayList<>()
if (A.length == 0) return result
int[] hash= new int[26]
for (int i = 0
hash[A[0].charAt(i)- 'a']++
}
// 统计除第一个字符串外字符的出现频率
for (int i = 1
int[] hashOtherStr= new int[26]
for (int j = 0
hashOtherStr[A[i].charAt(j)- 'a']++
}
// 更新hash,保证hash里统计26个字符在所有字符串里出现的最小次数
for (int k = 0
hash[k] = Math.min(hash[k], hashOtherStr[k])
}
}
// 将hash统计的字符次数,转成输出形式
for (int i = 0
while (hash[i] != 0) { // 注意这里是while,多个重复的字符
char c= (char) (i+'a')
result.add(String.valueOf(c))
hash[i]--
}
}
return result
}
}

class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> res=new ArrayList<>();
HashMap<String,ArrayList<String>> map=new HashMap<>();
for(String str:strs){
char[]ch=str.toCharArray();
Arrays.sort(ch);
String key=String.valueOf(ch);
if(!map.containsKey(key)) map.put(key,new ArrayList<>());
map.get(key).add(str);
}
return new ArrayList(map.values());
}
}

class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> res = new ArrayList<Integer>()
int n1 = s.length()
int n2 = p.length()
if(n2>n1){
return res
}
int[] sInt = new int[26]
int[] pInt = new int[26]
for(int i=0
sInt[s.charAt(i)-'a']++
pInt[p.charAt(i)-'a']++
}
if(Arrays.equals(sInt,pInt)){
res.add(0)
}
for(int i=n2
sInt[s.charAt(i-n2)-'a']--
sInt[s.charAt(i)-'a']++
if(Arrays.equals(sInt,pInt)){
res.add(i-n2+1)
}
}
return res
}
}