- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find and Replace Pattern in Python
Suppose we have a list of words and a pattern, and we have to find which words in words matches the pattern. Here a word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the target word. We have to find a list of the words in words that match the given pattern.
So for example, if the input is like ["abc","deq","mee","aqq","dkd","ccc"] and pattern is “abb”, then the output will be [“mee”, “aqq”], here mee and aqq are matching the style of the pattern “abb”. but “ccc” is not a pattern, because this is not a permutation.
To solve this, we will follow these steps −
- define one convert() method. This will take word as input, this will act like −
- counter := 1, s := empty string
- s := s + string equivalent of counter
- for i in range 1 to length of word – 1
- j := i – 1
- while j>=0
- if word[j] is word[i], then break
- decrease j by 1
- if j > -1, then s := s + s[j], otherwise increase counter by 1 and s := s + counter value as string
- return s
- the actual method will be like
- make one array word_num, and this is empty, make another empty array res
- for each element i in words −
- insert convert(i) into word_num
- pattern := convert(pattern)
- for i in range 0 to length of words – 1
- if words_num[i] = pattern, then insert words[i] into res
- return res
Let us see the following implementation to get better understanding −
class Solution(object): def findAndReplacePattern(self, words, pattern): words_num =  result =  for i in words: words_num.append(self.convert(i)) pattern = self.convert(pattern) for i in range(len(words)): if words_num[i] == pattern: result.append(words[i]) return result def convert(self,word): counter = 1 s = "" s+=str(counter) for i in range(1,len(word)): j= i -1 while j>=0: if word[j] == word[i]: break j-=1 if j >-1: s+=s[j] else: counter+=1 s+=str(counter) return s ob = Solution() print(ob.findAndReplacePattern(["abc","deq","mee","aqq","dkd","ccc"],"abb"))
- Search and Replace in Python
- Find And Replace in String in C++
- How to find and replace within a text file using Python?
- Find and replace NumberLong type field in MongoDB?
- How to find and replace text in Oracle?
- Pattern After Double, Reverse, and Swap in Python
- C++ Program to find and replace in a string
- Phyllotaxis pattern in Python?
- How to Search and Replace text in Python?
- Find and replace a part of URL records in MySQL?
- MySQL find/ replace string in fields?
- How can MySQL find and replace the data with REPLACE() function to UPDATE the table?
- Find and Replace text in the entire table using a MySQL?
- Unix filename pattern matching in Python
- Pattern matching in Python with Regex