The Love-Letter Mystery

  • + 0 comments

    C++:

    int theLoveLetterMystery(string s) {
        int cnt = 0;
        int k = 0;
        int left_part = 0;
        int right_part = 0;
        while (k < int(s.size() / 2)) {
            left_part = int(s[k]);
            right_part = int(s[s.size() - k - 1]);
            while (left_part != right_part) {
                if (left_part < right_part) {
                    cnt++;
                    right_part -= 1;
                } else if (left_part > right_part) {
                    cnt++;
                    left_part -= 1;
                }
            }
            k++;
        }
        return cnt;
    }