Python Counter and Dictionary Intersection Example (Make a string using deletion and rearrangement) (original) (raw)

Last Updated : 31 Oct, 2025

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 = fBoOkBIHnfndBthesibuishlider
Output : Possible

Input : s1 = Hello
: s2 = dnaKfhelddf
Output : Not Possible

**Approach:

**1. Count each character in both strings

**2. Compare character counts

**3. Decide the result

**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")

`

**Explanation: