The challenge

There is a secret string which is unknown to you. Given a collection of random triplets from the string, recover the original string.

A triplet here is defined as a sequence of three letters such that each letter occurs somewhere before the next in the given string. “whi” is a triplet for the string “whatisup”.

As a simplification, you may assume that no letter occurs more than once in the secret string.

You can assume nothing about the triplets given to you other than that they are valid triplets and that they contain sufficient information to deduce the original string. In particular, this means that the secret string will never contain letters that do not occur in one of the triplets given to you.

The solution in Java code

Option 1:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.lang.String;
import java.lang.Character;
import java.util.List;
import java.util.LinkedList;
import java.util.Arrays;

public class SecretDetective {

    public String recoverSecret(char[][] triplets) {
        List<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 removed = wordList.remove(cIndex);
                        wordList.add(pIndex, removed);
                        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()).reduce((p,n) -> p + n).get();
    }
  
}

Option 2:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class SecretDetective {

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

Option 3:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.LinkedList;

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

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


                } else {
                    if (list.subList(0, index).contains(triplets[i][j])) {
                        list.remove(list.indexOf(triplets[i][j]));
                        index--;
                    }
                    ;
                    if (!list.subList(index, list.size()).contains(triplets[i][j])) {
                        if (index == list.size()) list.add(triplets[i][j]);
                        else list.add(index + 1, triplets[i][j]);
                    }
                    index = list.indexOf(triplets[i][j]);
                }
            }

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

}

Test cases to validate our solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class SecretRecoveryTest {

  private SecretDetective detective;
  
  @Before public void setup() {
    detective = new SecretDetective();
  }

  @Test 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));
  }
}

Additional test cases

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class SecretRecoveryTest {

  private SecretDetective detective;
  
  @Before public void setup() {
    detective = new SecretDetective();
  }

  @Test 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));
  }

  @Test 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));
  }
  
  @Test 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));
  }

  @Test 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));
  }

  @Test 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'}, {'k','p','v'}, {'w','x','z'}, {'k','x','y'}, 
      {'r','w','x'}, {'a','n','w'}, {'b','d','t'}, {'p','u','y'}, 
      {'n','v','z'}, {'f','k','q'}, {'i','m','z'}, {'a','w','y'}, 
      {'b','k','n'}, {'t','u','w'}, {'x','y','z'}, {'f','g','j'}, 
      {'n','y','z'}, {'s','y','z'}, {'k','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','k','r'}, 
      {'n','u','w'}, {'h','v','w'}, {'t','u','y'}, {'l','q','y'}, 
      {'v','w','x'}, {'r','w','z'}, {'m','o','w'}, {'k','q','x'}, 
      {'e','h','r'}, {'e','k','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'}, {'k','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','k','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'}, 
      {'k','l','z'}, {'n','q','w'}, {'m','u','z'}, {'k','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','k'}, {'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','k','l'}, {'f','p','s'}, 
      {'p','r','v'}, {'g','x','z'}, {'o','u','z'}, {'h','k','s'}, 
      {'i','r','w'}, {'n','q','y'}, {'o','q','r'}, {'f','q','y'}, 
      {'e','j','z'}, {'e','o','u'}, {'j','k','z'}, {'b','g','t'}, 
      {'f','v','w'}, {'w','x','y'}, {'t','v','w'}, {'a','p','w'}, 
      {'c','l','x'}, {'q','s','y'}, {'k','n','q'}, {'d','y','z'}, 
      {'i','p','v'}, {'e','k','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','k','u'}, {'a','k','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'}, 
      {'k','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','k','o'}, {'d','j','m'}, 
      {'r','t','u'}, {'k','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','k','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'}, {'k','x','z'}, 
      {'p','v','y'}, {'j','l','p'}, {'p','v','z'}, {'f','h','t'}, 
      {'k','n','x'}, {'f','n','o'}, {'p','v','w'}, {'k','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'}, {'k','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'}, {'k','w','z'}, {'f','i','n'}, 
      {'i','u','y'}, {'p','v','x'}, {'k','l','u'}, {'b','c','f'}, 
      {'f','q','v'}, {'c','h','u'}, {'i','n','w'}, {'q','s','t'}, 
      {'k','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'}, 
      {'k','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'}, {'k','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'}, {'k','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','k','t'}, {'j','l','m'}, {'d','e','x'}, {'j','x','y'}, 
      {'a','k','v'}, {'j','q','v'}, {'s','v','y'}, {'d','k','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'}, 
      {'k','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','k'}, {'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'}, {'k','t','v'}, {'o','p','w'}, 
      {'e','k','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','k','y'}, {'f','x','y'}, 
      {'h','m','n'}, {'s','v','x'}
    };
    assertEquals("abcdefghijklmnopqrstuvwxyz", detective.recoverSecret(triplets));
  }
}