We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Strings
  4. Separate the Numbers
  5. Discussions

Separate the Numbers

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • deep099
    3 months ago+ 0 comments
    import java.io.*;
    import java.math.*;
    import java.security.*;
    import java.text.*;
    import java.util.*;
    import java.util.concurrent.*;
    import java.util.function.*;
    import java.util.regex.*;
    import java.util.stream.*;
    import static java.util.stream.Collectors.joining;
    import static java.util.stream.Collectors.toList;
    
    class Result {
    
    
        public static boolean separateNumbers(String s,String s1,int i,int j,String comp,String trace) {
        
        if(j>s.length())
        {
            return false;
        }
        else
        {
            comp=s1;
            s1=s.substring(i,j);
            if(comp==null)
            trace=s1;
            
            if(comp!=null&&(Long.valueOf(comp)+1)!=Long.valueOf(s1))
            return false;
            
            else if(comp!=null&&((Long.valueOf(comp)+1)==Long.valueOf(s1))&&(j+(Long.toString(Long.valueOf(s1)+1).length()))>s.length()&&j==s.length())
            {
                System.out.println("YES "+trace);
                return true;
            }
            
            
            i+=s1.length();
            j+=(Long.toString(Long.valueOf(s1)+1).length());
            
            return separateNumbers(s, s1, i, j,comp,trace);
            
            
        }
        
    
        }
    
    }
    
    public class Solution 
    {
        public static void main(String args[])throws Exception
        {
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            
            for(int k=1;k<=n;k++)
            {
                int flag=0;
                String str=sc.next();
                int max=str.length()/2; 
                for(int i=1;i<=max;i++)
                {
                    if (Result.separateNumbers(str,null,0,i,null,null)==true)
                    {
                        flag=1;
                    }
                }
                if(flag==0)
                System.out.println("NO");
    
            }
    
        }
    }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy