- 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 one string can be converted to other by shifting characters clockwise in Python
Suppose we have two strings p and q, and also have a number r, we have to check whether p can be converted to q by shifting some characters clockwise at most r times. So, as an example, "c" can be turned into "e" using 2 clockwise shifts.
So, if the input is like p = "abc", q = "ccc", r = 3, then the output will be True, as we can make "a" into "c" by using 2 clockwise shifts and then convert "b" into "c" by using 1 clockwise shift, for a total of 3 shifts.
To solve this, we will follow these steps −
- if size of a is not same as size of b, then
- return False
- if k is same as 0 and a is not same as b, then
- return False
- su:= 0
- for i in range 0 to size of a, do
- v := ASCII of b[i] - ASCII of a[i]
- if v>=0, then
- su := su + v
- otherwise,
- su := su + v + 26
- if su > k, then
- return False
- return True
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, a, b, k): if len(a) != len(b): return False if k == 0 and a != b: return False su=0 for i in range(len(a)): v = ord(b[i])- ord(a[i]) if v>=0: su+=v else: su+=v+26 if su>k: return False return True ob = Solution() print(ob.solve("abc", "ccc", 3))
Input
"abc", "ccc", 3
Output
True
- Related Articles
- Program to check one string can be converted to another by removing one element in Python
- Check if characters of one string can be swapped to form other in Python
- Program to check whether one point can be converted to another or not in Python
- Check if a string can be converted to another string by replacing vowels and consonants in Python
- How to check if a string can be converted to float in Python?
- Program to check two strings can be equal by swapping characters or not in Python
- Program to check whether one string can be 1-to-1 mapped into another string in Python
- Program to get final string after shifting characters with given number of positions in Python
- Program to check whether final string can be formed using other two strings or not in Python
- Check if matrix can be converted to another matrix by transposing square sub-matrices in Python
- Can one string be repeated to form other in JavaScript
- Check whether second string can be formed from characters of first string in Python
- Check if characters of a given string can be rearranged to form a palindrome in Python
- Python program to check if a string contains all unique characters
- Check if frequency of all characters can become same by one removal in Python

Advertisements