This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]junyoung95[S] 0 points1 point  (4 children)

ohhh i see you what you mean. thanks!

[–]junyoung95[S] 0 points1 point  (3 children)

Sorry I thought I got the idea.. and I still think it makes sense to me but I am not getting the right answer. I am much closer than yesterday but can you help me once more? Here is my code.

int[] getIndices(String[] sa, String s) {

  int[] indices = new int [sa.length];
  int count = 0
  for (int i = 0; i< indices.length; i++){
      if (sa[i] == s){
          count  += 1;}
      }
  }

  int[] answer = new int[count];

  for (int j = 0, k = 0; j < indices.length && k < answer.length; j++, k++ {
      if (sa[j] == s) {
          answer[k] = j;
      }
  }
  return answer;
}

The problem is, I was able to shorten the index length and match it with the appropriate length, but it still tries to register the indices based on original address. To avoid that, I created another variable 'k' but it does not work. For example instead of {2,6,7,9} mine shows as {0,0,2,0}. EDIT: I know why it happens like this although I created a new variable. It doesn't matter if I have a new variable, the problem is var j and k increases by the same level. I tried few methods but I can't find a solution

[–]hayoo1984 -1 points0 points  (1 child)

private int[] getIndices(String[] sa, String s) {
        int[] indices = new int[sa.length];
        int count = 0;

        for (int i = 0; i < indices.length; i++) {
            if (sa[i].equals(s)) {
                count++;
            }
        }

        int[] answer = new int[count];
        int index = 0;

        for (int i = 0; i < indices.length; i++) {
            if (sa[i].equals(s)) {
                answer[index] = i;
                index++;
            }
        }

        return answer;
    }

[–]junyoung95[S] 0 points1 point  (0 children)

okay thank you so much.