Java-使用交替字符合并两个字符串,同时保留运行

时间:2018-03-07 07:41:48

标签: java string merge

将两个字符串合并在一起,使用每个字符串中的交替字符,但同一字符的运行保持在一起。例如,(“abcde”,“xyz”)返回“axbyczde” (“abbcde”,“xxxyzzzz”)返回“axxxbbyczzzzde” 鉴于下面的代码,我得到的输出是“axbxbcydzezzzz”这不仅无法保留运行,而且还添加和删除一些字符。关于我的代码有什么问题的一些帮助将非常感激。提前谢谢。

public class Test {

    public static void main(String[] args) {
        String t = "abbcde";
        String s = "xxxyzzzz";
        String result = "";
        char tcurrent = t.charAt(0);
        char scurrent = s.charAt(0);
        result += tcurrent;
        result += scurrent;
        int i = 1;
        int j = 1;
        while (i < t.length() && j < s.length()) {
            if (tcurrent == t.charAt(i)) {
                result += t.charAt(i);
                i++;
            }
            if (scurrent == t.charAt(j)) {
                result += s.charAt(j);
                j++;
            } else {
                tcurrent = t.charAt(i);
                scurrent = s.charAt(j);
                result += t.charAt(i);
                result += s.charAt(i);
                i++;
                j++;
            }
        }
        while (i < t.length()) {
            result += t.charAt(i);
            i++;
        }
        while (j < s.length()) {
            result += s.charAt(i);
            j++;
        }
        System.out.println(result);
    }
}

2 个答案:

答案 0 :(得分:1)

首先,当你做结果+ = scurrent时,开头会出现问题;没有先检查t.charAt(1)及以下。

此外,还有一个复制粘贴错误: if(scurrent == t.charAt(j)) - &gt;应该是s.charAt(j)

最后,最后的另一个复制粘贴错误:结果+ = s.charAt(i) - &gt;应该是charAt(j)

从概念上讲,你所做错的是一直在s和t之间交替。相反,您应该一次只检查一个字符串并运行while循环,直到遇到不同的字符:

public static void main(String[] args) {
    String t = "abbcde";
    String s = "xxxyzzzz";
    String result = "";

    int tNextIndex = 0;
    int sNextIndex = 0;

    /* alternate while both strings have characters left */
    while (tNextIndex < t.length() && sNextIndex < s.length()) {
        char tPreviousChar = t.charAt(tNextIndex++);
        result += tPreviousChar;
        while (tNextIndex < t.length() &&  t.charAt(tNextIndex) == tPreviousChar){
            result += tPreviousChar;
            tNextIndex++;
        }

        char sPreviousChar = s.charAt(sNextIndex++);
        result += sPreviousChar;
        while (sNextIndex < s.length() &&  s.charAt(sNextIndex) == sPreviousChar){
            result += sPreviousChar;
            sNextIndex++;
        }   
    }

    /* if either of the string was finished, add the remainder of the other one to the result */
    while (tNextIndex < t.length()) {
        result += t.charAt(tNextIndex++);
    }
    while (sNextIndex < s.length()) {
        result += s.charAt(sNextIndex++);
    }
    System.out.println(result);
}

答案 1 :(得分:0)

好吧,如果您是PatternMatcher的粉丝,这将有效 - 相当慢

public static void main(String[] args) {
    String s1 = "abbcde";
    String s2 = "xxxyzzzz";

    Pattern p = Pattern.compile("(\\w)\\1*");
    Matcher m1 = p.matcher(s1);
    Matcher m2 = p.matcher(s2);
    StringBuilder sb = new StringBuilder();

    int start = -1;
    while (m1.find() && m2.find()) {
        sb.append((m1.group() + m2.group()));
        start = m1.end();
    }

    m1.reset(s1.substring(start, s1.length()));

    while (m1.find()) {
        sb.append(m1.group());
    }
    while(m2.find()) {
        sb.append(m2.group());
    }

    System.out.println(sb);

}

O / P:

axxxbbyczzzzde