0

Hello I can't make this work, I am given a main word followed by another sub words if the word is contained in the main word the part should be deleted.

//Example
//fmrog (in.nextLine)(main word)
//4  (in.nextInt)(the amount of sub words)
//roc(in.nextLine)(not contained)
//gor(in.nextLine)(not contained)
//rog(in.nextLine)(contained)
//ogr(in.nextLine)(not contained)
//result:fm
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    StringBuilder mainWord = new StringBuilder(in.nextLine);
    int n = in.nextInt();
    StringBuilder MainWord2 = new StringBuilder(mainWord);
    in.nextLine();
    for (int i = 0; i < n; i++) {
        String subWord = in.nextLine();
        int chars = subWord.length();
        if (chars> mainWord.length()){
            continue;
        }
        for (int j = 0; j < subWord.length(); j++) {
            int r = 0;

            for (int k = 0; k < mainWord.length(); k++) {
                r++;
                if (k > MainWord2.length() - 1) {
                    break;
                }
                if (MainWord2.charAt(k) == subWord.charAt(j)) {
                    break;
                }
            }
            if (r <= MainWord2.length() && MainWord2.charAt(r-1) == subWord.charAt(j)) {
                MainWord2.deleteCharAt(r - 1);
                if (j >= subWord.length() -1 ) {
                    mainWord = MainWord2;
                    break;
                }
            }
            if (r > MainWord2.length()) {
                MainWord2 = mainWord;
                break;
            }

        }
    }
        System.out.println(mainWord);
    }
}

Honestly I am stucked maybe there is an easier way to solve this. The main thing is that when I write a case like : "super 2 pe surr" At the end at "surr" the two StringBuilders start to act as one when I delete chatAt at one of them the other one changes also

2 Answers2

0

No need to make it so complex.

String input = // complete user input
String[] words = String.split(input);
String mainWord = words[0];
int numWords = Integer.parseInt(words[1]); // this variable isn't needed

for(int i = 2; i < words.length; i++) {
  if (mainWord contains words[i]) {
    mainWord = mainWord.replace(words[i], ""); // remove subword from mainword
  }
}

At the end, mainWord will be the original mainWord without any subwords that were entered later.

the_pwner224
  • 99
  • 1
  • 1
  • 11
  • Hey I did it like this, but it doesnt work if the letters are not one next to the other like "house" i can have "ose". –  Apr 07 '18 at 01:24
  • Hey I did it like this, but it doesnt work if the letters are not one next to the other like "house" i can have "ose". –  Apr 07 '18 at 01:27
  • Didn't see that in your original test case. This is a horribly inefficient way to do it, but you could generate all the permutations of the words to match, and then have a nested loop going through them and check each one for containment. https://stackoverflow.com/q/4240080/1159741 – the_pwner224 Apr 07 '18 at 02:09
0
   public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    in.nextLine();
    String[] words = new String[n];
    for (int i = 0; i <n ; i++) {
        words[i] = in.nextLine();
    }
   String mainWord = words[0];
    for (int i = 1; i <words.length ; i++) {
        if (mainWord.contains(words[i])){
            mainWord = mainWord.replace(words[i], "");
        }
    }
    System.out.println(mainWord);
   }
   }

Here but the thing is if the letters are not one next to another it doesnt remove the subword.