class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;
Map<Character, Integer> map = new HashMap<>();
for(int i = 0; i < s.length(); i++){
map.put(s.charAt(i) , map.getOrDefault(s.charAt(i), 0) + 1);
}
for(int i = 0; i < t.length(); i++){
char ch = t.charAt(i);
if(!map.containsKey(ch)) return false;
int count = map.get(ch);
if(count <= 0) return false;
map.put(ch, count - 1);
}
return true;
}
}
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
for(int num : nums1) set1.add(num);
for(int num : nums2){
if(set1.contains(num)) set2.add(num);
}
int[] ans = new int[set2.size()];
int idx = 0;
for(int num : set2) ans[idx++] = num;
return ans;
}
}
class Solution {
public boolean isHappy(int n) {
Set<Integer> set = new HashSet<>();
while(n != 1){
if(set.contains(n)) return false;
set.add(n);
// System.out.println(n);
n = get(n);
}
return true;
}
private int get(int n){
int ans = 0;
while(n > 0){
int temp = n % 10;
ans += temp * temp;
n /= 10;
}
return ans;
}
}
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
int n = nums.length;
for(int i = 0; i < n; i++){
if(map.containsKey(target - nums[i])) return new int[]{i, map.get(target - nums[i])};
map.put(nums[i], i);
}
return new int[]{-1, -1};
}
}
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
// a + b + c + d = 0
// key nums[i]和nums[j]的和 value nums[i]和nums[j] 的和的次数
Map<Integer, Integer> map = new HashMap<>();
int n = nums1.length, ans = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
int sum = nums1[i] + nums2[j];
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
int target = -nums3[i] - nums4[j];
if(map.containsKey(target)){
ans += map.get(target);
}
}
}
return ans;
}
}
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
Map<Character, Integer> map = new HashMap<>();
int n = ransomNote.length(), m = magazine.length();
for(int i = 0; i < m; i++) {
char ch = magazine.charAt(i);
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
for(int i = 0; i < n; i++){
char ch = ransomNote.charAt(i);
if(!map.containsKey(ch)) return false;
if(map.get(ch) <= 0) return false;
map.put(ch, map.get(ch) - 1);
}
return true;
}
}
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
int n = nums.length;
Arrays.sort(nums);
for(int i = 0; i < nums.length; i++){
if(nums[i] > 0) break;
if(i > 0 && nums[i] == nums[i - 1]) continue;
int l = i + 1, r = n - 1;
while(l < r){
int sum = nums[i] + nums[l] + nums[r];
if(sum > 0) r--;
else if(sum < 0) l++;
else{
ans.add(Arrays.asList(nums[i], nums[l], nums[r]));
while(l < r && nums[l] == nums[l+1]) l++;
while(l < r && nums[r] == nums[r-1]) r--;
l++;
r--;
}
}
}
return ans;
}
}
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);
int n = nums.length;
for(int i = 0; i < nums.length; i++){
if(i > 0 && nums[i] == nums[i - 1]) continue;
for(int j = i+ 1; j < nums.length; j++){
if(j > i + 1 && nums[j] == nums[j - 1]) continue;
int l = j + 1, r = n - 1;
while(l < r){
int sum = nums[j] + nums[i] + nums[l] + nums[r];
if(sum > target) r--;
else if(sum < target) l++;
else{
ans.add(Arrays.asList(nums[i], nums[j], nums[l], nums[r]));
while(l < r && nums[l] == nums[l + 1]) l++;
while(l < r && nums[r] == nums[r - 1]) r--;
l++;
r--;
}
}
}
}
return ans;
}
}