Check if a number starts with another number or not
Given two numbers A and B where (A > B), the task is to check if B is a prefix of A or not. Print “Yes” if it is a prefix Else print “No”.
Examples:
Input: A = 12345, B = 12
Output: Yes
Input: A = 12345, B = 345
Output: No
Approach:
- Convert the given numbers A and B to strings str1 and str2 respectively.
- Traverse both the strings from the start 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 checkprefix( 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[i]
!= s2[i]) {
return false ;
}
}
return true ;
}
int main()
{
int A = 12345, B = 12;
bool result = checkprefix(A, B);
if (result) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
class GFG{
static boolean checkprefix( 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(i) != s2.charAt( 1 ))
{
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
int A = 12345 , B = 12 ;
boolean result = checkprefix(A, B);
if (!result)
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
}
|
Python3
def checkprefix(A, B):
s1 = str (A)
s2 = str (B)
n1 = len (s1)
n2 = len (s2)
if n1 < n2:
return False
for i in range ( 0 , n2):
if s1[i] ! = s2[i]:
return False
return True
if __name__ = = '__main__' :
A = 12345
B = 12
result = checkprefix(A, B)
if result:
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool checkprefix( 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[i] != s2[i])
{
return false ;
}
}
return true ;
}
static public void Main ()
{
int A = 12345, B = 12;
bool result = checkprefix(A, B);
if (result)
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function checkprefix( 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[i] != s2[i])
{
return false ;
}
}
return true ;
}
var A = 12345, B = 12;
var result = checkprefix(A, B);
if (result)
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(n2), where n2 is the size of string s2
Auxiliary Space: O(1), as no extra space is required
Using in-built function: Using inbuilt function std::boost::algorithm::starts_with(), it can be checked whether any string contains prefix 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 checkprefix( int A, int B)
{
string s1 = to_string(A);
string s2 = to_string(B);
bool result;
result = boost::algorithm::starts_with(s1, s2);
if (result) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
int main()
{
int A = 12345, B = 12;
checkprefix(A, B);
return 0;
}
|
Java
class GFG{
static void checkprefix( int A, int B)
{
String s1 = String.valueOf(A);
String s2 = String.valueOf(B);
boolean result;
result = s1.startsWith(s2);
if (result)
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
public static void main(String[] args)
{
int A = 12345 , B = 12 ;
checkprefix(A, B);
}
}
|
Python3
def checkprefix(A, B):
s1 = str (A)
s2 = str (B)
result = s1.startswith(s2)
if result:
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
A = 12345
B = 12
checkprefix(A, B)
|
C#
using System.Threading;
using System.Globalization;
using System;
class GFG{
static void checkprefix( int A, int B)
{
string s1 = A.ToString();
string s2 = B.ToString();
bool result;
result = s1.StartsWith(s2, false ,
CultureInfo.InvariantCulture);
if (result)
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
static void Main()
{
int A = 12345, B = 12;
checkprefix(A, B);
}
}
|
Javascript
<script>
function checkprefix(A, B) {
var s1 = A.toString();
var s2 = B.toString();
var result;
result = s1.startsWith(s2);
if (result) {
document.write( "Yes" );
} else {
document.write( "No" );
}
}
var A = 12345,
B = 12;
checkprefix(A, B);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
07 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...