Given two strings s1 and s2, check if s1 is a suffix of s2. Or in simple words, we need to find whether string s2 ends with string s1.
Input : s1 = "Beginner" and s2 = "w3wiki"
Output : Yes
Input : s1 = "world", s2 = "my first code is hello world"
Output : Yes
Input : s1 = "Beginner" and s2 = "BeginnerforGeek"
Output : No
Method 1 (Writing our own code):
C++
#include <bits/stdc++.h>
using namespace std;
bool isSuffix(string s1, string s2)
{
int n1 = s1.length(), n2 = s2.length();
if (n1 > n2)
return false ;
for ( int i = 0; i < n1; i++)
if (s1[n1 - i - 1] != s2[n2 - i - 1])
return false ;
return true ;
}
int main()
{
string s1 = "Beginner" , s2 = "w3wiki" ;
bool result = isSuffix(s1, s2);
if (result)
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
C
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
bool isSuffix( char s1[], char s2[])
{
int n1 = strlen (s1), n2 = strlen (s2);
if (n1 > n2)
return false ;
for ( int i = 0; i < n1; i++)
if (s1[n1 - i - 1] != s2[n2 - i - 1])
return false ;
return true ;
}
int main()
{
char s1[] = "Beginner" , s2[] = "w3wiki" ;
bool result = isSuffix(s1, s2);
if (result)
printf ( "Yes" );
else
printf ( "No" );
return 0;
}
|
Java
class GFG
{
static boolean isSuffix(String s1, String s2)
{
int n1 = s1.length(), n2 = s2.length();
if (n1 > n2)
return false ;
for ( int i= 0 ; i<n1; i++)
if (s1.charAt(n1 - i - 1 ) != s2.charAt(n2 - i - 1 ))
return false ;
return true ;
}
public static void main(String []args)
{
String s1 = "Beginner" , s2 = "w3wiki" ;
boolean result = isSuffix(s1, s2);
if (result)
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isSuffix(s1, s2):
n1 = len (s1)
n2 = len (s2)
if (n1 > n2):
return False
for i in range (n1):
if (s1[n1 - i - 1 ] ! = s2[n2 - i - 1 ]):
return False
return True
if __name__ = = "__main__" :
s1 = "Beginner"
s2 = "w3wiki"
result = isSuffix(s1, s2)
if (result):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isSuffix( string s1, string s2)
{
int n1 = s1.Length, n2 = s2.Length;
if (n1 > n2)
return false ;
for ( int i=0; i<n1; i++)
if (s1[n1 - i - 1] != s2[n2 - i - 1])
return false ;
return true ;
}
public static void Main()
{
string s1 = "Beginner" , s2 = "w3wiki" ;
bool result = isSuffix(s1, s2);
if (result)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function isSuffix( $s1 , $s2 )
{
$n1 = ( $s1 );
$n2 = strlen ( $s2 );
if ( $n1 > $n2 )
return false;
for ( $i = 0; $i < $n1 ; $i ++)
if ( $s1 [ $n1 - $i - 1] != $s2 [ $n2 - $i - 1])
return false;
return true;
}
$s1 = "Beginner" ;
$s2 = "w3wiki" ;
$result = isSuffix( $s1 , $s2 );
if ( $result )
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function isSuffix(s1, s2)
{
let n1 = s1.length, n2 = s2.length;
if (n1 > n2)
return false ;
for (let i = 0; i < n1; i++)
if (s1[n1 - i - 1] != s2[n2 - i - 1])
return false ;
return true ;
}
let s1 = "Beginner" , s2 = "w3wiki" ;
let result = isSuffix(s1, s2);
if (result)
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n), where n is the size of the given string s1.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Method 2 (Using boost library in C++):
Since std::string class does not provide any endWith() function in which a string ends with another string so we will be using Boost Library. Make sure to include #include boost/algorithm/string.hpp and #include string to run the code fine.
C++
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "Beginner" , s2 = "w3wiki" ;
bool result = boost::algorithm::ends_with(s2, s1);
if (result)
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
public static void main(String[] args)
{
String s1 = "Beginner" , s2 = "w3wiki" ;
boolean result = s2.endsWith(s1);
if (result)
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
if __name__ = = '__main__' :
s1 = "Beginner" ;
s2 = "w3wiki" ;
result = s2.endswith(s1);
if (result):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
public static void Main(String[] args)
{
String s1 = "Beginner" , s2 = "w3wiki" ;
bool result = s2.EndsWith(s1);
if (result)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
let s1 = "Beginner" , s2 = "w3wiki" ;
let result = s2.endsWith(s1);
if (result)
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n), where n is the size of the given string s1.
Auxiliary Space: O(1)
Contact Us