Check if a number ends with another number or not
Last Updated :
08 Jan, 2024
Given two numbers A and B where (A > B), the task is to check if B is a suffix of A or not. Print “Yes” if it is a suffix Else print “No”.
Examples:
Input: A = 12345, B = 45
Output: Yes
Input: A = 12345, B = 123
Output: No
Method 1:
- Convert the given numbers A and B to strings str1 and str2 respectively.
- Traverse both the strings from the end of the strings.
- While traversing the strings, if at any index characters from str1 and str2 are unequal then print “No”.
- Else print “Yes”.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
bool checkSuffix( int A, int B)
{
string s1 = to_string(A);
string s2 = to_string(B);
int n1 = s1.length();
int n2 = s2.length();
if (n1 < n2) {
return false ;
}
for ( int i = 0; i < n2; i++) {
if (s1[n1 - i - 1]
!= s2[n2 - i - 1]) {
return false ;
}
}
return true ;
}
int main()
{
int A = 12345, B = 45;
bool result = checkSuffix(A, B);
if (result) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
class GFG{
public static boolean checkSuffix( int A,
int B)
{
String s1 = String.valueOf(A);
String s2 = String.valueOf(B);
int n1 = s1.length();
int n2 = s2.length();
if (n1 < n2)
{
return false ;
}
for ( int i = 0 ; i < n2; i++)
{
if (s1.charAt(n1 - i - 1 ) !=
s2.charAt(n2 - i - 1 ))
{
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
int A = 12345 , B = 45 ;
boolean result = checkSuffix(A, B);
if (result)
{
System.out.print( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
def checkSuffix(A, B):
s1 = str (A);
s2 = str (B);
n1 = len (s1)
n2 = len (s2)
if (n1 < n2):
return False ;
for i in range (n2):
if (s1[n1 - i - 1 ] ! = s2[n2 - i - 1 ]):
return False ;
return True ;
A = 12345
B = 45 ;
result = checkSuffix(A, B);
if (result):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
public static bool checkSuffix( int A,
int B)
{
string s1 = A.ToString();
string s2 = B.ToString();
int n1 = s1.Length;
int n2 = s2.Length;
if (n1 < n2)
{
return false ;
}
for ( int i = 0; i < n2; i++)
{
if (s1[n1 - i - 1] != s2[n2 - i - 1])
{
return false ;
}
}
return true ;
}
public static void Main( string [] args)
{
int A = 12345, B = 45;
bool result = checkSuffix(A, B);
if (result)
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function checkSuffix( A, B)
{
var s1 = A.toString();
var s2 = B.toString();
var n1 = s1.length;
var n2 = s2.length;
if (n1 < n2)
{
return false ;
}
for ( var i = 0; i < n2; i++)
{
if (s1[n1 - i - 1] != s2[n2 - i - 1])
{
return false ;
}
}
return true ;
}
var A = 12345, B = 45;
var result = checkSuffix(A, B);
if (result)
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Method 2: Using inbuilt function std::boost::algorithm::ends_with() which is included in Boost Library of C++ which is used to check whether any string contains suffix of another string or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <boost/algorithm/string.hpp>
using namespace std;
void checkSuffix( int A, int B)
{
string s1 = to_string(A);
string s2 = to_string(B);
bool result;
result = boost::algorithm::ends_with(s1,
s2);
if (result) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
int main()
{
int A = 12345, B = 45;
checkSuffix(A, B);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void checkSuffix( int A, int B)
{
String s1 = String.valueOf(A);
String s2 = String.valueOf(B);
boolean result;
result = s1.endsWith(s2);
if (result)
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
public static void main(String[] args)
{
int A = 12345 , B = 45 ;
checkSuffix(A, B);
}
}
|
Python3
def checkSuffix(A, B):
s1 = str (A)
s2 = str (B)
result = s1.endswith(s2)
if (result):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
A = 12345
B = 45
checkSuffix(A, B)
|
C#
using System;
class GFG{
static void checkSuffix( int A, int B)
{
String s1 = String.Join( "" , A);
String s2 = String.Join( "" , B);
bool result;
result = s1.EndsWith(s2);
if (result)
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
public static void Main(String[] args)
{
int A = 12345, B = 45;
checkSuffix(A, B);
}
}
|
Javascript
<script>
function checkSuffix( A, B)
{
let s1 = A.toString();
let s2 = B.toString();
let result;
result = s1.endsWith(s2);
if (result)
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
}
let A = 12345, B = 45;
checkSuffix(A, B);
</script>
|
Method 3:
- Subtract B from A.
- Find the number of digits in B(say X) by using the Method 3 discussed in this article.
- Check whether A ends with atleast X number zeros or not. If yes then print “Yes”.
- Else print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkSuffix( int A, int B){
int digit_B = log10 (B) + 1;
A -= B;
return (A % int ( pow (10, digit_B)));
}
int main(){
int A = 12345, B = 45;
bool result = checkSuffix(A, B);
if (!result) cout<< "Yes" ;
else cout<< "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean checkSuffix( int A, int B)
{
int digit_B = ( int ) (Math.log10(B) + 1 );
A -= B;
return (A % ( int )(Math.pow( 10 , digit_B)) > 0 );
}
public static void main(String[] args)
{
int A = 12345 , B = 45 ;
boolean result = checkSuffix(A, B);
if (!result)
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
}
|
Python3
import math
def checkSuffix(A, B):
digit_B = int (math.log10(B)) + 1 ;
A - = B;
return (A % int (math. pow ( 10 , digit_B)));
A = 12345 ; B = 45 ;
result = checkSuffix(A, B);
if (result = = 0 ):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG{
static bool checkSuffix( int A, int B)
{
int digit_B = ( int )(Math.Log10(B) + 1);
A -= B;
return (A % ( int )(Math.Pow(10, digit_B)) > 0);
}
public static void Main()
{
int A = 12345, B = 45;
bool result = checkSuffix(A, B);
if (!result)
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function checkSuffix(A, B)
{
let digit_B = parseInt(Math.log10(B) + 1, 10);
A -= B;
return (A % (Math.pow(10, digit_B)) > 0);
}
let A = 12345, B = 45;
let result = checkSuffix(A, B);
if (!result)
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(logn) because it is using inbuilt pow function
Auxiliary Space: O(1)
Method 4:
1. In this method, we use the modulo operator to check if the last len(str(B)) digits of A are equal to B.
2. We do this by computing A % (10**len(str(B))), which gives us the remainder when A is divided by 10**len(str(B)).
3. If this remainder is equal to B, then A ends with B.
C++
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int A = 12345;
int B = 45;
if (A % static_cast < int >( pow (10, to_string(B).length())) == B) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
|
Java
import java.lang.Math;
public class Main {
public static void main(String[] args)
{
int A = 12345 ;
int B = 45 ;
if (A % ( int )Math.pow(
10 , Integer.toString(B).length()) == B) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
A = 12345
B = 45
if A % ( 10 * * len ( str (B))) = = B:
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class Program {
static void Main( string [] args) {
int A = 12345;
int B = 45;
if (A % ( int )Math.Pow(10, B.ToString().Length) == B) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
}
|
Javascript
const A = 12345;
const B = 45;
if (A % Math.pow(10, B.toString().length) === B) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time Complexity: O(N)
The time complexity of the code is O(N), where N is the length of A (which is equivalent to the number of digits in A). This is because the code performs two operations that take O(N) time in the worst case:
The expression 10**len(str(B)) calculates the value of 10 raised to the power of the number of digits in B. Since B has at most N digits, the length of str(B) is at most N, and the expression takes O(N) time to compute.
The expression A % (10**len(str(B))) calculates the remainder when A is divided by the value computed in step 1. Since the value computed in step 1 is at most 10**N, the division takes O(N) time to compute.
Space Complexity: O(N)
The space complexity of the code is also O(N), because the code uses two variables (A and B) that can store numbers with up to N digits.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...