-2

Problem: Check if the numbers in the string are in increasing order.

Return:

  • True -> If numbers are in increasing order.
  • False -> If numbers are not in increasing order.

The String sequence are :

CASE 1 :1234     (Easy)      1 <2<3<4    TRUE
CASE 2 :9101112  (Medium)    9<10<11<12  TRUE
CASE 3 :9991000  (Hard)      999<1000    TRUE
CASE 4 :10203    (Easy)      1<02<03     FALSE
(numbers cannot have 0 separated).

*IMPORTANT : THERE IS NO SPACES IN STRING THAT HAVE NUMBERS"

My Sample Code:

// converting string into array of numbers
String[] str = s.split("");
int[] numbers = new int[str.length];
int i = 0;

for (String a : str) {
    numbers[i] = Integer.parseInt(a.trim());
    i++;
}
for(int j=0;j<str.length;j++)
     System.out.print(numbers[j]+" ");

//to verify whether they differ by 1 or not
int flag=0;
    
for(int j=0;j<numbers.length-1;j++){
    int result=Integer.parseInt(numbers[j]+""+numbers[j+1]) ;
    
    if(numbers[j]>=0 && numbers[j]<=8 && numbers[j+1]==numbers[j]+1){   
           flag=1;
    }
    else if(numbers[j]==9){
            int res=Integer.parseInt(numbers[j+1]+""+numbers[j+2]) ;
            if(res==numbers[j]+1)
                flag=1;
    }
    else if(result>9){
        //do something
     }
}  
   

This is the code I wrote ,but I cant understand how to perform for anything except one-digit-numbers ( Example one-digit number is 1234 but two-digit numbers are 121314). Can anyone have a solution to this problem?. Please share with me in comments with a sample code.

Aastha Jain
  • 180
  • 1
  • 1
  • 13
  • I think the best way to achieve the desired result is not have the input string separated by *spaces*. Like for example **999**, that can be interpreted as follows: **9 9 9**, **99 9**, **9 99**, or **999** – Den Isahac Feb 18 '17 at 08:24
  • Please learn to format (indent) your code for human readability. I, personally, refuse to look at code like this, so good luck. – Andreas Feb 18 '17 at 08:35
  • @DenIsahac. thanks for replying . But how to determine that we should consider a 3 digit number(999) or 2 digit number(99) or 1 digit number(9) at beginning of string . It is very confusing . Please give a advice. – Aastha Jain Feb 18 '17 at 08:40

4 Answers4

0

I'm gonna describe the solution for you, but you have to write the code.

You know that the input string is a sequence of increasing numbers, but you don't know how many digits is in the first number.

This means that you start by assuming it's 1 digit. If that fails, you try 2 digits, then 3, and so forth, until you've tried half the entire input length. You stop at half, because anything longer than half cannot have next number following it.

That if your outer loop, trying with length of first number from 1 and up.

In the loop, you extract the first number using substring(begin, end), and parse that into a number using Integer.parseInt(s). That is the first number of the sequence.

You then start another (inner) loop, incrementing that number by one at a time, formatting the number to text using Integer.toString(i), and check if the next N characters of the input (extracted using substring(begin, end)) matches. If it doesn't match, you exit inner loop, to make outer loop try with next larger initial number.

If all increasing numbers match exactly to the length of the input string, you found a good sequence.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

This is code for the pseudo-code suggested by Andreas .Thanks for the help.

       for (int a0 = 0; a0 < q; a0++) {
            String s = in.next();

            boolean flag = true;

            for (int i = 1; i < s.length() / 2; i++) {
                int first = Integer.parseInt(s.substring(0, i));
                            int k=1;
                for (int j = i; j < s.length(); j++) {
                 if (Integer.toString(first + (k++)).equals(s.substring(j, j + i)))
               flag = true;
                else{
                    flag=false;
                    break;
                }    
            }

            if (flag)
                System.out.println("YES");
            else
                System.out.println("NO");

        }
Aastha Jain
  • 180
  • 1
  • 1
  • 13
0

I would suggest the following solution. This code generates all substrings of the input sequence, orders them based on their start index, and then checks whether there exists a path that leads from the start index to the end index on which all numbers that appear are ordered. However, I've noticed a mistake (I guess ?) in your example: 10203 should also evaluate to true because 10<203.

import java.util.*;
import java.util.stream.Collectors;

public class PlayGround {


    private static class Entry {

        public Entry(int sidx, int eidx, int val) {
            this.sidx = sidx;
            this.eidx = eidx;
            this.val = val;
        }

        public int sidx = 0;
        public int eidx = 0;
        public int val = 0;

        @Override
        public String toString(){
            return String.valueOf(this.val);
        }
    }

    public static void main(String[] args) {
        assert(check("1234"));
        assert(check("9101112"));
        assert(check("9991000"));
        assert(check("10203"));
    }

    private static boolean check(String seq) {

        TreeMap<Integer,Set<Entry>> em = new TreeMap();


        // compute all substrings of seq and put them into tree map
        for(int i = 0; i < seq.length(); i++) {
            for(int k = 1 ; k <= seq.length()-i; k++) {
                String s = seq.substring(i,i+k);

                if(s.startsWith("0")){
                    continue;
                }

                if(!em.containsKey(i))
                    em.put(i, new HashSet<>());

                Entry e = new Entry(i, i+k, Integer.parseInt(s));
                em.get(i).add(e);
            }
        }


        if(em.size() <= 1)
            return false;


        Map.Entry<Integer,Set<Entry>> first = em.entrySet().iterator().next();

        LinkedList<Entry> wlist = new LinkedList<>();

        wlist.addAll(first.getValue().stream().filter(e -> e.eidx < seq
                .length()).collect(Collectors.toSet()));


        while(!wlist.isEmpty()) {
            Entry e = wlist.pop();

            if(e.eidx == seq.length()) {
                return true;
            }

            int nidx = e.eidx + 1;

            if(!em.containsKey(nidx))
                continue;

            wlist.addAll(em.get(nidx).stream().filter(n -> n.val > e.val).collect
                    (Collectors.toSet()));
        }

        return false;
    }
}
Julian
  • 1,694
  • 22
  • 29
-2

Supposed the entered string is separated by spaces, then the code below as follows, because there is no way we can tell the difference if the number is entered as a whole number.

boolean increasing = true;
String string = "1 7 3 4"; // CHANGE NUMBERS
String strNumbers[] = string.split(" "); // separate by spaces.


for(int i = 0; i < strNumbers.length - 1; i++) {

  // if current number is greater than the next number.
  if(Integer.parseInt(strNumbers[i]) > Integer.parseInt(strNumbers[i + 1])) {
    increasing = false;
    break; // exit loop
  }
}

if(increasing) System.out.println("TRUE");
else System.out.println("FALSE");
Den Isahac
  • 1,335
  • 11
  • 26
  • But the string is not separated by any space . All the numbers are concatenated with one another . That's why this is a difficult question and this wont work. – Aastha Jain Feb 18 '17 at 08:44