I have two strings.
我有两个字符串。
str_a = "the_quick_brown_fox"
str_b = "the_quick_red_fox"
I want to find the first index at which the two strings differ (i.e. str_a[i] != str_b[i]
).
我想找到两个字符串不同的第一个索引(即str_a[I] != str_b[I])。
I know I could solve this with something like the following:
我知道我可以用以下方法来解决这个问题:
def diff_char_index(str_a, str_b)
arr_a, arr_b = str_a.split(""), str_b.split("")
return -1 unless valid_string?(str_a) && valid_string?(str_b)
arr_a.each_index do |i|
return i unless arr_a[i] == arr_b[i]
end
end
def valid_string?(str)
return false unless str.is_a?(String)
return false unless str.size > 0
true
end
diff_char_index(str_a, str_b) # => 10
Is there a better way to do this?
有更好的方法吗?
4 个解决方案
#1
6
Something like this ought to work:
像这样的东西应该有用:
str_a.each_char.with_index
.find_index {|char, idx| char != str_b[idx] } || str_a.size
Edit: It works: http://ideone.com/Ttwu1x
Edit 2: My original code returned nil
if str_a
was shorter than str_b
. I've updated it to work correctly (it will return str_a.size
, so if e.g. the last index in str_a
is 3, it will return 4).
编辑:它工作:http://ideone.com/Ttwu1x编辑2:如果str_a小于str_b,我的原始代码返回nil。我已经更新它以正确工作(它将返回str_a。大小,例如,如果str_a中的最后一个索引是3,它将返回4)。
Here's another method that may strike some as slightly simpler:
还有一种方法可能会让一些人觉得有点简单:
(0...str_a.size).find {|i| str_a[i] != str_b[i] } || str_a.size
http://ideone.com/275cEU
#2
1
i = 0
i += 1 while str_a[i] and str_a[i] == str_b[i]
i
#3
1
str_a = "the_quick_brown_dog"
str_b = "the_quick_red_dog"
(0..(1.0)/0).find { |i| (str_a[i] != str_b[i]) || str_a[i].nil? }
#=> 10
str_a = "the_quick_brown_dog"
str_b = "the_quick_brown_dog"
(0..(1.0)/0).find { |i| (str_a[i] != str_b[i]) || str_a[i].nil? }
#=> 19
str_a.size
#=> 19
#4
0
This uses a binary search to find the index where a slice of str_a
no longer occurs at the beginning of str_b
:
这使用了一个二分查找来查找在str_b开始时不再出现的str_a片的索引:
(0..str_a.length).bsearch { |i| str_b.rindex(str_a[0..i]) != 0 }
#1
6
Something like this ought to work:
像这样的东西应该有用:
str_a.each_char.with_index
.find_index {|char, idx| char != str_b[idx] } || str_a.size
Edit: It works: http://ideone.com/Ttwu1x
Edit 2: My original code returned nil
if str_a
was shorter than str_b
. I've updated it to work correctly (it will return str_a.size
, so if e.g. the last index in str_a
is 3, it will return 4).
编辑:它工作:http://ideone.com/Ttwu1x编辑2:如果str_a小于str_b,我的原始代码返回nil。我已经更新它以正确工作(它将返回str_a。大小,例如,如果str_a中的最后一个索引是3,它将返回4)。
Here's another method that may strike some as slightly simpler:
还有一种方法可能会让一些人觉得有点简单:
(0...str_a.size).find {|i| str_a[i] != str_b[i] } || str_a.size
http://ideone.com/275cEU
#2
1
i = 0
i += 1 while str_a[i] and str_a[i] == str_b[i]
i
#3
1
str_a = "the_quick_brown_dog"
str_b = "the_quick_red_dog"
(0..(1.0)/0).find { |i| (str_a[i] != str_b[i]) || str_a[i].nil? }
#=> 10
str_a = "the_quick_brown_dog"
str_b = "the_quick_brown_dog"
(0..(1.0)/0).find { |i| (str_a[i] != str_b[i]) || str_a[i].nil? }
#=> 19
str_a.size
#=> 19
#4
0
This uses a binary search to find the index where a slice of str_a
no longer occurs at the beginning of str_b
:
这使用了一个二分查找来查找在str_b开始时不再出现的str_a片的索引:
(0..str_a.length).bsearch { |i| str_b.rindex(str_a[0..i]) != 0 }