-1

what is the difference between these two loops? I was working on a few competitive programming challenges, everytime I was using the first loop variant I was failing and when i changed it to the second kind of loop it passes all the tests:

Loop variant 1:

for(int j=0; j<str1.length() ; j++) {
    char ch =  str1[j]
    int diff = ch-'a';
    arr1[diff]++;
}

Loop variant 2:

for(int i =0; i<str1.length() ;i++) {
    arr1[str1[i]-'a']++;
}

I understand that this is a silly question but please be patient, I just want to be clear about why the first one is not working.


Example: Find the minimum number of character deletions required for two given strings to be anagrams

Input: cde abc

Output: 4

Incorrect Code

void minDeletions(string str1, string str2) {
    if(str1 == str2){
        cout << 0 << endl;
        return;
    }

    int arr1[26] = {0};
    int diff,diff1;
    for(int i =0; i<str1.length() ;i++) {
        char ch =  str1[i];
        diff = ch-'a';
        arr1[diff]++;
    }

    int arr2[26] = {0};
    for(int j=0; j<str2.length() ; j++) {
        char ch =  str2[j];
        diff1 = ch-'a';
        arr2[diff]++;
    }
    int count = 0;
    for(int k=0; k<26 ; k++){
        if(arr1[k]!=arr2[k]){
            count += abs(arr1[k]-arr2[k]);
        }
    }

    cout << count << endl;
} 

int main() {
    string str1,str2;
    cin >> str1;
    cin >> str2;
    minDeletions(str1,str2);
    return 0;
}

Example of test case that fails

Input:

 fcrxzwscanmligyxyvym
 jxwtrhvujlmrpdoqbisbwhmgpmeoke

Output:

30
La bla bla
  • 8,558
  • 13
  • 60
  • 109
L887
  • 312
  • 1
  • 4
  • 15
  • Those look pretty much identical except that the first loop is more explicit in working step-by-step. Are you sure you didn't change anything else? Can you post two versions of the code, one that works and one that doesn't, along with a test case that differentiates the two? – templatetypedef Feb 08 '16 at 06:09
  • 1
    are you getting compile errors as I can see that you have missed a `;` after `char ch = str1[j]` in the first loop – The Apache Feb 08 '16 at 06:11
  • @TheApache ignore that, it was compiling fine, i just missed it while copying code over – L887 Feb 08 '16 at 06:12
  • @L887 so what's the failure you talking bout...logical error or compiler error?? – The Apache Feb 08 '16 at 06:13
  • If `str` contains any character that has ASCII code lower than that of `a`, your program is subject to undefined behavior. – R Sahu Feb 08 '16 at 06:16
  • Let me guess, str1[] isn't `char []`? Or another type-related problem, since types are not explicit in the second one. – iksemyonov Feb 08 '16 at 06:16
  • @templatetypedef I have posted detailed code and an example that fails – L887 Feb 08 '16 at 06:17
  • @RSahu lets assume the characters in the string are all valid i.e all of them are between a-z and only small case – L887 Feb 08 '16 at 06:18
  • why down vote this question-please provide feedback.. – L887 Feb 08 '16 at 06:25

1 Answers1

0

Cant this be it? Compared to the second loop variant, that works fine, there is no explicit int diff here that is a source of confusion (see below):

int arr1[26] = {0};
int diff, diff1;
for(int i = 0; i < str1.length() ; i++){
    char ch =  str1[i];
    diff = ch - 'a';
//  ^^^^
    arr1[diff]++;
//       ^^^^
}

int arr2[26] = {0};
for(int j = 0; j < str2.length() ; j++){
    char ch =  str2[j];
    diff1 = ch - 'a';
//  ^^^^^
    arr2[diff]++;
//       ^^^^
}

P.S. Look closely, both loops use the same index! I.e. it's not updated in the second loop after it gets a value in the first loop.

iksemyonov
  • 4,106
  • 1
  • 22
  • 42
  • thanks didn't see that, i will just go back to the competitive programming question submission and check if the mistake i made was exactly this. – L887 Feb 08 '16 at 06:22
  • Ok, be sure to report back. I'm really curious. – iksemyonov Feb 08 '16 at 06:24
  • 1
    note for OP: this mistake could have been avoided by not declaring variables until you're ready to initialize them , e.g. `int diff = '` inside the loop – M.M Feb 08 '16 at 06:25
  • @iksemyonov this was not error, the code i had in the submission has correct variable usage. I just checked that many submission via the submission server were having this problem of undefined behaviour – L887 Feb 08 '16 at 06:27
  • Why accept then? If it's not the error. Still, I'm very sure that this is the reason why one of the loops worked fine and the other one failed. – iksemyonov Feb 08 '16 at 06:29
  • @iksemyonov accepted because you helped me spot one of the errors, the issue was something with the ide i was working on. Its was an SPOJ ide and its been not working well lately – L887 Feb 08 '16 at 06:31
  • Glad this has been useful to you. – iksemyonov Feb 08 '16 at 06:32