英文:
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 && 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
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<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);
}
}
答案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) > 0
and then make recursive calls.
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
glnkb
gnkbl
gnklb
gnlkb
ngkbl
ngklb
nglkb
nkbgl
nkgbl
nkglb
bnzh
bzhn
bznh
zbhn
zbnh
zhbn
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论