- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Check if strings are rotations of each other or not in Python
Suppose we have two strings s and t, we have to check whether t is a rotation of s or not.
So, if the input is like s = "hello", t = "llohe", then the output will be True.
To solve this, we will follow these steps −
- if size of s is not same as size of t, then
- return False
- temp := s concatenate with s again
- if count of t in temp > 0, then
- return True
- return False
Let us see the following implementation to get better understanding −
Example Code
def solve(s, t): if len(s) != len(t): return False temp = s + s if temp.count(t)> 0: return True return False s = "hello" t = "llohe" print(solve(s, t))
Input
"hello", "llohe"
Output
True
Advertisements