java - 列表中的子字符串
问题描述
我有一个类和方法
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)
有更好的解决方案吗?
谢谢!
解决方案
推荐阅读
- bash - 脚本在容器中失败,但奇异性返回 0(无错误)
- python - 将元组作为键的字典转换为 JSON
- haproxy - 使用设备检测在 macOS 上安装 HAProxy
- flutter - 如何使用 provider 更新特定的小部件?下面是我为解释问题而创建的示例场景
- office-js - 插入 Ooxml 并保留原始 XML 的结构
- python - RelatedObjectDoesNotExist at : 用户没有用户配置文件
- mongodb - MongoDB 仅选择类型对象
- javascript - HTML 将 4 个不同大小的表格并排放置
- javascript - Get Leaflet map tile image for polygon
- mysql - MySQL config uses 1G per thread - how do I reduce that?