英文:
Return words with characters [a,b,b,c]
问题
以下是第2和第3步的Java代码:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Practice {
public static void main(String[] args) {
String str1 = "abbc";
// 1. Build a character frequency map from the list of required characters.
Map<Character, Integer> requiredChars = new HashMap<>();
for (Character c : "abbc".toCharArray()) {
requiredChars.put(c, requiredChars.getOrDefault(c, 0) + 1);
}
List<String> inputStrings = new ArrayList<>();
inputStrings.add("abc");
inputStrings.add("abcdb");
inputStrings.add("gggh");
inputStrings.add("klsrtabcabc");
inputStrings.add("118*90");
inputStrings.add("ggggggggggggggggggggggggggggggggg");
List<String> result = new ArrayList<>();
// 2. Iterate the list of strings:
for (String str : inputStrings) {
// 2.1 Build a character frequency map from the string.
Map<Character, Integer> strChars = new HashMap<>();
for (Character c : str.toCharArray()) {
strChars.put(c, strChars.getOrDefault(c, 0) + 1);
}
// 2.2 Check if the string has at least the required number of characters.
boolean isValid = true;
for (Map.Entry<Character, Integer> entry : requiredChars.entrySet()) {
char requiredChar = entry.getKey();
int requiredCount = entry.getValue();
int actualCount = strChars.getOrDefault(requiredChar, 0);
if (actualCount < requiredCount) {
isValid = false;
break;
}
}
// 2.3 If selected, add the string to the result.
if (isValid) {
result.add(str);
}
}
// 3. Return the list of results.
for (String word : result) {
System.out.println(word);
}
}
}
这段代码执行以下操作:
- 建立所需字符的字符频率映射。
- 遍历字符串列表,对每个字符串构建字符频率映射,并检查是否包含足够数量的所需字符。
- 如果字符串符合条件,将其添加到结果列表中。
- 最后,输出结果列表中的字符串。
英文:
Given a list of strings ["abc", "abcdb", "gggh", "klsrtabcabc", "118*90", "ggggggggggggggggggggggggggggggggg"]
Return words which contain all letters from [a,b,b,c]
.(This means words with, at least, 'a' one time, 'b' two times, and 'c' one time)
Answer: "abcdb", "klsrtabcabc"
This is the hashmap algorithm for this
-
Build a character frequency map from the list of required characters. E.g. for
[a,b,b,c]
you end up with{a=1, b=2, c=1}
-
Iterate the list of strings:
> 1. Build a character frequency map from the string. E.g. for "klsrtabcabc" you end up with {a=2, b=2, c=2, k=1, l=1, r=1, s=1, t=1}
> 2. Check if string has at least the required number of characters. E.g. since a: 2 >= 1, b: 2 >= 2, and c: 2 >= 1, the string is selected.
> 3. If selected, add the string to the result.
3. Return the list of results.
I converted the first step into java code as shown as below, can anyone help me with step 2 and 3?
package JavaInterview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class practice {
public static void main(String[] args) {
String str1="abbc";
//1. Build a character frequency map from the list of required characters.
Map<Character,Integer> hm=new LinkedHashMap<Character,Integer>();
for(Character c : str1.toCharArray()) {
if(hm.containsKey(c)) {
hm.put(c, hm.get(c)+1);
}
else {
hm.put(c, 1);
}
}
}
}
答案1
得分: 1
Ideally you should try the steps 2 and 3 and come to SO when you are stuck with an issue and you can't find an answer. For now, I will give you my approach - Just a pseudocode:
for(int i=0;i<arr.length;i++)
{
Map<Character,Integer> temp = hm;
for(Character c : arr[i].toCharArray())
{
if(!temp.containsKey(c) or temp[c]<=0)
break the loop;
else
temp[c]--;
}
if(end of arr[i] charArray)
answer.add(arr[i]);
}
英文:
Ideally you should try the steps 2 and 3 and come to SO when you are stuck with an issue and you can't find an answer. For now, I will give you my approach - Just a pseudocode:
for(int i=0;i<arr.length;i++)
{
Map<Character,Integer> temp = hm;
for(Character c : arr[i].toCharArray())
{
if(!temp.containsKey(c) or temp[c]<=0)
break the loop;
else
temp[c]--;
}
if(end of arr[i] charArray)
answer.add(arr[i]);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论