返回具有字符 [a, b, b, c] 的单词。

huangapple go评论69阅读模式
英文:

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);
        }
    }
}

这段代码执行以下操作:

  1. 建立所需字符的字符频率映射。
  2. 遍历字符串列表,对每个字符串构建字符频率映射,并检查是否包含足够数量的所需字符。
  3. 如果字符串符合条件,将其添加到结果列表中。
  4. 最后,输出结果列表中的字符串。
英文:

Given a list of strings [&quot;abc&quot;, &quot;abcdb&quot;, &quot;gggh&quot;, &quot;klsrtabcabc&quot;, &quot;118*90&quot;, &quot;ggggggggggggggggggggggggggggggggg&quot;]

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

  1. 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}

  2. 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=&quot;abbc&quot;;

    //1. Build a character frequency map from the list of required characters.
            
    Map&lt;Character,Integer&gt; hm=new LinkedHashMap&lt;Character,Integer&gt;();
       
    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&lt;arr.length;i++)
{
Map&lt;Character,Integer&gt; temp = hm;
for(Character c : arr[i].toCharArray())
{
if(!temp.containsKey(c) or temp[c]&lt;=0)
break the loop;
else
temp[c]--;
}
if(end of arr[i] charArray)
answer.add(arr[i]);
}

huangapple
  • 本文由 发表于 2020年8月4日 23:55:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63250632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定