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
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Strings
  4. Separate the Numbers
  5. Discussions

Separate the Numbers

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 635 Discussions, By:

recency

Please Login in order to post a comment

  • deep099
    2 days 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
  • TuanBao
    2 weeks ago+ 0 comments

    My Java 8 (15) solution, feel free to ask me any questions.

    private static String createNumericString(long startNumber, int lenght) {
        String numeric = "";
        long current = startNumber;
        while(numeric.length() < lenght) {
            numeric = numeric.concat(String.valueOf(current));
            current += 1;
        }
        return numeric;
    }
        
    public static void separateNumbers(String s) {
        //Maximum numbers of the first integer
        int maximum = (int)Math.round(s.length()/2);
        for(int lenght = 1; lenght <= maximum; lenght++) {
            //Pick the first number
            long first = Long.valueOf(s.substring(0, lenght));
            //Perform checking a[i] - a[i - 1] = 1
            String numeric = createNumericString(first, s.length());
            if(numeric.equals(s)) {
                System.out.println("YES " + first);
                return;
            }
        }
        System.out.println("NO");
    }
    
    0|
    Permalink
  • alban_tyrex
    2 weeks ago+ 0 comments

    Here is my easy C++ solution, explanation here : https://youtu.be/e32U19k1X6A

    void separateNumbers(string s) {
        int bl = 1;
        bool f = false;
        while(bl * 2 <= s.size()){
            string base = s.substr(0, bl);
            string newString = "";
            long baselong = atol(base.c_str());
            do{
                newString += to_string(baselong);
                baselong++;
            }while(newString.size() < s.size());
            if(newString == s) {cout << "YES " << base;f = true;break;}
            bl++;
        }
        if(!f) cout << "NO";
        cout << endl;
    }
    
    0|
    Permalink
  • katie_chen2
    4 weeks ago+ 0 comments

    remember to check the range. |s|<=32, therefore the integer is 16-digit long at max, should use long to store its value.

    0|
    Permalink
  • birdmachine007
    4 weeks ago+ 0 comments

    Python 3: we can sove this question in 2 ways. one: is by adding into substr and second: is calculation difference of 1. Mine is from type one in O(n).

    def separateNumbers(s):
        loop_limit = len(s)//2
    
        substr = ""
        min_value = 0
        found = False
    
        for i in range(1, loop_limit + 1):
            slice_str = s[:i]
            temp = int(slice_str)
            min_value = temp
            substr = slice_str 
             
            while len(substr) < len(s):
                temp += 1
                substr += str(temp)
                
                if substr == s:
                    print("YES", min_value)
                    found = True
                    break
                
        if found == False:
          print("NO")
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy