Skip to content

Commit

Permalink
Merge branch 'master' into whp98
Browse files Browse the repository at this point in the history
  • Loading branch information
whp98 committed Oct 11, 2023
2 parents bd38a39 + 1982902 commit ded93e2
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions app/src/main/kotlin/com/aistra/hail/utils/FuzzySearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ object FuzzySearch {
val lenTextToSearch = textToSearchUpp.length
return diff < lenTextToSearch && containsInOrder(textToSearchUpp, queryUpp)
}

/**
* 判断一个字符串A是否依次包含另一个字符串B的每个字符,并且这些字符是按顺序从A的开头开始的
* @param strA
Expand All @@ -38,19 +39,27 @@ object FuzzySearch {
// 在字符串A的当前位置之后查找字符charB
val foundIndex = strA.indexOf(charB, indexA)
// 如果未找到字符或者字符的位置不是当前位置,表示不包含按顺序的字符
if (foundIndex == -1 || foundIndex != indexA) {
if (foundIndex == -1) {
return false
}
// 移动到下一个位置,以便查找下一个字符
indexA = foundIndex + 1
}
return true
}

private fun assertTrue(res: Boolean) {
if (!res){
throw RuntimeException("测试失败!")
}
}

@JvmStatic
fun main(args: Array<String>) {
assert(search("支付宝", ""))
assert(search("World Peace", "wp"))
assert(search("World Peace", "pee"))
assert(!search("World Peace", "dow"))
assertTrue(search("支付宝", ""))
assertTrue(search("World Peace", "wp"))
assertTrue(search("World Peace", "pee"))
assertTrue(!search("World Peace", "dow"))
assertTrue(containsInOrder("小费计算器", "小器"))
}
}

0 comments on commit ded93e2

Please sign in to comment.