Python Counter and Dictionary Intersection Example (Make a string using deletion and rearrangement) Last Updated : 31 Oct, 2025 Comments Improve Suggest changes 9 Likes Like Report Given two strings, check if it is possible to make the first string from the second by deleting some characters from the second string and rearranging the remaining characters.Examples:Input : s1 = BOBthebuilder : s2 = fBoOkBIHnfndBthesibuishliderOutput : PossibleInput : s1 = Hello : s2 = dnaKfhelddfOutput : Not PossibleApproach:1. Count each character in both stringsWe use Python’s Counter from the collections module.Counter(str1) gives a dictionary-like object showing how many times each character appears in str1.Similarly, Counter(str2) counts characters in str2.2. Compare character countsFor each character in str1, we check if str2 has that character in equal or greater quantity.Example:If str1 needs 2 'A's, then str2 must have at least 2 'A's.If even one character from str1 is missing or appears fewer times in str2, then the answer is “Not Possible.”3. Decide the resultIf all characters in str1 are found in sufficient quantity in str2, print “Possible.”Otherwise, print “Not Possible.”Implementation: Python from collections import Counter s1 = 'BOBthebuilder' s2 = 'fBoOkBIHnfndBthesibuishlider' # Count characters in both strings count1 = Counter(s1) count2 = Counter(s2) # Check if all characters of str1 are present in str2 possible = True for ch in count1: if count1[ch] > count2[ch]: possible = False break if possible: print("Possible") else: print("Not Possible") OutputPossible Explanation:We use Counter to count how many times each character appears.Then we check, one by one, if str2 has enough of each character.If any character is missing or fewer, it prints “Not Possible”.Otherwise, it prints “Possible”.Related Articles:Operations on Python CounterPython Collections Counter Create Quiz Comment S Shashank Mishra Follow 9 Improve S Shashank Mishra Follow 9 Improve Article Tags : Python python-dict python-string Python dictionary-programs 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