# 884. Unusual words in two sentences / 1544 Collating Strings / 1512 Number of good pairs

Posted by mistertylersmith on Thu, 03 Feb 2022 15:16:29 +0100

884. Unusual words in two sentences [simple question] [daily question]

Idea: [hash table simulation]

1. Define two hash tables to store each word and occurrence times of s1 and s2 respectively.
2. Define a list to store the uncommon words found.
3. Traverse all the keys of map1. If value=1 and does not appear in map2, then this key is an uncommon word, and add it to the list; Similarly, traverse all keys in map2. If value=1 and does not appear in map1, the key is an uncommon word and is added to the list.
4. Convert the list into an array of String type and return.

code:

```class Solution {
public String[] uncommonFromSentences(String s1, String s2) {
Map<String,Integer> map1 = new HashMap<>();
Map<String,Integer> map2 = new HashMap<>();
for (String str : s1.split(" ")){
if (!map1.containsKey(str)){
map1.put(str,1);
}else {
map1.put(str,map1.get(str)+1);
}
}
for (String str : s2.split(" ")){
if (!map2.containsKey(str)){
map2.put(str,1);
}else {
map2.put(str,map2.get(str)-1);
}
}
List<String> list = new ArrayList<>();
for (String key : map1.keySet()){
if (map1.get(key) == 1 && !map2.containsKey(key)){
}
}
for (String key : map2.keySet()){
if (map2.get(key) == 1 && !map1.containsKey(key)){
}
}
return list.toArray(new String[0]);
}
}
```

1544. Collating strings

Idea:

1. Convert the string s into a variable string sb, and traverse sb from 0 (up to the penultimate bit).
2. If the absolute value of the difference between the current character and the next character is equal to 32, it means that the current character is lowercase and the next character is uppercase of the same letter, or the current character is uppercase and the next character is lowercase of the same letter; If the absolute value is 32, the two letters should be deleted according to the meaning of the question.
3. The method of deleting these two characters is: delete sb the letter at the current i position twice (because after the first deletion, the original next letter is moved forward by 1 bit to the i position). After deletion, if i is not 0, it needs to be retreated by 1 bit, Because the subsequent letters after deleting two letters may form a pair of letters to be deleted with the letters ahead of the current two letters (except i=0, because it has reached the beginning of the string)
4. Finally, convert the modified sb into String and return it.

code:

```class Solution {
public String makeGood(String s) {
StringBuilder sb = new StringBuilder(s);
int i = 0;
while (i<sb.length()-1){
int cha = sb.charAt(i)-sb.charAt(i+1);
if (Math.abs(cha) == 32){
sb.deleteCharAt(i);
sb.deleteCharAt(i);
i = i > 0 ? i-1 : i;
}else {
i++;
}
}
return sb.toString();
}
}
```

1512. Number of good pairs

Idea:

1. Define a hash table to store all elements in nums and their occurrences.
2. Traverse the values of the hash table, find C (n, 2) for the number of occurrences of each element, add it to ans, and finally return ANS.

code:

```class Solution {
public int numIdenticalPairs(int[] nums) {
Map<Integer,Integer> map = new HashMap<>();
int ans = 0;
for (int n : nums){
if (!map.containsKey(n)){
map.put(n,1);
}else {
map.put(n,map.get(n)+1);
}
}
for (Integer value : map.values()){
ans += value*(value-1)/2;
}
return ans;
}
}
```

tip:

Approaching the Chinese new year, I'm still very busy. I've been waiting for two days. Today, I was rushed home to make nucleic acid. emmm I've been home from Hangzhou for half a month. I have to do it for the whole family. I have to check nucleic acid for the whole family. It's a bandit. I'm so bored

Topics: Java leetcode