Question

I was given this problem to solve. I have only the slightest idea on how it should be implemented, and I'm all too new with programming and stuffs, and would love to hear your comments on this.

Say given a string in the form "abc1234defgh567jk89", and I must create a new string "a1b2c3d5e6f7j8k9".

Note that there are corresponding [digits] & [characters] group and since there may be more of one type over the other, the output has only matching sequence and ignore extra digits or characters in this case '4' & 'g' & 'h'.

I know I will have to use 2 sets of queues to store both types of elements, but I do not know how else to proceed from here.

Would appreciate if you could share a pseudocode or a Java(prefably) version, since I am learning thru this language now.

Thank you.

Was it helpful?

Solution

Pseudocode:

Queue letterQueue;
Queue numberQueue;

for (every character in the string) {
    if (it's a letter) {
        if (numberQueue is not empty) {
            add the letters alternating into the buffer (stringbuilder), and purge buffers
        }
        add newest letter to letterqueue
    }
    if (it's a number) {
        add newest letter to numberqueue
    }
}
add any remaining unprocessed letters to the queue (this will happen most of the time)

return contents of string buffer

You will need:

Code:

import java.util.LinkedList;
import java.util.Queue;

public class StringTest {
    private static String str ="abc1234defgh567jk89";
    
    private static String reorganize(String str) {
        Queue<Character> letterQueue = new LinkedList<>();
        Queue<Character> numberQueue = new LinkedList<>();
            
        StringBuilder s = new StringBuilder();
        
        for (char c : str.toCharArray()) {
            if(Character.isLetter(c)) {
                if (!numberQueue.isEmpty()) processQueues(letterQueue, numberQueue, s);
                letterQueue.offer(c);
            } else if(Character.isDigit(c)) {
                numberQueue.offer(c);
            }
        }
    
        processQueues(letterQueue, numberQueue, s);

        return s.toString();
    }
    
    private static void processQueues(Queue<Character> letterQueue, Queue<Character> numberQueue, StringBuilder s) {
        while(!letterQueue.isEmpty() && !numberQueue.isEmpty()) {
            s.append(letterQueue.poll());
            s.append(numberQueue.poll());
        }
        letterQueue.clear();
        numberQueue.clear();
    }
    
    public static void main(String... args) {
        System.out.println(reorganize(str));
    }
}

OTHER TIPS

See this hint:

String str = "abc1234defgh567jk89";
String c = str.replaceAll("\\d", "");  // to store characters
String d = str.replaceAll("\\D", ""); // to store digits

Try this:

public static void main(String[] args) {
    String str = "abc1234defgh567jk89";
    String c = str.replaceAll("\\d", "");
    String d = str.replaceAll("\\D", "");
    String result = "";
    int j = 0, k = 0;
    int max = Math.max(c.length(), d.length());
    for (int i = 0; i < max; i++) {
        if (j++ < c.length())
            result = result + c.charAt(i);
        if (k++ < d.length())
            result = result + d.charAt(i);
    }
    System.out.println(result);
}

Output:

a1b2c3d4e5f6g7h8j9k
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top