自己寫的答題思路,做個記錄。反正是沒過的,千萬别當真,大機率是個反例。
/**
*
* @Title: searchKeywordInText
* @Description: 查詢關鍵字索引
* @param: @param text
* @param: @param keywords
* @param: @return
* @return: Map<String,String>
* @throws
*/
public Map<String, String> searchKeywordInText(String text, List<String> keywords) throws KeyNotFoundException {
// 存放結果 <key,2_5_7>
Map<String, String> result = new HashMap<String, String>();
keywords.forEach(key -> {
// 篩選包含目标文本的
if (key.indexOf(text) > 0) {
// 對keywords按text作分割
String[] spArr = key.split(text);
// 同個key可能出現多個目标文本,對位置進行拼接
StringBuffer indexbuffer = new StringBuffer();
for (int i = 0; i < spArr.length; i++) {
indexbuffer.append(spArr[i].length());
if (i + 1 < spArr.length) {
// "2_5_7"
indexbuffer.append("_");
}
}
// keywords沖突的話....這裡沒解決
result.put(key, indexbuffer.toString());
}
});
if (result.isEmpty()) {
throw new KeyNotFoundException("目标檔案無比對!");
}
return result;
}
有大佬知道的話,麻煩指點一手,不勝感激!!!!