@Override
  public void addPath(String word, int outputSymbol) {
    MutableState state = getStartState();

    if (state == null) {
      throw new IllegalStateException("Start state cannot be null");
    }
    List<MutableArc> arcs = state.getArcs();
    boolean isFound = false;
    for (MutableArc arc : arcs) {
      if (arc.getNextState().getLabel() == word.charAt(0)) {
        state = arc.getNextState();
        isFound = true;
        break;
      }
    }
    int foundPos = -1;
    if (isFound) {
      Pair<MutableState, Integer> pair = findPointOfDiversion(state, word);
      if (pair == null) {
        // Word already exists
        return;
      }
      foundPos = pair.getRight();
      state = pair.getLeft();
    }
    for (int i = foundPos + 1; i < word.length(); i++) {
      MutableState nextState = new MutableState();
      nextState.setLabel(word.charAt(i));

      int currentOutputSymbol = -1;
      if (i == word.length() - 1) {
        currentOutputSymbol = outputSymbol;
      }

      MutableArc mutableArc = new MutableArc(currentOutputSymbol, nextState);
      state.addArc(mutableArc);
      state = nextState;
    }
    state.setIsTerminal(true);
  }
@Test
  public void testRegexMatcherMatchQuestionMark() {
    MutableFST fst = new MutableFSTImpl();

    fst.addPath("car", 12);
    fst.addPath("cars", 21);

    RoaringBitmapWriter<MutableRoaringBitmap> writer = RoaringBitmapWriter.bufferWriter().get();
    RealTimeRegexpMatcher.regexMatch("cars?", fst, writer::add);

    Assert.assertEquals(writer.get().getCardinality(), 2);
  }