如何计算两个字符串之间的字符交集?
例如(假设我们有一个名为String.intersection
):
"abc".intersection("ab") = 2
"hello".intersection("hallo") = 4
好的,孩子们,感谢你们的大量反馈。更多示例:
"aaa".intersection("a") = 1
"foo".intersection("bar") = 0
"abc".intersection("bc") = 2
"abc".intersection("ac") = 2
"abba".intersection("aa") = 2
更多注意事项:
维基百科定义路口 http://en.wikipedia.org/wiki/Intersection_%28set_theory%29如下:
集合 A 和 B 的交集,
表示为 A ∩ B,是所有的集合
既是 A 又是 A 的成员的对象
B. {1, 2, 3} 和的交集
{2, 3, 4} 是集合 {2, 3}
Use 字符串#count http://ruby-doc.org/core/classes/String.html#M001203:
irb(main):001:0> "hello".count("hallo")
=> 4
irb(main):002:0> "abc".count("ab")
=> 2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)