Error in code for MATPAN

import java.util.Scanner;
import java.util.Arrays;
public class codechef {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int[] integers = new int[26];
int i, sum = 0;
for (i = 0; i < 26; i++) {
integers[i] = sc.nextInt();
}
int sum2 = 0;
for (i = 0; i < 26; i++) {
sum2 += integers[i];
}

    Scanner string = new Scanner(System.in);
    String sr;
    sr = string.nextLine();
    
    char[] charArray = sr.toCharArray();
    Arrays.sort(charArray);
    String sortedString = new String(charArray);
    String srt="abcdefghijklmnopqrstuvwxyz";
    char[] abc= srt.toCharArray();
    for(int k=0;k<charArray.length;k++) {
        for (int j = 0; j < 26; j++) {
            if ( abc[j] ==charArray[k]) {
                sum = sum +integers[j];
            } else {
                int p=0;
            }
        }
    }int op=sum2-sum;
    System.out.print(op);
}

}

there are two errors RE (NZEC)(0.070000) and WA(0.130000)

N is not equal to 26…

i guess you do not the the frequency array concept , see in all these frequencies problems what you need to do is just simply create an array of 26 elements initialized 0.

the simply iterate through the string.

for(i=0;i<s.length();i++)
arr[s[i]-'a']=1:

what this will do is whichever elements are present , for e.g if a is present then 0th pos off arr will be filled with 1 , and then you can just iterate through the array of 26 elements to check which elements are 0 , indicting that they are not present , simply calculate the sum. sum=0

for (int i=0;i<26;i++)
{
if (a[i]==0)
sum+=s[i]
}
print sum

@raj79 . I did the same but got only 30 pts. TlE in rest.

link to my submission.- link

https://www.codechef.com/viewsolution/15136162

check this out , i just submitted your solution , your solution was taking a lot more time since every time strlen(s) was calculated when the checking condition of the for loop was done. its better to calculate it before and then use , since it will then be done once only , rather then every time the comparison comes in.

1 Like

check below

thanks a lot.