Q: Melrah Shake - Java Task

+10 votes

You are given a string of random characters, and a pattern of random characters. You need to “shake off” (remove) all of the border occurrences of that pattern, in other words, the very first match and the very last match of the pattern you find in the string.

When you successfully shake off a match, you remove from the pattern the character which corresponds to the index equal to the pattern’s length / 2. Then you continue to shake off the border occurrences of the new pattern until the pattern becomes empty or until there is less than the - needed for shake, matches in the remaining string.

In case you have found at least two matches, and you have successfully shaken them off, you print “Shaked it.” on the console. Otherwise you print “No shake.”, the remains of the main string, and you end the program. See the examples for more info.


  • The input will consist only of two lines.
  • On the first line you will get a string of random characters.
  • On the second line you will receive the pattern and that ends the input sequence.


  • You must print “Shaked it.” for every time you successfully do the melrah shake.
  • If the melrah shake fails, you print “No shake.”, and on the next line you print what has remained of the main string.


  • The two strings may contain ANY ASCII character.
  • Allowed time/memory: 250ms/16MB.


Melrah shake Java task

asked in Java category by user hues
edited by user golearnweb

2 Answers

+4 votes

Here's my solution:

import java.util.Scanner;

public class Melrah {
    public static void main(String[] args) {
        Scanner console = new Scanner(;

        String text = console.nextLine();
        String pattern = console.nextLine();

        while (text.length() > 0 && pattern.length() > 0) {
            int firstIndex = text.indexOf(pattern);
            int lastIndex = text.lastIndexOf(pattern);

            if (firstIndex != -1 && lastIndex != -1 && firstIndex != lastIndex) {
                StringBuilder sb = new StringBuilder();
                sb.append(text.substring(0, firstIndex));
                sb.append(text.substring(firstIndex + pattern.length(), lastIndex));
                sb.append(text.substring(lastIndex + pattern.length(), text.length()));

                text = sb.toString();

                int indexToBeRemoved = pattern.length() / 2;
                pattern = sb.toString();

                System.out.println("Shaked it.");
            } else {
        System.out.println("No shake.");


answered by user eiorgert
+2 votes

... and here's my solution - slightly different:

import java.util.Scanner;

public class Zadacha_01 {
    public static void main(String[] args) {

        Scanner console = new Scanner(;
        String data = console.nextLine();
        String patt = console.nextLine();

        while (true) {
            int fIndex = data.indexOf(patt);
            int lIndex = data.lastIndexOf(patt);
            if (fIndex > -1 && lIndex > -1 && patt.length() > 0) {
                StringBuilder sb = new StringBuilder(data);
                sb.replace(fIndex, patt.length() + fIndex, "");
                sb.replace(lIndex - patt.length(), patt.length() + (lIndex - patt.length()), "");
                data = sb.toString();
                System.out.println("Shaked it.");
                sb = new StringBuilder(patt);
                if (patt.length() > 0) {
                    sb.deleteCharAt(patt.length() / 2);
                    patt = sb.toString();

            } else {
                System.out.println("No shake.");


answered by user richard8502