-3

For this code kata I need to continue 2 for loops at the same time. How can I do that?

public class StringMerger {

    public static boolean isMerge(String s, String part1, String part2) {
        StringBuilder MergedWord = new StringBuilder("");
        String Whole = part1+part2;
        for(int j = 0; j < Whole.length(); j++){
           for(int I = 0; I < part1.length(); I++){
              if((Character.compare(s.charAt(j), part1.charAt(I)) ==  0) && (j == I)){
                  MergedWord.append(s.charAt(I)+"");
                  continue;
              }
              else
                  break;
            }
            for(int i = 0; i < part2.length(); i++){
                if((Character.compare(s.charAt(j), part2.charAt(i)) == 0) && (j == i)){
                    MergedWord.append(part2.charAt(i) + "");
                    continue;
                }
                else
                    break;
            }
        }
        return s.equals(MergedWord.toString())? true : false;
    }
}

I noticed when one of the for loops continue it only continues the internal loop, but labels would continue the upper loop and would be inefficient. Could I continue 2 for loops at the same time a.k.a continue the inner and upper loop in this nested for loop?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 2
    What do you mean by “at the same time”? Is it you want to continue the `j` loop but preserve the values of the other loop variables? Please give an example of the values of the 3 loop vars when “double continuing” from each loop. – Bohemian Sep 05 '21 at 21:29
  • like j++ and i/I++ – TheGuradian Sep 05 '21 at 21:31
  • If I understand correctly, you want to break the first inner loop and not enter the second one after certain condition is met in the first. The best solution will be to use a flag as the condition for the execution of the second loop. – M. Dudek Sep 05 '21 at 21:31
  • 4
    It's a good idea to summarize the problem in the question itself. Many are not going to go to external links (which can change and go stale over time, rendering the question useless to future visitors) to figure out what you need. – ggorlen Sep 05 '21 at 21:31
  • Maybe this is what you mean: https://www.developer.com/design/understanding-the-java-labeled-statement/ , you can scroll down to 'The continue Statement, with a Label' – Ofek Sep 05 '21 at 21:39
  • Why do you think labels would be inefficient? – David Conrad Sep 05 '21 at 21:54
  • Thanks but also answer the question – TheGuradian Sep 05 '21 at 21:55
  • As I don't think that I will j == i – TheGuradian Sep 05 '21 at 21:55
  • when I continue the loop with j – TheGuradian Sep 05 '21 at 21:55
  • Where can I improve this to remove downvotes? – TheGuradian Sep 09 '21 at 23:54

3 Answers3

1

The task is to match each character in the target string, so you only need one loop, which iterates over the characters in the string. You do need two other loop variables to track how much of the two source strings have been used up.

My solution below is looping over three things at once: the target string and the two 'part' strings. The rate it moves over the 'part' strings isn't constant, but it does progress over them monotonically.

It wasn't clear to me whether the source strings could include extra characters not used in the target string. As the example didn't show any, I assumed not.

public class MergedStringChecker {

    public static void main(String[] args) {
        String target = "codewars";
        String part1 = "cdw";
        String part2 = "oears";

        for (int i = 0, p1 = 0, p2 = 0; i < target.length(); ++i) {
            if (p1 < part1.length() && target.charAt(i) == part1.charAt(p1)) {
                ++p1;
            } else if (p2 < part2.length() && target.charAt(i) == part2.charAt(p2)) {
                ++p2;
            } else {
                throw new RuntimeException("No matching characters at index " + i);
            }
        }
    }
}

The above solution is not Unicode safe if the string contains > 16 bit code points.

tgdavies
  • 10,307
  • 4
  • 35
  • 40
0

Try this.

public static boolean isMerge(String s, String part1, String part2) {
    int length = s.length();
    int length1 = part1.length();
    int length2 = part2.length();
    int i1 = 0, i2 = 0;
    for (int i = 0; i < length; ++i)
        if (i1 < length1 && s.charAt(i) == part1.charAt(i1))
            ++i1;
        else if (i2 < length2 && s.charAt(i) == part2.charAt(i2))
            ++i2;
        else
            return false;
    return i1 == length1 && i2 == length2;
}

public static void main(String[] args) throws IOException {
    System.out.println(isMerge("codewars", "cdw", "oears"));
    System.out.println(isMerge("codewars", "codewars", ""));
    System.out.println(isMerge("codewars", "cdw", "oearsEXTRA"));
}

output:

true
true
false
-1
  • you have an error on the first part loop you must append from the part not the s char
  • you don't need to chek the id of the part with the inital word (remove this check (j == I) and this one (j == i)
  • Use counter to avoid looping many times

Try this code :

  public static boolean isMerge(String s, String part1, String part2) {
    StringBuilder MergedWord = new StringBuilder("");
    String Whole = part1+part2;
    int counter1=0;
    int counter2=0;
    for(int w = 0; w < Whole.length(); w++){
        for(int c1 = counter1; c1 < part1.length(); c1++){
            if((s.charAt(w) == part1.charAt(c1))  ){
                MergedWord.append(part1.charAt(c1)+"");
                counter1++;
                continue;
            }
            else
                break;
        }
        for(int c2 = counter2; c2 < part2.length(); c2++){
            if((s.charAt(w) == part2.charAt(c2)) ){
                MergedWord.append(part2.charAt(c2) + "");
                counter2++;
                continue;
            }
            else
                break;
        }
    }
    return s.equals(MergedWord.toString())? true : false;
}
Chakib Arrama
  • 93
  • 3
  • 12
  • No-explanation "try-this" type answers are perhaps not the best type of answers since they help no one but possibly the original poster and only the original poster. Much better to explain your solution first, and then use code to illustrate what you're explaining. Please read the [answer] link. – Hovercraft Full Of Eels Sep 05 '21 at 22:00
  • 1
    beware the solution of tgdavies will not work with : String target = "codewars"; String part1 = "cdw"; String part2 = "oearsxxxxxxxxx"; – Chakib Arrama Sep 05 '21 at 22:10