简体   繁体   English

如何将char数组存储到字符串数组中并在java中对该字符串数组进行排序?

[英]how to store the char array into string array and sort that string array in java?

Given 2 strings A and B, print all the interleavings of the 2 strings.给定 2 个字符串 A 和 B,打印 2 个字符串的所有交错。 An interleaved string of given two strings preserves the order of characters in individual strings and uses all the characters of both the strings.给定两个字符串的交错字符串保留单个字符串中字符的顺序并使用两个字符串的所有字符。 For simplicity, you can assume that the strings have unique characters.为简单起见,您可以假设字符串具有唯一字符。

input:输入:
2 2
nkb gl NKB GL
bn zh bn zh
i wrote some code but im not getting the sorted strings.我写了一些代码,但我没有得到排序的字符串。

public class Test {

static void interleavings(String A, String B, char[] ans, int m, int n, int idx) 
{
    if(m==0 && n==0) 
    {
        System.out.println(ans);
        return;
    } 
    if(m != 0) 
    {
        ans[idx]=A.charAt(0);
        interleavings(A.substring(1,m), B, ans, m-1, n, idx+1);
    } 
    if(n != 0) 
    {
        ans[idx]= B.charAt(0);
        interleavings(A, B.substring(1,n), ans, m, n-1, idx+1);
    }
}
public static void main(String[] args) 
{
    Scanner s =  new Scanner(System.in);
    int t = s.nextInt();
    for(int i = 0; i < t; i++)
    {
        String s1=s.next();
        String s2=s.next();
        char [] ans = new char[s1.length()+s2.length()];
        System.out.println("Case #"+(i+1)+":");
        interleavings(s1,s2,ans,s1.length(),s2.length(),0);  
    }
}

} }

My output我的 output
Case #1:情况1:
nkbgl nkbgl
nkgbl nkgbl
nkglb nkglb
ngkbl ngkbl
ngklb ngklb
nglkb nglkb
gnkbl gnkbl
gnklb gnklb
gnlkb gnlkb
glnkb glnkb
Case #2:案例2:
bnzh bnzh
bznh bznh
bzhn bzhn
zbnh zbnh
zbhn zbhn
zhbn zhbn
Expected output预期 output
Case #1:情况1:
glnkb glnkb
gnkbl gnkbl
gnklb gnklb
gnlkb gnlkb
ngkbl ngkbl
ngklb ngklb
nglkb nglkb
nkbgl nkbgl
nkgbl nkgbl
nkglb nkglb
Case #2:案例2:
bnzh bnzh
bzhn bzhn
bznh bznh
zbhn zbhn
zbnh zbnh
zhbn zhbn

I'm new to java can someone please guide me.我是 java 的新手,有人可以指导我。 how to over come this problem please.请问如何解决这个问题。

I would no use char array as it will make things more complicated.我不会使用char数组,因为它会使事情变得更复杂。 I would pass a current value as string.我会将当前值作为字符串传递。 See curr + A.charAt(0) this will append the char to a String , this way you are not dealing with char .请参阅curr + A.charAt(0)这会将 append char转换为String ,这样您就不会处理char

Store results in Collection .将结果存储在Collection中。 Than you can sort the whole collection an print it when done, instead of doing it on the fly.你可以在完成后对整个集合进行排序并打印它,而不是即时进行。

class Main {

    public static void interleavings(String curr, String A, String B, Set<String> result) {
        if (A.length() == 0 && B.length() == 0) {
            result.add(curr);
            return;
        }
        if (A.length() > 0) {
            interleavings(curr + A.charAt(0), A.substring(1), B, result);
        }
        if (B.length() > 0) {
            interleavings(curr + B.charAt(0), A, B.substring(1), result);
        }
    }

    public static void main(String[] args) {
        String A = "bn";
        String B = "zh";

        Set<String> result = new HashSet<>();
        interleavings("", A, B, result);

        // this will print all the results sorted
        result.stream()
              .sorted()
              .forEach(System.out::println);
    }
}

You should swap a and b when a.compareTo(b) > 0 and then make recursive calls.a.compareTo(b) > 0时,您应该交换ab ,然后进行递归调用。

static void interleavings(String a, String b, String result) {
    if (a.isEmpty() && b.isEmpty())
        System.out.println(result);
    else if (a.compareTo(b) > 0)
        interleavings(b, a, result);
    else {
        if (!a.isEmpty())
            interleavings(a.substring(1), b, result + a.charAt(0));
        if (!b.isEmpty())
            interleavings(a, b.substring(1), result + b.charAt(0));
    }
}

and

interleavings("nkb", "gl", "");
interleavings("bn", "zh", "");

output output

glnkb
gnkbl
gnklb
gnlkb
ngkbl
ngklb
nglkb
nkbgl
nkgbl
nkglb
bnzh
bzhn
bznh
zbhn
zbnh
zhbn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM