VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-find-string-start-end-geeks/

⇱ Find if a string starts and ends with another given string - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find if a string starts and ends with another given string

Last Updated : 20 Feb, 2023

Given a string str and a corner string cs, we need to find out whether the string str starts and ends with the corner string cs or not.
Examples: 

Input : str = "geeksmanishgeeks", cs = "geeks"
Output : Yes

Input : str = "shreya dhatwalia", cs = "abc"
Output : No

Algorithm 

  • Find length of given string str as well as corner string cs. Let this length be n and cl respectively.
  • If cl>n, return false as cs can't be greater than str.
  • Otherwise, find the prefix and suffix of length cl from str. If both prefix and suffix match with corner string cs, return true otherwise return false.

Implementation:


Output
Yes

Time Complexity: O(n)
Auxiliary Space: O(1) 

Comment
Article Tags: