Check length of a String is Equal to the Number Appended at its Last  

Difficulty Level Easy
Frequently asked in CodeNation Fourkites Microsoft UHG Optum
String

Problem Statement  

In the “Check length of a string is Equal to the Number Appended at its Last” problem we have given a string that is appended with a number at last. Write a program that checks whether the length of the string excluding the number is the same as the number(which is appended at the end) or not.

Input Format  

The first and only line containing a string s.

Output Format  

Print “Yes” if the number which is appended at the end of the string is equal to the length of the string excluding the number.  Otherwise, print “No“.

Constraints  

  • 1<=|s|<=10^6.
  • s[i] is either a lower case alphabet or an integer 0 to 9.

Example  

tutorialcup11
YES

Explanation: Here we first check the length of the string which is 11. Now we find the number which is appended at the end of the string which is 11. Now we check both values and we see both are the same. So the answer is “YES”.

Please click Like if you loved this article?

Algorithm  

  1. Take the input and set temp to zero initially.
  2. Traverse from the beginning of the string and add 1 if s[i] is a char otherwise break.
  3. Traverse from the end of the string till we getting s[i] as “0” to “9” and find the value of the number(X) which is appended at the end.
  4. Check if X is equal to the temp then print “Yes” else print “No“.
See also
Reverse Integer

Implementation  

C++ program to Check length of a String is Equal to the Number Appended at its Last

#include <bits/stdc++.h>
using namespace std;

int main()
{
   string s;
   cin>>s;
   int temp=0;
   int n=s.length();
   for(int i=0;i<n;i++)
   {
       if(s[i]>='a' && s[i]<='z')
       {
           temp++;
       }
       else
       {
           break;
       }
   }
   int x=1,num=0;
   for(int i=n-1;i>=0;i--)
   {
        if(s[i]>='0' && s[i]<='9') 
        { 
           num=(s[i]-'0')*x+num; 
           x=x*10; 
        } 
        else 
        {
           break;
        }
   }
   if(num==temp)
   {
       cout<<"YES"<<endl;
   }
   else
   {
       cout<<"NO"<<endl;
   }
   return 0;
}

Java program to Check length of a String is Equal to the Number Appended at its Last

import java.util.Scanner;

class sum { 
    public static void main(String[] args) 
    {
        Scanner sr= new Scanner(System.in);
        String s= sr.next();
        int n = s.length(); 
        char ans[] = s.toCharArray();
        int temp=0;
        for(int i=0;i<n;i++)
        {
            if(ans[i]>='a' && ans[i]<='z') 
            { 
                temp++;
            } 
            else  
            { 
                break; 
            }
        }
        int x=1,num=0;
        for(int i=n-1;i>=0;i--)
        {
            if(ans[i]>='0' && ans[i]<='9') 
            { 
                num=(ans[i]-'0')*x+num; 
                x=x*10; 
            } 
            else 
            {
                break;
            }
        }
        if(num==temp)
        {
            System.out.println("YES");
        }
        else
        {
            System.out.println("NO");
        }
    } 
}
tutorialcup11
YES

Complexity Analysis  

Time Complexity

O(n) where n is the size of the given array. Here we just traverse the string and perform some tasks.

Space Complexity

O(1) because we don’t create any space for the solution. Here we just visit the whole string and update some integer values for getting the solution.

Reference

Please click Like if you loved this article?