Check if a number starts with another number or not

23 Jun, 2022
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”.
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++
Java
Python3
C#
Javascript
// C++ program for the above approach#include "bits/stdc++.h"using namespace std; // Function to check if B is a// prefix of A or notbool checkprefix(int A, int B){     // Convert numbers into strings    string s1 = to_string(A);    string s2 = to_string(B);     // Find the lengths of strings    // s1 and s2    int n1 = s1.length();    int n2 = s2.length();     // Base Case    if (n1 < n2) {        return false;    }     // Traverse the strings s1 & s2    for (int i = 0; i < n2; i++) {         // If at any index characters        // are unequals then return false        if (s1[i]            != s2[i]) {            return false;        }    }     // Return true    return true;} // Driver Codeint main(){    // Given numbers    int A = 12345, B = 12;     // Function Call    bool result = checkprefix(A, B);     // If B is a prefix of A, then    // print "Yes"    if (result) {        cout << "Yes";    }    else {        cout << "No";    }    return 0;}
// Java program for the above approachclass GFG{ // Function to check if B is a// prefix of A or notstatic boolean checkprefix(int A, int B){         // Convert numbers into Strings    String s1 = String.valueOf(A);    String s2 = String.valueOf(B);     // Find the lengths of Strings    // s1 and s2    int n1 = s1.length();    int n2 = s2.length();     // Base Case    if (n1 < n2)    {        return false;    }     // Traverse the Strings s1 & s2    for(int i = 0; i < n2; i++)    {                 // If at any index characters        // are unequals then return false        if (s1.charAt(i) != s2.charAt(1))        {            return false;        }    }     // Return true    return true;} // Driver Codepublic static void main(String[] args){         // Given numbers    int A = 12345, B = 12;     // Function call    boolean result = checkprefix(A, B);     // If B is a prefix of A, then    // print "Yes"    if (!result)    {        System.out.print("Yes");    }    else    {        System.out.print("No");    }}} // This code is contributed by amal kumar choubey
# Python3 program for the# above approach # Function to check if B is# a prefix of A or notdef checkprefix(A, B):         # Convert numbers into strings    s1 = str(A)    s2 = str(B)         # Find the length of s1 and s2    n1 = len(s1)    n2 = len(s2)         # Base case    if n1 < n2:        return False             # Traverse the string s1 and s2    for i in range(0, n2):                 # If at any index characters        # are unequal then return False        if s1[i] != s2[i]:            return False                 return True # Driver codeif __name__=='__main__':         # Given numbers    A = 12345    B = 12         # Function call    result = checkprefix(A, B)         # If B is a prefix of A ,    # then print Yes    if result:        print("Yes")    else:        print("No")         # This code is contributed by virusbuddah_
// C# program for the above approachusing System; class GFG{     // Function to check if B is a// prefix of A or notstatic bool checkprefix(int A, int B){     // Convert numbers into Strings    String s1 = A.ToString();    String s2 = B.ToString();     // Find the lengths of Strings    // s1 and s2    int n1 = s1.Length;    int n2 = s2.Length;     // Base Case    if (n1 < n2)    {        return false;    }     // Traverse the Strings s1 & s2    for(int i = 0; i < n2; i++)    {         // If at any index characters        // are unequals then return false        if (s1[i] != s2[i])        {            return false;        }    }     // Return true    return true;} // Driver Codestatic public void Main (){         // Given numbers    int A = 12345, B = 12;     // Function call    bool result = checkprefix(A, B);     // If B is a prefix of A, then    // print "Yes"    if (result)    {        Console.Write("Yes");    }    else    {        Console.Write("No");    }}} // This code is contributed by shubhamsingh10
<script> // javascript program for the above approach    // Function to check if B is a// prefix of A or not function checkprefix( A,  B){       // Convert numbers into Strings         var s1 = A.toString();    var s2 = B.toString();       // Find the lengths of Strings    // s1 and s2         var n1 = s1.length;    var n2 = s2.length;       // Base Case    if (n1 < n2)    {        return false;    }       // Traverse the Strings s1 & s2    for(var i = 0; i < n2; i++)    {           // If at any index characters        // are unequals then return false        if (s1[i] != s2[i])        {            return false;        }    }       // Return true    return true;}   // Driver Code            // Given numbers    var A = 12345, B = 12;       // Function call    var result = checkprefix(A, B);       // If B is a prefix of A, then    // print "Yes"    if (result)    {        document.write("Yes");    }    else    {        document.write("No");    }  </script>   
Yes
Time Complexity: O(n2), where n2 is the size of string s2Auxiliary 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++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>#include <boost/algorithm/string.hpp>using namespace std; // Function to check if B is a// prefix of A or notvoid checkprefix(int A, int B){     // Convert numbers into strings    string s1 = to_string(A);    string s2 = to_string(B);     bool result;     // Check if s2 is a prefix of s1    // or not using starts_with() function    result = boost::algorithm::starts_with(s1, s2);     // If result is true, print "Yes"    if (result) {        cout << "Yes";    }    else {        cout << "No";    }} // Driver Codeint main(){    // Given numbers    int A = 12345, B = 12;     // Function Call    checkprefix(A, B);    return 0;}
// Java program for the above approachclass GFG{ // Function to check if B is a// prefix of A or notstatic void checkprefix(int A, int B){     // Convert numbers into Strings    String s1 = String.valueOf(A);    String s2 = String.valueOf(B);     boolean result;     // Check if s2 is a prefix of s1    // or not using starts_with() function    result = s1.startsWith(s2);     // If result is true, print "Yes"    if (result)    {        System.out.print("Yes");    }    else    {        System.out.print("No");    }} // Driver Codepublic static void main(String[] args){         // Given numbers    int A = 12345, B = 12;     // Function call    checkprefix(A, B);}} // This code is contributed by amal kumar choubey
# Python3 program for the# above approach # Function to check if B is# a prefix of A or notdef checkprefix(A, B):         # Convert numbers into strings    s1 = str(A)    s2 = str(B)         # Check if s2 is a prefix of s1    # or not using startswith() function    result = s1.startswith(s2)         # If result is true print Yes    if result:        print("Yes")    else:        print("No") # Driver codeif __name__=='__main__':         # Given numbers    A = 12345    B = 12         # Function call    checkprefix(A, B)     # This code is contributed by virusbuddah_
// C# program for the above approachusing System.Threading;using System.Globalization;using System; class GFG{     // Function to check if B is a// prefix of A or notstatic void checkprefix(int A, int B){     // Convert numbers into Strings    string s1 = A.ToString();    string s2 = B.ToString();     bool result;     // Check if s2 is a prefix of s1    // or not using starts_with() function    result = s1.StartsWith(s2, false,        CultureInfo.InvariantCulture);     // If result is true, print "Yes"    if (result)    {        Console.Write("Yes");    }    else    {        Console.Write("No");    }} // Driver codestatic void Main(){         // Given numbers    int A = 12345, B = 12;         // Function call    checkprefix(A, B);}} // This code is contributed by divyeshrabadiya07
<script>       // JavaScript program for the above approach      // Function to check if B is a      // prefix of A or not      function checkprefix(A, B) {        // Convert numbers into Strings        var s1 = A.toString();        var s2 = B.toString();         var result;         // Check if s2 is a prefix of s1        // or not using starts_with() function        result = s1.startsWith(s2);         // If result is true, print "Yes"        if (result) {          document.write("Yes");        } else {          document.write("No");        }      }       // Driver code      // Given numbers      var A = 12345,        B = 12;       // Function call      checkprefix(A, B);        </script>
Yes
Time Complexity: O(1)Auxiliary Space: O(1)
virusbuddha
Amal Kumar Choubey
SHUBHAMSINGH10
divyeshrabadiya07
bunnyram19
arorakashish0911
rdtank
singhh3010
cpp-boost
prefix
Mathematical
Pattern Searching
Strings
Strings
Mathematical
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Find minimum number of coins that make a given value
Algorithm to solve Rubik's Cube
KMP Algorithm for Pattern Searching
Rabin-Karp Algorithm for Pattern Searching
Check if a string is substring of another
Check if an URL is valid or not using Regular Expression
Boyer Moore Algorithm for Pattern Searching