我在技术面试中被问到这个问题。问题是:给定一个目标和一个字符串数组,返回一个包含所有与目标只有一个不同之处的字符串的数组。
例如,如果目标是cat,则catt、caT、caa、ca、at都只有一个不同之处。相反,cat、cattt、dog、flower、c都不只有一个不同之处,因此不应该返回。
oneDiff(String target, String[] a) ...
我的方法是:
ans = []
for all element e in the array
count -> 0
if absoulte(e's length - target length) > 1
continue
endif
for all character c in e
scan through, increment count if difference is found.
endfor
if (count == 1)
continue
else
add e to ans
endfor
return ans
但是面试官对上面的内容不满意。有没有任何高效/聪明的想法?
谢谢。
ccat
怎么样?它应该有一个差异为1吗? - Roel Strolenberg