字符串之数组中两个字符串的最小距离
【摘要】 题目:
给定一个字符串数组strs, 再给定两个字符串str1和str2,返回在strs中str1和str2的最小距离,如果str1和str2为null,或者不再strs中,都返回-1
列如:
strs = {"1","3","3","2","3","1","3"} ,str1 = "1" str2 = "2" 返回2
strs = {"CD"},st...
题目:
给定一个字符串数组strs, 再给定两个字符串str1和str2,返回在strs中str1和str2的最小距离,如果str1和str2为null,或者不再strs中,都返回-1
列如:
strs = {"1","3","3","2","3","1","3"} ,str1 = "1" str2 = "2" 返回2
strs = {"CD"},str1 = "CD",str2="AC" 返回-1
代码实现:
-
package com.chenyu.string.cn;
-
-
public class MinDistance {
-
public static void main(String[] args) {
-
String[] ss = {"a", "b", "c", "d", "c", "a", "d"};
-
int result = minDistance(ss, "a", "d");
-
System.out.println(result);
-
}
-
-
public static int minDistance(String[] target, String str1, String str2) {
-
if (target == null || target.length == 0) {
-
return -1;
-
}
-
if (str1 == nu
文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。
原文链接:chenyu.blog.csdn.net/article/details/53446580
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)