इस समस्या में, हमें कैमलकेस में स्ट्रिंग की एक सरणी और एक पैटर्न दिया गया है। हमें सरणी के उन सभी स्ट्रिंग को प्रिंट करना है जो दिए गए पैटर्न से मेल खाते हैं।
स्ट्रिंग की सरणी एक सरणी है जिसमें तत्व स्ट्रिंग डेटा प्रकार के हैं।
कैमलकेस प्रोग्रामिंग में नामकरण की एक सामान्य विधि है, इसमें नए शब्द का पहला अक्षर अपरकेस से शुरू होता है, बाकी सभी लोअरकेस होते हैं।
उदाहरण - iLoveProgramming
समस्या - दिए गए पैटर्न से मेल खाने वाले सभी स्ट्रिंग ढूंढें।
उदाहरण -
Input : "TutorialsPoint" , "ProgrammersPoint" , "ProgrammingLover" , "Tutorials". Pattern : 'P' Output : "TutorialsPoint" , "ProgrammersPoint" , "ProgrammingLover"
व्याख्या - हमने उन सभी स्ट्रिंग पर विचार किया है जिनमें 'P' है।
इस समस्या को हल करने के लिए, हम डिक्शनरी की सभी कुंजियों को एक ट्री में जोड़ेंगे और फिर अपरकेस वर्णों का उपयोग करेंगे। और ट्री में शब्दों को प्रोसेस करके उन सभी को प्रिंट करेंगे जो पैटर्न से मेल खाते हैं।
उदाहरण
#include <bits/stdc++.h> using namespace std; struct TreeNode{ TreeNode* children[26]; bool isLeaf; list<string> word; }; TreeNode* getNewTreeNode(void){ TreeNode* pNode = new TreeNode; if (pNode){ pNode->isLeaf = false; for (int i = 0; i < 26; i++) pNode->children[i] = NULL; } return pNode; } void insert(TreeNode* root, string word){ int index; TreeNode* pCrawl = root; for (int level = 0; level < word.length(); level++){ if (islower(word[level])) continue; index = int(word[level]) - 'A'; if (!pCrawl->children[index]) pCrawl->children[index] = getNewTreeNode(); pCrawl = pCrawl->children[index]; } pCrawl->isLeaf = true; (pCrawl->word).push_back(word); } void printAllWords(TreeNode* root){ if (root->isLeaf){ for(string str : root->word) cout << str << endl; } for (int i = 0; i < 26; i++){ TreeNode* child = root->children[i]; if (child) printAllWords(child); } } bool search(TreeNode* root, string pattern){ int index; TreeNode* pCrawl = root; for (int level = 0; level <pattern.length(); level++) { index = int(pattern[level]) - 'A'; if (!pCrawl->children[index]) return false; pCrawl = pCrawl->children[index]; } printAllWords(pCrawl); return true; } void findAllMatch(vector<string> dictionary, string pattern){ TreeNode* root = getNewTreeNode(); for (string word : dictionary) insert(root, word); if (!search(root, pattern)) cout << "No match found"; } int main(){ vector<string> dictionary = { "Tutorial" , "TP" , "TutorialsPoint" , "LearnersPoint", "TutorialsPointsPrograming" , "programmingTutorial"}; string pattern = "TP"; findAllMatch(dictionary, pattern); return 0; } आउटपुट
TP TutorialsPoint TutorialsPointsPrograming