Python String Rotation Check with Concatenation

Day 30 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to check whether two strings are rotations of each other. The idea is that if one string is a rotation of another, it will appear as a substring of the original string concatenated with itself. What the program does: • Takes two strings as input • Checks if both strings have the same length • Concatenates the first string with itself • Verifies if the second string exists inside the concatenated string • Returns True if they are rotations, otherwise False How the logic works: 1)First, the program checks if both strings have equal length 2)If lengths differ, they cannot be rotations 3)The first string is concatenated with itself: temp = string1 + string1 4)If string2 appears as a substring inside temp, then it is a valid rotation 5)The function returns the result (True or False) Example: String 1:"abcde" String 2:"cdeab" Output: True (because "cdeab" is a rotation of "abcde") Another example: String 1:"abc" String 2:"acb" Output:False Why this approach works well: – Uses string concatenation trick – Avoids complex rotation checks – Time Complexity: O(n) Key learnings from Day 30: – Understanding string rotation logic – Using substring checks effectively – Solving problems with simple mathematical insights – Strengthening string manipulation skills #100DaysOfCode #Day30 #Python #PythonProgramming #StringAlgorithms #ProblemSolving #CodingPractice #DataStructures #Algorithms #InterviewPrep #LearnByDoing #ComputerScience #BTech #CSE #AIandML #VITBhopal #TechJourney

  • text

To view or add a comment, sign in

Explore content categories