
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 a String Is Suffix of Another in Python
Suppose we have two strings s and t. We have to check whether s is suffix of t or not.
So, if the input is like s = "ate" t = "unfortunate", then the output will be True.
To solve this, we will follow these steps −
- s_len := size of s
- t_len := size of t
- if s_len > t_len, then
- return False
- for i in range 0 to s_len, do
- if s[s_len - i - 1] is not same as t[t_len - i - 1], then
- return False
- if s[s_len - i - 1] is not same as t[t_len - i - 1], then
- return True
Let us see the following implementation to get better understanding −
Example
def solve(s, t): s_len = len(s) t_len = len(t) if (s_len > t_len): return False for i in range(s_len): if(s[s_len - i - 1] != t[t_len - i - 1]): return False return True s = "ate" t = "unfortunate" print(solve(s, t))
Input
"ate", "unfortunate"
Output
True
Advertisements