How to "minify" an array - Java - java

So suppose I create an array with 5 spaces, like so - String[] myArray = new String[5]. Then suppose I define some of those items, but leave some of them null (or whatever the Java term is for an undefined array item/variable), like so:
myArray[0] = "foo";
myArray[2] = "bar";
myArray[4] = "foobar";
Is there a way I can "minify" that array, squeezing out all the null items (not changing the size of it)? So that the index of "foo" stays at 0, but "bar"'s index becomes 1, and "foobar" resides at 2, with the last 2 spaces being empty? Long story short - shuffle around the items in an array, pushing all the null items to the end, while maintaining the relative order of the other items. Is there already a predefined Java method for that, or do I need to make my own?

You could use the following approach (without overhead of Collections instance):
import java.util.Arrays;
public class ArraySample {
public static void main(final String[] args) {
String[] src = new String[] { "foo", null, "bar", null, "foobar" };
String[] dest = new String[src.length];
int i = 0;
for (String s : src) {
if (s != null) {
dest[i++] = s;
}
}
System.out.println(Arrays.toString(src));
System.out.println(Arrays.toString(dest));
}
}

You can do it with only the source array :
String[] src = new String[] { "foo", "foo3", null,null, "bar", null,null, "foobar", "foo2", null,"foo5",null };
int lastNullIdx = -1;
for (int i=0; i <src.length ; i++) {
if (src[i] == null) {
if(lastNullIdx==-1)lastNullIdx=i;
}
else if(lastNullIdx!=-1){
src[lastNullIdx++]=src[i];
src[i]=null;
}
}
System.out.println(Arrays.toString(src));

In-place minification, simpler code:
public static String[] minify(String[] x) {
int d = 0;
for (String s : x) if (s != null) x[d++] = s;
while (d < x.length) x[d++] = null;
return x;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(
minify(new String[] {"foo", null, "bar", null, "foobar"})));
}

