String matching algorithm - check if a string matches a pattern
There are various algorithms for pattern matching in string.

Exact String matching algorithms


Brute Force
Rabin Karp
Boyer-Moree
KMP
Aho Corasick etc.


Approximate String Matching Algorithm

Approximate String matching is applied in text searching, pattern
recognition and signal processing applications. For a
text T[1..n] and pattern P[1...m], we are supposed to
find all the occurrences of pattern in the text whose edit
distance to the pattern is at most K. The edit distance
between two strings is defined as minimum number of
character insertion, deletion and replacements needed to
make them equal. 

Approximate string matching problem is solved with the
help of dynamic programming.

You can have a look through all these algorithms.
These algorithms will take optimum complexity like O(m),O(n+m) (in case of KMP for example).