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
  • return True

Let us see the following implementation to get better understanding −

Example

 Live Demo

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

Updated on: 29-Dec-2020

292 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements