Open In App

CamelCase Pattern Matching

Last Updated : 03 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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

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.

Approach:

The idea is to use the fact that for each word in the input list, we are only concerned with the uppercase letters of the word. So, we can initialize two pointers, say i and j at the beginning of the current word and pattern respectively. Now, we can have 3 cases:

  1. word[i] is a lowercase character, move to the next character by incrementing i by 1.
  2. word[i] is an uppercase character and word[i] = pat[j], the characters match in both word and pattern, so increment both i and j.
  3. word[i] is an uppercase character and word[i] != pat[j], the characters don't match, so skip the current word.

If all the characters of the pattern matches, that is j reaches the end of the pattern string, then add the word to the result.

Working:


C++
// C++ program for CamelCase Pattern Matching by Matching
// Pattern Characters in Sequence using Two Pointers

#include <iostream>
#include <vector>
using namespace std;

vector<string> camelCase(vector<string> &arr, string &pat) {

    // vector for storing matched words
    vector<string> res;
    for (string word : arr) {

        int i = 0, j = 0;
        while (i < word.length() && j < pat.length()) {

            // If ith character of word is a lowercase
            // character, move to next character
            if (islower(word[i])) {
                i++;
            }

            // If ith character of word is an uppercase
            // character and does not match with the jth
            // character of pattern, move to the next word
            else if (word[i] != pat[j]) {
                break;
            }

            // If ith character of word is an uppercase
            // character and matches with the jth character
            // of pattern move to the next characters
            else {
                i++;
                j++;
            }
        }

        // If all characters of pattern matched, then insert
        // the word in result
        if (j == pat.length())
            res.push_back(word);
    }

    return res;
}

int main() {
    vector<string> arr = {"WelcomeGeek", "WelcomeToGeeksForGeeks",
                          			"GeeksForGeeks", "WayToGo" };
    string pat = "WTG";

    vector<string> res = camelCase(arr, pat);
    for (string s : res)
        cout << s << " ";

    return 0;
}
Java
// Java program for CamelCase Pattern Matching by Matching
// Pattern Characters in Sequence using Two Pointers

import java.util.ArrayList;
import java.util.List;

class GfG {
    static List<String> camelCase(String[] arr, String pat) {
        
        // List for storing matched words
        List<String> res = new ArrayList<>();
        for (String word : arr) {
            int i = 0, j = 0;
            while (i < word.length() && j < pat.length()) {
                
                // If ith character of word is a lowercase 
                // character, move to next character
                if (Character.isLowerCase(word.charAt(i))) {
                    i++;
                } 
                
                // If ith character of word is an uppercase 
                // character and does not match with the jth 
                // character of pattern, move to the next word    
                else if (word.charAt(i) != pat.charAt(j)) {
                    break;
                } 
                
                // If ith character of word is an uppercase 
                // character and matches with the jth character 
                // of pattern move to the next characters
                 else {
                    i++;
                    j++;
                }
            }
          
            // If all characters of pattern matched, then 
            // insert the word in result
            if (j == pat.length())
                res.add(word);
        }
        return res;
    }

    public static void main(String[] args) {
        String[] arr = {"WelcomeGeek", "WelcomeToGeeksForGeeks", 
                        "GeeksForGeeks", "WayToGo" };
        String pat = "WTG";

        List<String> res = camelCase(arr, pat);
        for (String s : res)
            System.out.print(s + " ");
    }
}
Python
# Python program for CamelCase Pattern Matching by Matching
# Pattern Characters in Sequence using Two Pointers

def camelCase(arr, pat):
    
    # List for storing matched words
    res = []
    for word in arr:
        i, j = 0, 0
        while i < len(word) and j < len(pat):
            
            # If ith character of word is a lowercase 
            # character, move to next character
            if word[i].islower():
                i += 1
            
            # If ith character of word is an uppercase 
            # character and does not match with the jth 
            # character of pattern, move to the next word
            elif word[i] != pat[j]:
                break
                
            # If ith character of word is an uppercase 
            # character and matches with the jth character 
            # of pattern move to the next characters
            else:
                i += 1
                j += 1
        
        # If all characters of pattern matched, 
        # then insert the word in result
        if j == len(pat):
            res.append(word)
    return res

if __name__ == '__main__':
    arr = ["WelcomeGeek", "WelcomeToGeeksForGeeks", 
           					"GeeksForGeeks", "WayToGo" ]
    pat = "WTG"
    res = camelCase(arr, pat)
    for s in res:
        print(s, end=' ')
C#
// C# program for CamelCase Pattern Matching by Matching
// Pattern Characters in Sequence using Two Pointers

using System;
using System.Collections.Generic;

class GfG {
    static List<string> CamelCase(string[] arr, string pat) {
        
        // List for storing matched words
        List<string> res = new List<string>();
        foreach (string word in arr) {
            int i = 0, j = 0;
            while (i < word.Length && j < pat.Length) {
                
                // If ith character of word is a lowercase 
                // character, move to next character
                if (char.IsLower(word[i])) {
                    i++;
                } 
              
                // If ith character of word is an uppercase 
                // character and does not match with the jth 
                // character of pattern, move to the next word
                else if (word[i] != pat[j]) {
                    break;
                } 
              
                // If ith character of word is an uppercase 
                // character and matches with the jth character 
                // of pattern move to the next characters
                else {
                    i++;
                    j++;
                }
            }
          
            // If all characters of pattern matched, then 
            // insert the word in result
            if (j == pat.Length)
                res.Add(word);
        }
        return res;
    }

    static void Main(string[] args) {
        string[] arr = {"WelcomeGeek", "WelcomeToGeeksForGeeks", 
                        		"GeeksForGeeks", "WayToGo" };
        string pat = "WTG";

        List<string> res = CamelCase(arr, pat);
        foreach (string s in res)
            Console.Write(s + " ");
    }
}
JavaScript
// JavaScript program for CamelCase Pattern Matching by Matching
// Pattern Characters in Sequence using Two Pointers

function camelCase(arr, pat) {
    
    // Array for storing matched words
    let res = [];
    for (let word of arr) {
        let i = 0, j = 0;
        while (i < word.length && j < pat.length) {
        
            // If ith character of word is a lowercase 
            // character, move to next character
            if (word[i] === word[i].toLowerCase()) {
                i++;
            } 
            
            // If ith character of word is an uppercase 
            // character and does not match with the jth 
            // character of pattern, move to the next word
            else if (word[i] !== pat[j]) {
                break;
            } 
            
            // If ith character of word is an uppercase 
            // character and matches with the jth character 
            // of pattern move to the next characters
            else {
                i++;
                j++;
            }
        }
        
        // If all characters of pattern matched, then 
        // insert the word in result
        if (j === pat.length)
            res.push(word);
    }
    return res;
}

const arr = ["WelcomeGeek", "WelcomeToGeeksForGeeks", 
								"GeeksForGeeks", "WayToGo" ];
const pat = "WTG";

const res = camelCase(arr, pat);
console.log(res.join(' '));

Output
WelcomeToGeeksForGeeks WayToGo 

Time Complexity: O(n*len), where n is size of array and len is max size of word in array.
Auxiliary Space: O(1) 


Next Article

Similar Reads