Topic describes

This is the 1078. Bigram participle on LeetCode. The difficulty is simple.

Tag: “Simulation”

Given the first word first and the second word second, consider the possibility of having the form “first second third” in some text, where second follows first and third follows second.

For each such case, the third word “third” is added to the answer and the answer is returned.

Example 1:

Input: text = "Alice is a good girl she is a good student", first = "a", second = "good" output: ["girl","student"]Copy the code

Example 2:

Input: text = "we will we will rock you", first = "we", second = "will" output: ["we","rock"]Copy the code

Tip:


  • 1 < = t e x t . l e n g t h < = 1000 1 <= text.length <= 1000
  • textThe value consists of lowercase letters and Spaces
  • textAll words in are separated by a single space character

  • 1 < = f i r s t . l e n g t h . s e c o n d . l e n g t h < = 10 1 <= first.length, second.length <= 10
  • first 和 secondConsists of lowercase English letters

simulation

Seems like every time you get up late, it’s an easy question? 🤣

Make a simulation according to the meaning of the questions.

Code:

class Solution {
    public String[] findOcurrences(String text, String a, String b) {
        String[] ss = text.split("");
        int n = ss.length;
        List<String> list = new ArrayList<>();
        for (int i = 0; i + 2 < n; i++) {
            if (ss[i].equals(a) && ss[i + 1].equals(b)) list.add(ss[i + 2]);
        }
        return list.toArray(newString[list.size()]); }}Copy the code
  • Time complexity: O(n)O(n)O(n)
  • Space complexity: O(n)O(n)O(n)

The last

This is the No.1078 of our “Brush through LeetCode” series, which started on 2021/01/01. There are 1916 topics in LeetCode by the start date, some of which have locks.

In this series of articles, in addition to explaining how to solve the problem, I will also present the most concise code possible. If a general solution is involved, the corresponding code template will also be used.

In order to facilitate the students to debug and submit the code on the computer, I set up the relevant warehouse: github.com/SharingSour… .

In the repository, you’ll see links to the series of articles, the corresponding code for the series of articles, the LeetCode source links, and other preferred solutions.