首页 > 解决方案 > 列表中的子字符串

问题描述

我有一个类和方法

class Dictionary {
    public Dictionary(List<String> dic) {
        // ...
    }
    
    public int getCount(String substr) {
        // ...
    }
}

应该发生什么:
getCount方法中,您需要使用类的构造函数中的列表并找到所有以子字符串substr开头的字符串

我在面试中使用这个解决方案

return (int) this.dic.stream().filter(s -> s.startsWith(substr)).count();

复杂度为 O(n)

有更好的解决方案吗?

谢谢!

标签: javalightweight-stream-api

解决方案


推荐阅读