简体   繁体   English

从字符串数组中获取包含指定字符的字符串列表

[英]Get a list of strings that contain a specified character from an array of string

I just had an interview, and the interviewer asked me the following question: 我刚刚接受采访,采访者问我以下问题:

From an array of String (English words), write a function that returns all the words that contain a character. 从一个字符串数组(英语单词)中,编写一个函数,该函数返回所有包含字符的单词。

I initially proposed something like: 我最初建议是这样的:

public static List<String> find(String[] array, char c) {
     if(null ==  array || array.length == 0)
          return null;

     List<String> res = new ArrayList<String>();
     for(String s : array) {
          if(s.indexOf(c) > -1) {
               res.add(s);
          }
     }
     return res;
}

Then, the interviewer asked me to optimize my solution, what kind of data structure could be used. 然后,面试官要求我优化我的解决方案,可以使用哪种数据结构。 (She said my solution is brute-force) (她说我的解决办法是蛮力的)

I still don't see how I can optimize this. 我仍然看不到如何优化它。 Any ideas? 有任何想法吗?

You cannot optimize this function further, but I suppose interviewer wanted to hear something like this: 您无法进一步优化此功能,但我想面试官想听听这样的话:

If we need to perform this operation several times on the same array, we can use following class (writing in pseudo code): 如果我们需要在同一数组上多次执行此操作,则可以使用以下类(用伪代码编写):

class FastStringArrayContainsChar {
    private Map<Char, Set<String>> index = new HashMap<Char, Set<String>>();
    public FastStringArrayContainsChar(String[] input) {
        for (String s: input) {
            for (int i=0; i<s.length; s++) {
                char c = s.charAt(i);
                if (index.contains(c))
                    index.get(c).put(s);
                else
                    index.put(c, new HashSet<String>(){{this.put(s);}});
            }
        }
    }
    public List<String> containsChar(char c) {
        return new ArrayList<String>(index.get(c));
    }
}

To check that the ith string contains character c , you check it letter by letter and return once found. 要检查ith字符串是否包含字符c ,请逐字母对其进行检查,并在找到后返回。

boolean check(String s, char c)
{   for(int i = 0, n = s.length(); i < n; i++)
       if(s[i] == c)
          return true;
    return false;
}

This does not change the worst-case time but might run fast in practice. 这不会改变最坏情况的时间,但实际上可以快速运行。 Other than this I don't see how you can further improve code (i'm assuming that no other information was given regarding words in array). 除此之外,我没有看到如何进一步改善代码(我假设没有给出有关数组中单词的其他信息)。

Perhaps input array contains duplicates. 输入数组可能包含重复项。 It's using more memory though 虽然它使用更多的内存

public static List<String> find(String[] array, char c) {
     if(array == null || array.length == 0) {
        return null;
     }
     Map<String, Boolean> map = new HashMap<String, Boolean>();
     List<String> res = new ArrayList<String>();
     for(String s : array) {
        if (map.containsKey(s)) {
            if (map.get(s)) {
                res.add(s);
            }
        } else {
            if(s.indexOf(c) > -1) {
                 map.put(s, true);
                 res.add(s);
            } else {
                map.put(s, false);
            }
        }

     }
     return res;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM