Java 如何在字符串中查找带字符的最长距离

Java 如何在字符串中查找带字符的最长距离,java,arrays,sorting,logic,Java,Arrays,Sorting,Logic,我想先获得“这是我在世界上最大的成就”距离b/w a=i和最后一个我喜欢的t=1,h=2,i=3,s=4,空间的计数不应小于i=5,s=6,m=7,y=8,g=9i=27so firsti=3lasti=27so最长距离b/w24请为此编写一个代码,您可以使用它来获取字符的第一个索引,以及获取最后一个索引,例如: public class LongestAlphabetDistance { public int longDistance(String str, String a) {

我想先获得“这是我在世界上最大的成就”距离b/w
a=i
和最后一个我喜欢的
t=1,h=2,i=3,s=4,
空间的计数不应小于
i=5,s=6,m=7,y=8,g=9
i=27
so first
i=3
last
i=27
so最长距离
b/w
24请为此编写一个代码,您可以使用它来获取字符的第一个索引,以及获取最后一个索引,例如:

public class LongestAlphabetDistance {

    public int longDistance(String str, String a) {
        str = "this is my greatest achievement in the world";
        a = "i";
        int count =0;
        // code logic for longest difference
        return count;
    }
}

注意这两种方法
indexOf
lastIndexOf
从0开始,因此您可以得到2和24,而不是3和27

您可以使用它们来获取字符的第一个索引和最后一个索引,例如:

public class LongestAlphabetDistance {

    public int longDistance(String str, String a) {
        str = "this is my greatest achievement in the world";
        a = "i";
        int count =0;
        // code logic for longest difference
        return count;
    }
}


注意这两种方法
indexOf
lastIndexOf
从0开始,如果拼写正确,则得到2和24,而不是3和27,距离为25:

String str = "this is my greatest achivement in the world";
String a = "i";

int indexFirst = str.replace(" ", "").indexOf(a);//this return 2
int indexLast = str.replace(" ", "").lastIndexOf(a);//this return 26

int dist = indexLast - indexFirst; //this return 24
输出:

public static void main(String[] args) {

        String str = "this is my greatest achievement in the world".replaceAll(" ", "");
        String a = "i";
        int firstI = str.indexOf(a);
        int lastI = str.lastIndexOf(a);

        System.out.printf("%s%n%s%d%n%s%d%n%s%d", str, "First i: ", firstI, "Last i: ", lastI, "Distance: ", (lastI - firstI));

    }

如果拼写正确,则距离为25:

String str = "this is my greatest achivement in the world";
String a = "i";

int indexFirst = str.replace(" ", "").indexOf(a);//this return 2
int indexLast = str.replace(" ", "").lastIndexOf(a);//this return 26

int dist = indexLast - indexFirst; //this return 24
输出:

public static void main(String[] args) {

        String str = "this is my greatest achievement in the world".replaceAll(" ", "");
        String a = "i";
        int firstI = str.indexOf(a);
        int lastI = str.lastIndexOf(a);

        System.out.printf("%s%n%s%d%n%s%d%n%s%d", str, "First i: ", firstI, "Last i: ", lastI, "Distance: ", (lastI - firstI));

    }
public class LongestAlphabetDistance{
公共整数长距离(字符串str,字符串a){
str=“这是我在世界上最伟大的成就”;
a=“i”;
str=str.replaceAll(“\\s”和“);
char[]chArray=str.toCharArray();
ArrayList inArray=新的ArrayList();
对于(int i=0;i
public class LongestAlphabetDistance){
公共整数长距离(字符串str,字符串a){
str=“这是我在世界上最伟大的成就”;
a=“i”;
str=str.replaceAll(“\\s”和“);
char[]chArray=str.toCharArray();
ArrayList inArray=新的ArrayList();

对于(int i=0;我能分享你到目前为止尝试过的吗?我们不是来做你的家庭作业的!我们是来问问题的,不是来做你的工作的!@ControlAltDel老实说,我甚至不理解这个问题。程序是否应该只计算
字符串中的字符数(减去空格)或者字符之间有一些距离?还有一个输入错误,应该是achi
e
vement。家庭作业与否,对我来说,这看起来像是一个写代码的请求,在堆栈溢出上并不流行。你能分享一下你到目前为止尝试过的吗?我们不是来做你的家庭作业的!我们是来问问题的,不是来做你的工作的!@ControlAltDel我甚至都不知道你在做什么诚实地理解这个问题。程序应该只计算
字符串
(减去空格)中的字符数还是字符之间的距离?还有一个输入错误,它应该是achi
e
vement。家庭作业与否,对我来说,它看起来像是一个代码编写请求,在堆栈溢出时不受欢迎。