Don't think that there is a method for it. If therewould be something like that, it would be in java.util.Arrays. I would do it like this
String[] src = new String[] { "foo", null, "bar", null, "foobar" };
for (int c = 0, j = 0; c < src.length; c++) {
if (src[c] != null) {
src[j++] = src[c];
src[c] = null;
}
}
edit:
String[] src = new String[] { "foo", null, "bar", null, "foobar" };
Comparator<String> NEW_ORDER = new Comparator<String>() {
public int compare(String e1, String e2) {
if(e1 == null)return 1;
if(e2 == null)return -1;
return 0;
}
Arrays.sort(src, NEW_ORDER);
Should work aswell, since Arrays.sort is a stable sort, but i think the other solution is better, since its O(n) and not O(n log n)

With functionaljava,
array(myArray).filter(new F<String, Boolean>() {
public Boolean f(String s) {
return s != null;
}
});
This will retain the elements that satisfy the given conditions. The result will be the new array returned by filter.
Edit:
Sorry, I misread the question before. Here's how you can squeeze out nulls to right.
array(myArray).sort(
booleanOrd.comap(new F<String, Boolean>() {
public Boolean f(String s) {
return s == null;
}
})
);

Java is already prepared for such sorting problem using the Arrays.sort(T[] a, Comparator c) method:
Arrays.sort(myArray, new StringNullComparator());
with
class StringNullComparator implements Comparator<String> {
#Override
public int compare(String s1, String s2)
{
if(s1==null && s2!=null) return 1;
else if(s1!=null && s2==null) return -1;
else return 0;
}
}
This way you can even sort the null values to lower indecees by inverting the value returned from the compare method.

Related

How do you remove repetitions in string characters,and sort it?

I'm having a problem with this, it is supposed to take 2 string and return the largest one, sorted alphabetically, with no repetitions.
like String x = "xbbacd" and String y = "ppacd"
would return "abcdx".
It's also giving a no output return without System.....ln();
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Collections;
public class MyClass {
public static String longest(String s1, String s2) {
// your code
HashSet<String> list1 = new HashSet<String>();
HashSet<String> list2 = new HashSet<String>();
for (char x : s1.toCharArray()) {
String y = Character.toString(x);
list1.add(y);
}
for (char q : s2.toCharArray()) {
String y = Character.toString(q);
list2.add(y);
}
ArrayList<String> arr1 = new ArrayList<String>();
ArrayList<String> arr2 = new ArrayList<String>();
for (String t : list1) {
arr1.add(t);
}
for (String z : list2) {
arr2.add(z);
}
Collections.sort(arr1);
Collections.sort(arr2);
String one = "";
if (arr1.size() > arr2.size()) {
for (String i : arr1) {
one = one + i;
}
} else {
for (String i : arr2) {
one = one + i;
}
}
// System.out.print(one);
return one;
}
public static void main(String[] args) {
DeleteMe a = new DeleteMe();
a.longest("adfafasf", "xvsdvwv");
}
}
If you check the string length in the beginning you do not need to process both strings, just the longer one. You save quite a bit of coding effort by only working on the longer string. You should also check on how to handle the edge cases of both strings being equal in length, null, empty strings, etc.
Try this:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Collections;
public class MyClass {
public static String longest (String s1, String s2) {
// your code
if (len1 == null) return "String 1 is null";
if (len2 == null) return "String 2 is null";
// first determine which string is longer
int len1 = s1.length();
int len2 = s2.length();
String longerString = null;
if (len1 >= len2) {
longerString = len1;
} else {
longerString = len2;
}
HashSet<String> stringHash = new HashSet<String>();
for(char x : longerString.toCharArray() )
{
String y = Character.toString(x);
stringHash.add(y);
}
ArrayList<String> arr1 = new ArrayList<String>();
for(String t : list1){ arr1.add(t); }
Collections.sort(arr1);
String one = new String();
for(String i : arr1){ one = one + i; }
// System.out.print(one);
return one;
}
public static void main(String[ ] args) {
MyClass a = new MyClass();
System.out.println(a.longest("adfafasf","xvsdvwv"));
}
You could (and most likely should) extract the 'distinct and sort' part of your method into a new method to reduce code-duplication. Your variable names are somewhat confusing (at least for me) as you name a set 'list' and a list 'arr'.
Regarding the no output: You currently do not use the return value of the method, you want your last line to be System.out.println(longest("adfafasf", "xvsdvwv");
Below a refactored version of your version (the creation of the shorter string could be avoided but performance seems negligible for this problem):
public static String longest(
final String s1,
final String s2) {
////
final String ds1 = distinctSorted(s1);
final String ds2 = distinctSorted(s2);
return ds1.length() >= ds2.length() ? ds1 : ds2;
}
private static String distinctSorted(
final String s) {
////
final Set<Character> set = new HashSet<>();
for (final char c : s.toCharArray()) {
set.add(c);
}
final List<Character> list = new ArrayList<>(set);
Collections.sort(list);
final StringBuilder sb = new StringBuilder(list.size());
for (final char c : list) {
sb.append(c);
}
return sb.toString();
}
An alternative for the distinct and sort method:
private static String distinctSorted(
final String s) {
////
return s.chars().sorted().distinct()
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}

Sorting a 2D array with comparator in java for each column

I a working o table sorted for a 2D array with the Array.sort method with a custom comparator
String[] array=new String[]{
"a b c",
"a",
"a b",
"a c"};
I want the output too look as follows
{"a",
"a b",
"a b c",
"a c"}
The compare method for the comparator looks as follows
public int compare(String arg0, String arg1) {
String[] tempArray0 = arg0.split(" +");
String[] tempArray1 = arg1.split(" +");
if (!arg0.isEmpty() && !arg1.isEmpty())
{
return sortStrings(tempArray0[0], tempArray1[0]);
}
The sortStrings methods looks like this
private int sortStrings(String arg0, String arg1) {
if (arg0.toLowerCase().compareTo(arg1) > 0) {
return 1;
} else if (arg0.toLowerCase().compareTo(arg1) < 0) {
return -1;
} else {
return 0;
}
}
This problem works great if you want to just by first column,but for me it is necessary to check that if the column has the same value it should sort the next column and so on.
I know that in this case I would need to catch a IndexOutOfBounds exception since on of the array which are later divided into 2D array has rows of different sizes.
I just ran into a wall with this problem since I have no idea what kind of loop structure to use here
Try this
String[][] array = new String[][] { { "a", "b", "c" }, { "a" }, { "a", "b" }, { "a", "c" } };
Arrays.sort(array, new Comparator<String[]>() {
#Override
public int compare(String[] o1, String[] o2) {
for (int i = 0; i < o1.length; i++) {
if (o2.length == i) return 1;
int comp = o1[i].compareTo(o2[i]);
if (comp != 0)
return comp;
}
return -1;
}
});
should print:
a
ab
abc
ac

Java Mixing variable String[] with String in one only array of strings

Suppose this:
String s0 = "01234";
String[] S1 = {"jkl","abc","xyz"};
String[] S2 = {"OPQ","ghi","STU"};
String s3 = "56789";
get_AllStrings(s3, S1, S2, s0);
The returned String[] must be:
String[] NewArray = {"56789","OPQ","ghi","STU","01234"}
I want to obtain this strings like only one array of strings...
Here my method:
public String[] get_AllStrings(String... argString) { //Only String or String[]
int TheSize = 0;
for (int i = 0; i<argString.length; i++) {
if(argString[i]!= null && argString[i].getClass().isArray()) {
String[] OneArray = (String [])argString[i];
TheSize += OneArray.length;
} else {
TheSize += 1;
}
}
String[] NewArray = new String[TheSize];
int ThePos = 0;
for (int i = 0; i<argString.length; i++) {
if(argString[i]!= null && argString[i].getClass().isArray()) {
String[] OneArray = argString[i];
System.arraycopy(OneArray, 0, NewArray, ThePos, OneArray.length);
ThePos += OneArray.length;
} else {
String[] OneArray = {argString[i]};
System.arraycopy(OneArray, 0, NewArray, ThePos, 1);
ThePos += OneArray.length;
}
}
return NewArray;
}
But, is not working...
What you want to do is to use an ArrayList instead of an array.
public static String[] getAllStrings(Object ... argString) {
ArrayList<String> list = new ArrayList<String>();
for (Object stringOrArray : argString) {
if (stringOrArray instanceof String)
list.add((String) stringOrArray);
else {
String[] strings = (String) stringOrArray;
list.addAll(Arrays.asList(strings));
}
}
return list.toArray(new String[list.size()]);
}
I changed your code a bit and got this:
public static String[] get_AllStrings(Object... argString) {
ArrayList<String> strings = new ArrayList<String>();
for (int i = 0; i<argString.length; i++) {
if(argString[i]!= null && argString[i].getClass().isArray()) {
String[] OneArray = (String [])argString[i];
for(String str : OneArray)
strings.add(str);
} else {
strings.add((String)argString[i]);
}
}
return (String[])strings.toArray();
}
I could not get it to accept both String and String[] with your method signature, but a change to Object... did the trick. You can use an ArrayList to create the array directly instead of looping through everything twice.
unfortunately, you're running up against Java's type system here.
String and String[] are not subtypes.
so a variable, or array can only hold one or the other.
Using object, as done by #Johan Henriksson throws away any type safety assurances from the compiler, since any object can be put in the array. this is okay if you have some garuantee that you'll only ever have Strings, and you'll need to cast to string on pulling out of the collection.
I'm not sure exactly what you're trying to achieve here
So I'm not sure how to go about resolving this.
if you just want all the strings from all sources in a collection of some sort, I'd use a list
it's unclear where you're getting these strings and string arrays from however.
You can't pass a String[] into an element of a varargs String... parameter.
The only way to accept either String or String[] is a "typeless" varargs Object..., because Object is the only common type to both.
public static String[] get_AllStrings(Object... args) {
ArrayList<String> result = new ArrayList<String>();
for (Object o : args) {
if (o instanceof String) {
result.add((String)o);
} else if (o instanceof String[]) {
result.addAll(Arrays.asList((String[])o));
} else {
throw new IllegalArgumentException();
}
}
return (String[])result.toArray();
}

How to remove null from an array in java

I've written a method to remove null-values from an array i need in a program.
The method, however, doesn't seem to work, the null values won't go away. This is my code so far.
public void removeNull(String[] a)
{
for(int i=0; i<a.length; i++)
{
if(a[i] == null)
{
fillArray(a, i);
}
}
}
public void fillArray(String[] a, int i)
{
String[] a2 = new String[a.length-1];
for(int j=0; j<a2.length; j++)
{
if(j<i)
{
a2[j]=a[j];
}
else if(j>i)
{
a2[j]=a[j+1];
}
}
a=a2;
}
Thanks in advance!
I would advocate doing it the simple way unless performance is really a problem:
public String[] removeNull(String[] a) {
ArrayList<String> removedNull = new ArrayList<String>();
for (String str : a)
if (str != null)
removedNull.add(str);
return removedNull.toArray(new String[0]);
}
Streams API version of the solution:
SomeClass[] array = new SomeClass[N];
...
array = Arrays.stream(array).filter(Objects::nonNull).toArray(SomeClass[]::new);
(I post this down to maybe get some thoughts on applicability, relative performance etc)
hi everyone first of all i want to appologize for my english im learning at this moment and this is my first post so i want to try to put my solution about the problem here it is
String[] removeNulls(String[] nullsArray) {
int countNulls = 0;
for (int i = 0; i < nullsArray.length; i++) { // count nulls in array
if (nullsArray[i] == null) {
countNulls++;
}
}
// creating new array with new length (length of first array - counted nulls)
String[] nullsRemoved = new String[nullsArray.length - countNulls];
for (int i = 0, j = 0; i < nullsArray.length; i++) {
if (nullsArray[i] != null) {
nullsRemoved[j] = nullsArray[i];
j++;
}
}
return nullsRemoved;
}
You can't change the reference to a variable in a method and expect it to be reflected in the calling method.
You'll instead have to return the new array.
public String[] removeNull(String[] a)
{
for(int i=0; i<a.length; i++)
{
if(a[i] == null)
{
a = fillArray(a, i);
}
}
return a;
}
public String[] fillArray(String[] a, int i)
{
String[] a2 = new String[a.length-1];
for(int j=0; j<a2.length; j++)
{
if(j<i)
{
a2[j]=a[j];
}
else if(j>i)
{
a2[j]=a[j+1];
}
}
return a2;
}
This way would be faster:
private static String[] removeNulls(String[] strs) {
int i = 0;
int j = strs.length - 1;
while (i <= j) {
if (strs[j] == null) {
--j;
} else if (strs[i] != null) {
++i;
} else {
strs[i] = strs[j];
strs[j] = null;
++i; --j;
}
}
return Arrays.copyOfRange(strs, 0, i);
}
I can see two errors in your code:
Your method fillArray doesn't cover the case i == j
Your assignation a = a2; doesn't have the effect you think it might have. Arguments are passed by value in Java, and your assignment does NOT change the value of a in your first method. Try returning an instance to a2 in fillArray, and assign this value to a in removeNull.
A couple of things:
Don't you wantString[] a2 = new String[a.length-1];` to be
String[] a2 = new String[a.length];
Won't making it length - 1 make it too short?
You need a case for i == j in your code. This is why the nulls aren't getting updated.
What problem are you trying to solve with the second function? It seems complicated given what I thought your problem was.
Try this (I didn't test it):
public String[] removeNull(String[] a) {
String[] tmp = new String[a.length];
int counter = 0;
for (String s : a) {
if (s != null) {
tmp[counter++] = s;
}
}
String[] ret = new String[counter];
System.arraycopy(tmp, 0, ret, 0, counter);
return ret;
}
This way you can remove nulls in one cycle, but it will not resize array:
public static void removeNull(String[] a) {
int nullCount = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == null) {
nullCount++;
} else {
a[i-nullCount] = a[i];
}
}
}
This one creates new array, but includes two cycles:
public static String[] removeNull(String[] a) {
int nullCount = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == null) nullCount++;
}
String[] b = new String[a.length-nullCount];
int j = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] != null) b[j++] = a[i];
}
return b;
}
You can think on optimizing that code using System.arraycopy. I hope the code works.
When removing values in an array, the size changes so you can't keep the same array (you could push the nulls at the end).
The structure close to an array that has a auto-adjustable size is an ArrayList. One option would be :
String[] inputs;
List<String> items = new ArrayList<String>(inputs.length);
for(String input : inputs) {
if (input != null) {
items.add(input);
}
}
String[] outputs = items.toArray(new String[items.size()]);
Performance might be a bit less than working directly with arrays, but because an array has a fixed size, you would need two loops with arrays :
one to count the number of non-null values
after building the array, the same loop to copy the values.
This might not have an ideal performance either, and it is really much more complex to get it right...
Another approach would be to move the nulls at the end, then create a shorter array that wouldn't include the nulls. The idea would be :
String[] strings;
int writeIndex = 0;
int max = strings.length;
for(int readIndex = 0; readIndex < max; readIndex++) {
String read = strings[readIndex];
if (read != null) {
strings[writeIndex++] = read;
}
}
String[] outputs = new String[writeIndex];
System.arraycopy(strings, 0, ouputs, 0, writeIndex);
Well, more people said it before... but I also want to emphasize this solution:
You can use some type of Collection, like ArrayList or List and add only the not null elements. Finally you must return the new String[] formed by the Collection.
Here an example where you can check the correctness:
import java.util.ArrayList;
public class NullRemove {
public static String[] removeNull(String[] a) {
ArrayList<String> aux = new ArrayList<String>();
for (String elem : a) {
if (elem != null) {
aux.add(elem);
}
}
return (String[]) aux.toArray(new String[aux.size()]);
}
public static void main(String[] args) {
String[] init = new String[]{"aaa", null, "bbb", "ccc", null, "ddd",
"eee", "fff", null};
String[] result = NullRemove.removeNull(init);
System.out.println("Start Check result");
for (String elem : result) {
if (elem == null) System.out.println("NULL element");
}
System.out.println("End Check result");
}
}
The for with the code don't print anything cause there is any null element :)
Regards!
You have two options:
Create new array that length is same as the input, then assign to it not null values and add the substract it to the count of not null elememts .
Example in 0xJoKe answer.
If You need to work only sutch array you could create an adapter for it.
public class NullProofIterable<T> implements Iterable<T>{
private final T[] array;
public NullProofIterable(T[] array){
this.array = array;
}
#Override
public Iterator<T> iterator() {
return new NullProofIterator<T>(this.array);
}
private static class NullProofIterator<T> implements Iterator<T> {
private final T[] array;
private final int index = 0;
private NullProofIterator(T[] array) {
this.array = array;
}
#Override
public boolean hasNext() {
return this.index < this.array.length;
}
#Override
public T next() {
return this.array[this.index];
}
#Override
public void remove() {
throw new RuntimeException("Remove not allowed in this iterator");
}
}
}
Then in source code, only thing you have to do is:
for(String str : new NullProofIterable<String>(strArray)) {
//Perform action on not null string
}
The second option is very fancy usage of != null condition bu it might be helful when a method need to return some data.

