- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to check if we reverse sublist of one list to form second list or not in Python
Suppose we have two lists of numbers called A, and B. We have to take some sublist in A and reverse it. Then check whether it is possible to turn A into B or not. We can take sublist and reverse it any number of times.
So, if the input is like A = [2, 3, 4, 9, 10], B = [4, 3, 2, 10, 9], then the output will be True as we can reverse [2,3,4] and [9,10].
To solve this, we will follow these steps −
- res := a map, initially empty
- for each n in nums, do
- res[n] := res[n] + 1
- for each t in target, do
- res[t] := res[t] - 1
- return true when all elements in the values of res is same as 0.
Let us see the following implementation to get better understanding −
Example
from collections import defaultdict class Solution: def solve(self, nums, target): res = defaultdict(int) for n in nums: res[n] += 1 for t in target: res[t] -= 1 return all(n == 0 for n in res.values()) ob = Solution() A = [2, 3, 4, 9, 10] B = [4, 3, 2, 10, 9] print(ob.solve(A, B))
Input
[2, 3, 4, 9, 10], [4, 3, 2, 10, 9]
Output
True
- Related Articles
- Program to check whether list of points form a straight line or not in Python
- Python program to sort a list according to the second element in sublist
- Python program to sort a list according to the second element in the sublist.
- Program to check if the given list has Pythagorean Triplets or not in Python
- Program to check every sublist in a list containing at least one unique element in Python
- Check if list is sorted or not in Python
- Program to check whether we can split list into consecutive increasing sublists or not in Python
- Python program to check whether a list is empty or not?
- Program to check we can form array from pieces or not in Python
- Program to check we can spell out the target by a list of words or not in Python
- Program to convert one list identical to other with sublist sum operation in Python
- Program to check linked list items are forming palindrome or not in Python
- Program to reverse a list by list slicing in Python
- Program to check whether given list is in valid state or not in Python
- Program to check whether list is alternating increase and decrease or not in Python

Advertisements