1
static boolean permutation(String s, String t){
    if(s.length() != t.length()) return false;
    int [] letters = new int [128];
    char[] s_array = s.toCharArray();
    for (char c: s_array){
        letters[c]++;
    }
    for(int i = 0; i< t.length(); i++){
        int c = (int) t.charAt(i);
        letters[c]--;
        if ( letters [c] < 0 )return false; 
    }
    return true;
}

So this code snippet is from Cracking the Coding Interview and I was wondering what the letters[c]++ and letters[c]-- means.

Is that the same thing as letters[c++]?

5 Answers 5

2

So this code snippet is from Cracking the Coding Interview and I was wondering what the letters[c]++ and letters[c]-- means.

Is that the same thing as letters[c++]?

No. letters[c]++ is equivalent to

letters[c] = letters[c] + 1;

and letters[c]-- performs

letters[c] = letters[c] - 1;

while letters[c++] is equivalent to,

letters[c] = letters[c];
c = c + 1;
Sign up to request clarification or add additional context in comments.

Comments

1

letters array is used to counting how much each letter in those two strings t and s has been repeated

each char has ascii-code so in this for-each loop , c convert to its ascii code , actually it casts from char to integer so it will be a index and ascii-code is unique for each character

in this statement letters[c]++; consider char c = 'A' so c is 65 according ascii-table , and in letters array 65th element will increase once, so until now we have one 'A' in our s string, and when loop ends we had counted repetition of each letter in string s.

for (char c: s_array){
    letters[c]++;
}

and other loop is similar , but it uses string t characters and for each character in t decreases once from it's repetition in string s to determine permutation of s and t

hope you understand what i said !

and for this question!

Is that the same thing as letters[c++]?

no they are different.consider char c ='A' this statement that you said will increase repetition of character B because ascii code of B is 66.

Comments

0

Is that the same thing as letters[c++]?

Not even a little bit. :-) letters[c++] in those places would be a syntax error (since Java doesn't allow arbitrary expressions as statements), since it would be letters[c]; c = c + 1;

letters[c]++ is effectively:

letters[c] = letters[c] + 1;

E.g., it's increasing the value in letters[c] by one. It's not changing c at all.

-- works the same way, just decreasing.

Comments

0

It's incrementing and decrementing values from the letters array. c is the index of the letters array you are incrementing; therefore letters[c++] would not work.

Comments

0

In this case are the same at this:

  letters[c] = letters[c] + 1;

And

  letters[c] = letters[c] - 1;

Because letters is an array of integer. But doesn't make sense when you use the ++ or -- operators on a value(when you have ex: an array of String).

2 Comments

letters[c]++ and letters[c] = letters[c] + 1; are same ! because letters[c] is an integer and we can use ` ++ or -- ` with an integer
In this case yes but in general when you have an array of STRING NOT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.