0

I was doing this leetcode problem, and this was the solution in Java they gave for this problem:

class Solution {
public List<String> wordSubsets(String[] A, String[] B) {
    int[] bmax = count("");
    for (String b: B) {
        int[] bCount = count(b);
        for (int i = 0; i < 26; ++i)
            bmax[i] = Math.max(bmax[i], bCount[i]);
    }

    List<String> ans = new ArrayList();
    search: for (String a: A) {
        int[] aCount = count(a);
        for (int i = 0; i < 26; ++i)
            if (aCount[i] < bmax[i])
                continue search;
        ans.add(a);
    }

    return ans;
}

public int[] count(String S) {
    int[] ans = new int[26];
    for (char c: S.toCharArray())
        ans[c - 'a']++;
    return ans;
}

}

In the second for loop after the "ans" List is instantiated, you can see that there is a term used called "search" followed by a colon. In addition, it is also used inside the second for loop which is contained by the first, with "continue" before it. Can someone explain this term to me and how it works?

dphil1
  • 113
  • 4
  • 2
    `search: ` could have been named anything, it is simply a tag used to identify where the `continue search` goes to. Note, for most situations it is seen as bad practice and should be avoided. – Nexevis Aug 11 '21 at 18:16
  • It’s a label. It’s used with continue and break. – Nathan Hughes Aug 11 '21 at 18:18
  • 1
    This is a [labeled continue statement](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html). – Olivier Aug 11 '21 at 18:20

1 Answers1

0

This search: is a for loop alias, mostly used when you have nested loops. It is not a reserved word, you can call it whatever you want, like loop1:, loop2:. Take a look here

tomrlh
  • 1,006
  • 1
  • 18
  • 39