Separate the Numbers

Sort by

recency

|

670 Discussions

|

  • + 0 comments

    instead of a lot of comparisons, just take first number and build it and then check

        public static void separateNumbers(String s) {
            if (s.charAt(0) == '0') {
                System.out.println("NO");
                return;
            } else {
                for (int i = 1; i < (s.length() / 2)+1; i++) {
                    Long num = Long.valueOf(s.substring(0, i));
                    StringBuilder b = new StringBuilder(s.substring(0,i));
                    Long l = Long.valueOf(num);
                    while (b.length() < s.length()) {
                        b.append(l + 1);
                        l = l + 1;
                    }
                    if (b.toString().equals(s)) {
                        System.out.println("YES " + num);
                        return;
                    }
                }
                System.out.println("NO");
            }
        }
    
  • + 0 comments

    Java solution(i feel it need some optimization):

    static boolean thereLeadingZero(String s){
            return s.length() > 1 && s.startsWith("0");  
            }
        
        static boolean validateTokens(String curr_token, String next_token){
            
            if(thereLeadingZero(curr_token) || thereLeadingZero(next_token))
                return false;
                
            long curr = Long.parseLong(curr_token);
            long next = Long.parseLong(next_token);
            return (next - curr) == 1;
            }
        
       
        public static void separateNumbers(String s) {
        int n = s.length();
        int curr_index = 0, curr_len = 1;
        int next_index = 1, next_len = 1;
        String first_token= "";
        
        if(n == 1){
            System.out.println("NO");
            return;
            }
        
        while(true){
            String curr_token = s.substring(curr_index, curr_index + curr_len);
            String next_token = s.substring(next_index, next_index + next_len);
           
            if(curr_index == 0)
                first_token = curr_token;
                    
            if( validateTokens(curr_token, next_token) ){
                
                if(next_index + next_len == n){
                    System.out.println("YES " + first_token);
                    return;
                    }
                curr_index = next_index;
                curr_len = next_len;
                next_index = curr_index + curr_len;
                next_len = curr_len;    
                }
            
            else{
                if( (next_len - curr_len) == 1 ){
                    curr_index = 0;
                    curr_len += 1;
                    next_index = curr_index + curr_len;
                    next_len = curr_len;
                    }
                else{
                    next_len += 1;
                    }        
                }
            
            if(next_index + next_len > n){
                System.out.println("NO");
                return;
                }     
            
            }
            
        } 
    
  • + 0 comments
    def separateNumbers(s):
        if s[0] == '0':
            print('NO')
            return
        
        for i in range(1,len(s)//2+1):
            p = 0
            k = 0
            fir = (0,i)
            x = s[fir[0]:fir[1]]
            while fir[1] < len(s):
                if re.match(r'9+$', s[fir[0]:fir[1]]):
                    p += 1
                sec = (fir[1], fir[1]+i+p)
                if int(s[fir[1]:fir[1]+i+p]) - int(s[fir[0]:fir[1]]) != 1:
                    break
                fir = sec
                k+=1
            else:
                print('YES', x)
                return
        print('NO')
    
  • + 0 comments

    My C code 😎😁

    void separateNumbers(char* s) {
        int len = strlen(s);
        if(len == 1){
            printf("NO\n");
            return;
        }
        
        for(int i = 1;i <= len /2 ;i++){
            char t[32];
            strncpy(t,s,i);
            t[i] = '\0';
            long long firstNum = atoll(t);
            
            char tempStr[1024];
            snprintf(tempStr,sizeof(tempStr),"%lld",firstNum);
            long long nextNum = firstNum + 1;
            while(strlen(tempStr) < len){
                char buffer[32];
                snprintf(buffer,sizeof(buffer),"%lld",nextNum);
                strcat(tempStr,buffer);
                nextNum++;
            }
            
            if(strcmp(tempStr,s) == 0){
                printf("YES %lld\n",firstNum);
                return;
            }
        }
        printf("NO\n");
    }
    
  • + 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;
    }