- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用这段 Java 代码来查找相似的字符串:
if( str1.indexof(str2) >= 0 || str2.indexof(str1) >= 0 ) ......
但是对于 str1 = "pizzabase"
和 str2 = "namedpizzaowl"
它不起作用。
最佳答案
遍历 str1
中的每个字母,检查它是否存在于 str2
中。如果它不存在,则继续下一个字母,如果存在,则将您在 str2
中检查的 str1
中的子字符串的长度增加到两个字符,并重复直到找不到更多匹配项或您已遍历 str1
。
这会发现所有共享的子串,但是 - 就像冒泡排序 - 几乎不是最优的,而是一个如何解决问题的非常基本的例子。
类似于这个伪示例:
pos = 0
len = 1
matches = [];
while (pos < str1.length()) {
while (str2.indexOf(str1.substring(pos, len))) {
len++;
}
matches.push(str1.substring(pos, len - 1));
pos++;
len = 1;
}
关于java - 从两个字符串中找到相似子串的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3509163/
我正在尝试创建一个程序,其中字符串的前三个字符重复给定次数,如下所示: foo('Chocolate', 3) # => 'ChoChoCho' foo('Abc', 3) # => 'AbcAbcA
我有以下字符串: std::string str = "Mode:AAA:val:101:id:A1"; 我想分离一个位于 "val:" 和 ":id" 之间的子字符串,这是我的方法: std::st
DNA 字符串可以是任意长度,包含 5 个字母(A、T、G、C、N)的任意组合。 压缩包含 5 个字母(A、T、G、C、N)的 DNA 字母串的有效方法是什么?不是考虑每个字母表 3 位,我们可以使用
是否有一种使用 levenstein 距离将一个特定字符串与第二个较长字符串中的任何区域进行匹配的好方法? 例子: str1='aaaaa' str2='bbbbbbaabaabbbb' if str
使用 OAuth 并使用以下函数使用我们称为“foo”(实际上是 OAuth token )的字符串加密 key public function encrypt( $text ) { // a
我是一名优秀的程序员,十分优秀!