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

huangapple go评论67阅读模式
英文:

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

问题

给定两个字符串 A 和 B请打印出所有可能的交错组合所谓交错组合是指保留每个字符串中字符的顺序并使用两个字符串的所有字符为简单起见可以假设这两个字符串只包含不重复的字符

输入
2
nkb gl
bn zh

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);
        }
    }
}

我的输出
Case #1:
nkbgl
nkgbl
nkglb
ngkbl
ngklb
nglkb
gnkbl
gnklb
gnlkb
glnkb
Case #2:
bnzh
bznh
bzhn
zbnh
zbhn
zhbn

期望的输出
Case #1:
glnkb
gnkbl
gnklb
gnlkb
ngkbl
ngklb
nglkb
nkbgl
nkgbl
nkglb
Case #2:
bnzh
bzhn
bznh
zbhn
zbnh
zhbn

我是新手,有人可以指导我如何解决这个问题吗?

英文:

Given 2 strings A and B, print all the interleavings of the 2 strings. 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
nkb gl
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 &amp;&amp; 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 &lt; t; i++)
{
String s1=s.next();
String s2=s.next();
char [] ans = new char[s1.length()+s2.length()];
System.out.println(&quot;Case #&quot;+(i+1)+&quot;:&quot;);
interleavings(s1,s2,ans,s1.length(),s2.length(),0);  
}
}

}

My output
Case #1:
nkbgl
nkgbl
nkglb
ngkbl
ngklb
nglkb
gnkbl
gnklb
gnlkb
glnkb
Case #2:
bnzh
bznh
bzhn
zbnh
zbhn
zhbn
Expected output
Case #1:
glnkb
gnkbl
gnklb
gnlkb
ngkbl
ngklb
nglkb
nkbgl
nkgbl
nkglb
Case #2:
bnzh
bzhn
bznh
zbhn
zbnh
zhbn

I'm new to java can someone please guide me. how to over come this problem please.

答案1

得分: 2

我不会使用char数组,因为这会使事情变得更加复杂。我会将当前值作为字符串传递。看到curr + A.charAt(0),这将把char附加到一个String,这样你就不必处理char

将结果存储在Collection中。然后在完成后对整个集合进行排序并打印出来,而不是即时进行排序。

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);
    }
}
英文:

I would no use char array as it will make things more complicated. 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.

Store results in 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&lt;String&gt; result) {
if (A.length() == 0 &amp;&amp; B.length() == 0) {
result.add(curr);
return;
}
if (A.length() &gt; 0) {
interleavings(curr + A.charAt(0), A.substring(1), B, result);
}
if (B.length() &gt; 0) {
interleavings(curr + B.charAt(0), A, B.substring(1), result);
}
}
public static void main(String[] args) {
String A = &quot;bn&quot;;
String B = &quot;zh&quot;;
Set&lt;String&gt; result = new HashSet&lt;&gt;();
interleavings(&quot;&quot;, A, B, result);
// this will print all the results sorted
result.stream()
.sorted()
.forEach(System.out::println);
}
}

答案2

得分: 2

`a.compareTo(b) > 0`您应该交换 `a``b`,然后进行递归调用

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));
    }
}


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

输出

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

You should swap a and b when a.compareTo(b) &gt; 0 and then make recursive calls.

static void interleavings(String a, String b, String result) {
if (a.isEmpty() &amp;&amp; b.isEmpty())
System.out.println(result);
else if (a.compareTo(b) &gt; 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(&quot;nkb&quot;, &quot;gl&quot;, &quot;&quot;);
interleavings(&quot;bn&quot;, &quot;zh&quot;, &quot;&quot;);

output

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

huangapple
  • 本文由 发表于 2020年8月17日 15:36:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63446530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定