'Java (move point in string)

I have a string = "abc"; And also I have a point ".". How I can move this point "." in that string("abc"). Example : Input date = "abc". Output date = "abc", "a.bc", "ab.c", "a.b.c".

Thanks'.

public class MovePoint {
    public static void main(String[] args) {
        String str = "abcd";
        String str1 = ".";
        String[] ara = new String[str.length()];

        for (int i = 0; i < str.length(); i++) {
            ara[i] = str.substring(i, 1) + str1 + str.substring(1, 2);
            System.out.print(Arrays.toString(ara));

            }
        }
}


Solution 1:[1]

Here is one way to do it. This uses StringBuilder as well as a plain char array to avoid having another loop over the array to build the last String, but it therefore consumes more memory.

First I print the first desired output, which is just the unmodified input String. Then I create a StringBuilder which can hold all chars from the input + one more for the chosen separator to avoid unnecessary array resizing. Then I initialize the StringBuilder so that it is in the form of the second desired ouput [char, sep, char, ...]. I am using StringBuilder here because it is just more convenient as it has the append() function that I need here.

Last but not least I also initialize a char array which will hold the values for the last String to avoid having to iterate over the array twice to generate that.

Now I loop over over the StringBuilder starting from one (as its already initialize to the first result with separator) to the last character. In this loop I do three things.

  1. Print out the current value of StringBuilder
  2. Swap the separator with the next character in the StringBuilder
  3. Put the character and separator to the correct position in the char array as required for the last string

After the loop the last desired output is computed and I just have to print it to the console.

Runtime for this in BigO-notation would be O(n).

public static void main(String[] args) {
    String str = "abcd";
    char sep = '.';
    movePoint(str, sep);
}

public static void movePoint(String str, char sep){
    // print first desired output
    System.out.println(str);
    // String builder that can hold str.length + 1 characters, so no unnecessary resizing happens
    var sb = new StringBuilder(str.length() + 1); 
    // fill with first char
    sb.append(str.charAt(0));
    // add separator
    sb.append(sep);
    // add rest of the string
    sb.append(str.substring(1));
    // Array that holds the last string
    var lastStr = new char[str.length() + str.length() - 1];
    
    for (int i = 1; i < sb.capacity() - 1; i++) {
        System.out.println(sb);
        // build current string
        // swap separator with next character
        var temp = sb.charAt(i);
        sb.setCharAt(i, sb.charAt(i+1));
        sb.setCharAt(i+1, temp);
        // manipulate char array so last string is built correctly
        int doubled = i << 1;
        // set character at correct position
        lastStr[doubled - 2] = sb.charAt(i-1);
        // set separator at correct position
        lastStr[doubled - 1] = sep;
    }
    // add last character of string to this char array
    lastStr[lastStr.length - 1] = sb.charAt(sb.length() - 2);
    // print last desired output
    System.out.println(lastStr);
}

Expected output:

abcd
a.bcd
ab.cd
abc.d
a.b.c.d

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1