-4

I was trying competitive coding and there was this question where i was asked to print the number of strings which are greater than the given string

eg: ab: then the sunsequences possible are a, b, ab ... out of these b is the only greater one (note ba is not the subsequence)

abc : then the subsequences possible are a,b,c,ab,ac,bc,abc out of these only 4 fill the criteria....

so i tried this code

for(i=0;i<length;i++)
{  
  // find the number of permutations
}

but in this i also get strings which are not allowed by the problem.... so how do i find the subsequence.

  • why the downvote?? – Shivani Kaur Aug 03 '17 at 17:57
  • 1
    You would wonder how many questions at SO are asked for *"how to compute all permutations?*" ^^ So I would guess the down-votes come primarily from "*the question shows no research effort*". I suggest to do a quick search, then you probably have an algorithm to compute all permutations. After that you just need to count everything that passes your **filter criterion** which basically is given by `candidate.compareTo(input)` like seen here: https://stackoverflow.com/questions/5153496/how-can-i-compare-two-strings-in-java-and-define-which-of-them-is-smaller-than-t – Zabuzard Aug 03 '17 at 18:00
  • @Zabuza i can find all the permutations easily... but that lexicograpical criterion is the problem – Shivani Kaur Aug 03 '17 at 18:02
  • 1
    Okay, than `candidate.compareTo(input)` does the trick, like seen in the linked question. – Zabuzard Aug 03 '17 at 18:03
  • the thing is on creating permutations for lets say abc, it give a permutation as bac or bca which is not needed by me to perform the `compareTo`operation – Shivani Kaur Aug 03 '17 at 18:05
  • As far as I understood it you want, from all permutations, filter out the ones that are *smaller* than a given `input`, right? So you can just compute all permutations, iterate the result and check each element with `element.compareTo(input)` whether it is greater, smaller or equal. – Zabuzard Aug 03 '17 at 18:07
  • the example which i gave with the problem statement for abc are the only permutations that are possible according to the question. These permutations needs to be passed. But in actual there are more permutaions possible – Shivani Kaur Aug 03 '17 at 18:11
  • If an user answered your question please also accept his answer ([Accepting Answers: How does it work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)). If not than please specify what remains unanswered, this is a really crucial part of StackOverflow, thank you very much. – Zabuzard Sep 24 '17 at 15:41

1 Answers1

0

Based on your comments you already have a method that computes all permutations of a given input, lets call this method permutate, it takes a String and gives a String[].

Now you want, from all that permutations, discard the ones that are lexicographically smaller or equals to the given input. In the end you want a collection that only contains lexicographically greater permutations.


The only thing we now need is how do we compare Strings lexicographically? But this is rather easy as Strings already offer such a method, it is called compareTo (here is the official documentation). You use it with first.compareTo(second) and it returns a negative, positive or zero value based on if the first String is lexicographically smaller, greater or equals to the second String.

So we just iterate the resulting permutations and check with compareTo which elements need to be discarded and which we keep.


Here is a snippet:

String input = "abc";
String[] allPermutations = permutate(input); // Contains [a, b, c, ab, ac, bc, abc]

ArrayList<String> permutationsToKeep = new ArrayList<>();
for (String permutation : allPermutations) {
    if (permutation.compareTo(input) > 0) {
        // Permutation is lexicographically greater than input, keep it
        permutationsToKeep.add(permutation);
    }
}

// permutationsToKeep now contains your desired result
// [b, c, ac, bc]
Zabuzard
  • 25,064
  • 8
  • 58
  • 82