Check Order Of Character In String Using OrderedDict() - Python Last Updated : 15 Nov, 2025 Comments Improve Suggest changes 8 Likes Like Report Given an input string and a pattern, check if characters in the input string appear in the same order as in the pattern. Assume the pattern has no duplicate characters. For Examples:Input: s = "engineers rock", pattern = "er"Output: trueExplanation: All 'e' in the input string are before all 'r'.Let's explore different methods to check order of character in string using OrderedDict().Using OrderedDict with Index MappingThis method stores the first occurrence index of each unique character using OrderedDict and then checks whether the pattern’s characters appear in increasing index order within the string. Python from collections import OrderedDict s = 'engineers rock' p = 'er' od = OrderedDict() for i, ch in enumerate(s): if ch not in od: od[ch] = i pos = -1 for ch in p: if ch not in od or od[ch] < pos: print(False) break pos = od[ch] else: print(True) OutputTrue Explanation:od[ch] = i: Stores the first occurrence index of each unique character in OrderedDict.for ch in p: Iterates through pattern characters sequentially.if ch not in od or od[ch] < pos: Checks if the character is missing or out of order.pos = od[ch]: Updates the last matched index.Using OrderedDict.fromkeys()OrderedDict preserves the order of characters while removing duplicates. You can use it to check if the letters of a pattern appear in the correct order in a string. Python from collections import OrderedDict s = 'engineers rock' p = 'er' od = OrderedDict.fromkeys(s) ptr = 0 for k in od: if k == p[ptr]: ptr += 1 if ptr == len(p): print(True) break else: print(False) OutputTrue Explanation:OrderedDict.fromkeys(s): creates an ordered set of unique characters.ptr: tracks the position in the pattern.Loops through keys; if a key matches p[ptr], increment ptr.If all pattern characters are matched, prints True; otherwise prints False.Using Manual Insertion into OrderedDictIn this method, characters are manually inserted into an OrderedDict while keeping their order intact. The pattern is then checked sequentially against the ordered keys to verify if its characters appear in the same order as in the string. Python from collections import OrderedDict s = 'engineers rock' p = 'er' od = OrderedDict() for ch in s: if ch not in od: od[ch] = None ptr = 0 for k in od: if k == p[ptr]: ptr += 1 if ptr == len(p): print(True) break else: print(False) OutputTrue Explanation:if ch not in od: od[ch] = None: Inserts each unique character while preserving order.for k in od: Iterates through ordered unique characters.if k == p[ptr]: ptr += 1: Advances the pattern pointer when characters match.if ptr == len(p): print(True): Confirms all pattern characters appear in correct order.Related Articles:K’th Non-repeating CharacterRemove All Duplicates from a Given String Create Quiz Comment S Shashank Mishra Follow 8 Improve S Shashank Mishra Follow 8 Improve Article Tags : Python python-dict Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like