Interleaving of two strings

I have two strings str1 and str2.
Is there any algorithm that can be used in order to print out all interleavings of the two strings using recursion?
Update:
public class Interleave {
private String resultString[] = new String[10];
private String[] interStr(String str1, String str2){
int n = ((Factorial.factorial(str1.length() + str2.length())) / (Factorial.factorial(str1.length()) * Factorial.factorial(str2.length())));
//n is number of interleavings based on (str1.length()+str2.length())! / (str1.length()! * str2.length()!)
if(str1.length() == 0){
resultString[0] = str2;
return resultString;
}
if(str2.length() == 0){
resultString[0] = str1;
return resultString;
}
else{
for(int i = 0; i < n; i++){
resultString[i]= str1.substring(0, 1) + interStr(str1.substring(1), str2.substring(1));
}
}
return resultString;
}
public static void main(String[] args) {
Interleave obj = new Interleave();
obj.interStr("12", "abc");
for(int i = 0; i < obj.resultString.length; i ++){
System.out.println(obj.resultString[i]);
}
}
}
The question simply asked whether a recursive algorithm exists for the problem, and the answer is yes. To find it, look for the base case and then for the "step".
The base case is when one of the two strings are empty:
interleave(s1, "") = {s1}
interleave("", s2) = {s2}
Notice the order of the arguments doesn't really matter, because
interleave("ab", "12") = {"ab12", "a1b2", "1ab2", "a12b", "1a2b", "12ab"} = interleave("12", "ab")
So since the order doesn't matter we'll look at recursing on the length of the first string.
Okay so let's see how one case leads to the next. I'll just use a concrete example, and you can generalize this to real code.
interleave("", "abc") = {"abc"}
interleave("1", "abc") = {"1abc", "a1bc", "ab1c", "abc1"}
interleave("12", "abc") = {"12abc", "1a2bc", "1ab2c", "1abc2", "a12bc", "a1b2c", "a1bc2", "ab12c", "ab1c2" "abc12"}
So everytime we added a character to the first string, we formed the new result set by adding the new character to all possible positions in the old result set. Let's look at exactly how we formed the third result above from the second. How did each element in the second result turn into elements in the third result when we added the "2"?
"1abc" => "12abc", "1a2bc", "1ab2c", "1abc2"
"a1bc" => "a12bc", "a1b2c", "a1bc2"
"ab1c" => "ab12c", "ab1c2"
"abc1" => "abc12"
Now look at things this way:
"1abc" => {1 w | w = interleave("2", "abc")}
"a1bc" => {a1 w | w = interleave("2", "bc")}
"ab1c" => {ab1 w | w = interleave("2", "c")}
"abc1" => {abc1 w | w = interleave("2", "")}
Although one or two examples doesn't prove a rule in general, in this case you should be able to infer what the rule is. You will have a loop, with recursive calls inside it.
This is actually a little more fun to do with pure functional programming, but you tagged the question with Java.
Hopefully this is a start for you. If you get stuck further you can do a web search for "interleaving strings" or "interleaving lists". There are some solutions out there.
EDIT:
Okay I just wrote the silly thing! It's a lot of fun to write these things in scripting languages, so I thought it would be great to see what it looked like in Java. Not as bad as I thought it would be! Here it is, packaged as an entire Java application.
import java.util.ArrayList;
import java.util.List;
public class Interleaver {
/**
* Returns a list containing all possible interleavings of two strings.
* The order of the characters within the strings is preserved.
*/
public static List<String> interleave(String s, String t) {
List<String> result = new ArrayList<String>();
if (t.isEmpty()) {
result.add(s);
} else if (s.isEmpty()) {
result.add(t);
} else {
for (int i = 0; i <= s.length(); i++) {
char c = t.charAt(0);
String left = s.substring(0, i);
String right = s.substring(i);
for (String u : interleave(right, t.substring(1))) {
result.add(left + c + u);
}
}
}
return result;
}
/**
* Prints some example interleavings to stdout.
*/
public static void main(String[] args) {
System.out.println(interleave("", ""));
System.out.println(interleave("a", ""));
System.out.println(interleave("", "1"));
System.out.println(interleave("a", "1"));
System.out.println(interleave("ab", "1"));
System.out.println(interleave("ab", "12"));
System.out.println(interleave("abc", "12"));
System.out.println(interleave("ab", "1234"));
}
}
If I interpreted your question correctly - that you want all the permutations of all the characters in both strings, then the following code will help. You will need to write your own swap function, and somehow obtain an array of all the characters in both strings.
This algorithm will permute from the i'th element up to the n'th element in the array. It is in C++, I would include a reference to where the algorithm is from but I can't remember.
void getPermutationsR(char characters[], int n, int i)
{
if (i == n)
{
//Output the current permutation
}
else
{
for (int j=i; j<n; j++)
{
swap (characters, i, j);
getPermutationsR(characters, n, i+1);
swap (characters, i, j);
}
}
}
What you have now is a good start. The problem is that it returns just one string, instead a list of those.
Change your function to return a list of string, and then think about how you could combine several lists to produce all the output you want.
Here is a solution using recursive approach, easy to understand too
public class Interleave {
public static List<String> interleave(String first, String second){
if(first.length() == 0){
List<String> list = new ArrayList<String>();
list.add(second);
return list;
}
else if(second.length() == 0){
List<String> list = new ArrayList<String>();
list.add(first);
return list;
}
else{
char c1 = first.charAt(0);
List<String> listA = multiply(c1,interleave(first.substring(1),second));
char c2 = second.charAt(0);
List<String> listB = multiply(c2,interleave(first,second.substring(1)));
listA.addAll(listB);
return listA;
}
}
public static List<String> multiply(char c,List<String> list){
List<String> result = new ArrayList<String>();
for(String str : list){
String res = Character.toString(c) + str;
result.add(res);
}
return result;
}
public static void main(String[] args){
System.out.println(interleave("ab", "1234"));
System.out.println(interleave("a", "b"));
System.out.println(interleave("ab", "cd"));
}
}
Below is the much better and simple to understand solution for this problem:
public class Interleaver {
/**
* Returns a list containing all possible interleavings of two strings.
* The order of the characters within the strings is preserved.
*/
public static String s1 = "abc";
public static String s2 = "12";
public static void interleave(int i, int j, String s) {
if (i == s1.length() && j == s2.length()) {
System.out.println("" + s);
}
if (i != s1.length()) {
interleave(i + 1, j, s + s1.charAt(i));
}
if (j != s2.length()) {
interleave(i, j + 1, s + s2.charAt(j));
}
}//Method ends here
/**
* Prints some example interleavings to stdout.
*/
public static void main(String[] args) {
interleave(0, 0, "");
}//Method ends here
}//Class ends here
Program is using just simple recursive calls to find the solution.
Here is another recursive solution:
public class Interleaving2 {
public static void main(String[] args) {
String x = "ab";
String y = "CD";
int m = x.length();
int n = y.length();
char[] result = new char[m + n + 1];
interleave(x, y, result, m, n, 0);
}
public static void interleave(String x, String y, char[] result, int m, int n, int i) {
if (m == 0 && n == 0) {
System.out.println(String.valueOf(result));
}
if (m != 0) {
result[i] = x.charAt(0);
interleave(x.substring(1), y, result, m - 1, n, i + 1);
}
if (n != 0) {
result[i] = y.charAt(0);
interleave(x, y.substring(1), result, m, n - 1, i + 1);
}
}
}

Categories

Resources