VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-if-a-number-starts-with-another-number-or-not/

⇱ Check if a number starts with another number or not - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if a number starts with another number or not

Last Updated : 15 Jul, 2025

Given two numbersA 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:

  1. Convert the given numbers A and B to strings str1 and str2 respectively.
  2. Traverse both the strings from the start of the strings.
  3. While traversing the strings, if at any index characters from str1 and str2 are unequal then print "No".
  4. Else print "Yes".

Below is the implementation of the above approach: 


Output
Yes


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:


Output
Yes

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

Comment