Syntax error Program to check one string can be converted to another by removing one element in Python

Program to check one string can be converted to another by removing one element in Python



Suppose we have two strings s and t, we have to check whether we can get t by removing 1 letter from s.

So, if the input is like s = "world", t = "wrld", then the output will be True.

To solve this, we will follow these steps −

  • i:= 0
  • n:= size of s
  • while i < n, do
    • temp:= substring of s[from index 0 to i-1] concatenate substring of s[from index i+1 to end]
    • if temp is same as t, then
      • return True
    • i := i + 1
  • return False

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, s, t):
      i=0
      n=len(s)
      while(i<n):
         temp=s[:i] + s[i+1:]
         if temp == t:
            return True
         i+=1
      return False
ob = Solution()
s = "world"
t = "wrld"
print(ob.solve(s, t))

Input

"world", "wrld"

Output

True
Updated on: 2020-10-07T13:33:00+05:30

173 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements