Find Words Containing Character
Real-world version: Labels Carrying A Given LetterThe 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.
def labels_with_letter(labels: list[str], mark: str) -> list[int]:public List<Integer> labelsWithLetter(String[] labels, char mark)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Longest Common PrefixReal-world version: Shared Crate CodeEasy
- Shortest Word DistanceReal-world version: Closest Station Calls in the Announcement LogEasy
- Verifying an Alien DictionaryReal-world version: Catalogue Order Under a Custom Glyph SequenceEasy
- Find Common CharactersReal-world version: Tiles Every Rack Can SupplyEasy