Show problems as
Classic problem #2942EasyArrayString

Find Words Containing Character

Real-world version: Labels Carrying A Given Letter
Tracked in this browser only
Read Find Words Containing Character on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A shelf holds product labels in the order given by labels, where labels[i] is the text printed on the label at position i. You are also given mark, a single lowercase letter.

Find every position whose label has mark somewhere in its text, anywhere at all, not just at the start or the end. A label carrying mark several times still counts as one position.

Return those positions in increasing order. When no label carries mark, return an empty list.

Examples

Example 1

Input
labels = ["mango", "plum", "fig"], mark = "m"
Output
[0, 1]

"mango" opens with the letter and "plum" carries it as its third character, so positions 0 and 1 qualify. "fig" has no "m".

Example 2

Input
labels = ["pear", "plumb", "kale"], mark = "b"
Output
[1]

Only "plumb" carries a "b", and it is the last character of that label, so position 1 is the single answer.

Example 3

Input
labels = ["fig", "kiwi"], mark = "q"
Output
[]

Neither label contains a "q", so the answer is empty.

Constraints

  • 1 <= labels.length <= 50
  • 1 <= labels[i].length <= 50
  • mark is a lowercase English letter.
  • labels[i] consists only of lowercase English letters.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def labels_with_letter(labels: list[str], mark: str) -> list[int]:
Java
public List<Integer> labelsWithLetter(String[] labels, char mark)