VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-words-matching-pattern-camelcase-notation-dictonary/

⇱ Print all words matching a pattern in PascalCase Notation Dictionary - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print all words matching a pattern in PascalCase Notation Dictionary

Last Updated : 23 Jul, 2025

Given a list of words where each word follows PascalCase notation, the task is to print all words in the list that match the given pattern, where the pattern consists of uppercase characters only.
word matches the pattern if all characters of the pattern appear sequentially in the word’s uppercase letters.

Note: PascalCase is the practice of writing compound words or phrases so that each word or abbreviation begins with a capital letter. Examples: "PowerPoint," "WikiPedia" and "GeeksForGeeks".

Examples:

Input: arr[] = [“WelcomeGeek”, “WelcomeToGeeksForGeeks”, “GeeksForGeeks”, “WayToGo” ], pat = “WTG” 
Output: [“WelcomeToGeeksForGeeks”, “WayToGo”]
Explanation: All uppercase characters of given pattern appears sequentially in the words “WelcomeToGeeksForGeeks” and “WayToGo”.

Input: arr[] = [ “Hi”, “Hello”, “HelloWorld”, “HiTech”, “HiGeek”, “HiTechWorld”, “HiTechCity”, “HiTechLab” ], pat = “HA” 
Output: []
Explanation: None of the words matches the given pattern.

Finding Pattern using Trie - O(n*m) Time and O(n) Space

Below is the idea to solve the problem:

Insert all keys into the Trie one by one. Here key refers to only Uppercase characters in original word in PascalCase notation.

  • When encountering the key for the first time, mark the last node as leaf node and insert the complete word for that key into the container associated with the leaf node. 
  • If encountering a key that is already in the trie, update the container associated with the leaf node with current word. 

After all words are processed, search for the pattern in the trie and print all words that match the pattern.


Output
HiTech
HiTechCity
HiTechLab
HiTechWorld

Using Two Pointer - O(n*m) Time and O(n) Space

Here, we are checking if the current word matches with the pattern or not. If it matches then we store that string. For checking we are using two pointer approach:

  • If the character in 's'matches the current character in 'pattern', then the function moves on to the next character in 'pattern'. 
  • If the character in 's' is uppercase and does not match the current character in 'pattern', then the returns false. Returns true if it has reached the end of 's' and matched every character in 'pattern', and false otherwise.

Output
HiTech
HiTechWorld
HiTechCity
HiTechLab

Related article:

Comment
Article Tags: