1

So I was grinding leetcode and came across a solution that looked like this. What does the syntax "search:" and "continue search;" do? I have never seen this syntax before when I write for-loop. Thanks in advance!

public boolean isAlienSorted(String[] words, String order) {
    int[] index = new int[26];
    for (int i = 0; i < order.length(); ++i)
        index[order.charAt(i) - 'a'] = i;

    search: for (int i = 0; i < words.length - 1; ++i) {
        String word1 = words[i];
        String word2 = words[i+1];

        // Find the first difference word1[k] != word2[k].
        for (int k = 0; k < Math.min(word1.length(), word2.length()); ++k) {
            if (word1.charAt(k) != word2.charAt(k)) {
                // If they compare badly, it's not sorted.
                if (index[word1.charAt(k) - 'a'] > index[word2.charAt(k) - 'a'])
                    return false;
                continue search;
            }
        }

        // If we didn't find a first difference, the
        // words are like ("app", "apple").
        if (word1.length() > word2.length())
            return false;
    }
    return true;
         }
Ewan
  • 79
  • 4

2 Answers2

1
  • That's a label (see gotosimilar to C), it's rather unnecessary here for solving the problem.

  • The following solution would simply get through:

class Solution {

    int[] letterMap = new int[26];

    public final boolean isAlienSorted(
        final String[] words,
        final String order
    ) {
        for (int index = 0; index < order.length(); index++) {
            letterMap[order.charAt(index) - 'a'] = index;
        }

        for (int index = 1; index < words.length; index++)
            if (wordIsLarger(words[index - 1], words[index])) {
                return false;
            }

        return true;
    }

    private final boolean wordIsLarger(
        final String a,
        final String b
    ) {
        final int lengthA = a.length();
        final int lengthB = b.length();

        for (int index = 0; index < lengthA && index < lengthB; index++)
            if (a.charAt(index) != b.charAt(index)) {
                return letterMap[a.charAt(index) - 'a'] > letterMap[b.charAt(index) - 'a'];
            }

        return lengthA > lengthB;
    }
}

If we would format it, we can see much easier:

public boolean isAlienSorted(String[] words, String order) {
    int[] index = new int[26];

    for (int i = 0; i < order.length(); ++i) {
        index[order.charAt(i) - 'a'] = i;
    }

    search:

    for (int i = 0; i < words.length - 1; ++i) {
        String word1 = words[i];
        String word2 = words[i + 1];

        // Find the first difference word1[k] != word2[k].
        for (int k = 0; k < Math.min(word1.length(), word2.length()); ++k) {
            if (word1.charAt(k) != word2.charAt(k)) {
                // If they compare badly, it's not sorted.
                if (index[word1.charAt(k) - 'a'] > index[word2.charAt(k) - 'a'])
                { return false; }

                continue search;
            }
        }

        // If we didn't find a first difference, the
        // words are like ("app", "apple").
        if (word1.length() > word2.length()) {
            return false;
        }
    }

    return true;
}
Emma
  • 27,428
  • 11
  • 44
  • 69
1

This is labeled continue. This is mostly used when there are multilevel loops. And we want to continue to outer loop directly from inner loop.

Although it is highly recommended to avoid it.

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47