IIn today's network security, network intrusion detection systems (NIDS) play an increasingly crucial role in detecting and averting harmful network attacks. This study presents an innovative and efficient string-matching algorithm, called Minimal Deterministic Finite Automata with Optimization of Pattern Matching (MDFAOPM), that has the benefits of high performance, compact memory and Time analysis. The suggested MDFAOPM, whether it is implemented in software or hardware, considerably reduces the memory required without sacrificing high performance by utilizing the magic state properties found from deterministic finite state automata. Additionally, the MDFAOPM algorithm has great flexibility in that it may be adjusted to meet particular resource and performance requirements. The experimental findings demonstrate that MDFAOPM outperforms other systems by more than 21.25% in hardware implementation and 21 times in software implementation compared to Deterministic Finite Automata (DFA).