/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Wrong Answer 1ms 324.0 KiB
#2 Wrong Answer 6ms 1.312 MiB

Code

#include <stdio.h>
#include <string.h>

long long count_swaps(char *s, int n) {
    int freq[26] = {0}; // frequency array for lowercase letters
    long long swaps = 0;

    for (int i = 0; i < n; i++) {
        freq[s[i] - 'a']++; // increment frequency of each character
    }

    for (int i = 0; i < 26; i++) {
        swaps += (freq[i] * (freq[i] - 1)) / 2; // calculate swaps for each character
    }

    return swaps;
}

int main() {
    int T;
    scanf("%d", &T);

    for (int i = 0; i < T; i++) {
        int N;
        scanf("%d", &N);
        char S[N + 1]; // +1 for null terminator
        scanf("%s", S);

        printf("%lld\n", count_swaps(S, N));
    }

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1038 Do not touch my string
Contest
Brain Booster #3
Language
C++17 (G++ 13.2.0)
Submit At
2024-05-06 15:45:11
Judged At
2024-11-11 03:33:55
Judged By
Score
0
Total Time
6ms
Peak Memory
1.312 MiB