在Ruby中找到两个字符串中第一个不同的字符

3

我有两个字符串。

str_a = "the_quick_brown_fox"
str_b = "the_quick_red_fox"

我想找到两个字符串第一个不同的字符(如 str_a[i] != str_b[i] )的索引。
我知道可以使用以下代码解决这个问题:
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

有没有更好的方法来做这个?

4个回答

8

类似这样的代码应该可以工作:

str_a.each_char.with_index
  .find_index {|char, idx| char != str_b[idx] } || str_a.size

编辑:它可以工作:http://ideone.com/Ttwu1x
编辑2:如果str_astr_b短,我的原始代码会返回nil。我已经更新它以正确处理(它将返回str_a.size,因此如果str_a中的最后一个索引是3,则它将返回4)。

这里有另一种方法,可能会使一些人觉得略微简单:

(0...str_a.size).find {|i| str_a[i] != str_b[i] } || str_a.size

http://ideone.com/275cEU


2
我更喜欢使用str_a.each_char.with_index,因为它不会创建中间数组。尽管如此,加一分。 - Amadan
很好的发现,@Amadan。我总是忘记了 each_char - Jordan Running

1
i = 0
i += 1 while str_a[i] and str_a[i] == str_b[i]
i

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 

为什么不直接写Float::INFINITY而不是(1.0)/0 - Jordan Running
@Jordan,我不应该这样轻率。我同意 Float::INFINITY 更好。事实上,我最初写的就是这个,但随后为了那些不熟悉 (1.0)/0 的读者而做了更改。 - Cary Swoveland

0

这里使用二分查找来找到一个切片str_a不再出现在str_b开头的索引:

(0..str_a.length).bsearch { |i| str_b.rindex(str_a[0..i]) != 0 }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接