The Love-Letter Mystery

Sort by

recency

|

971 Discussions

|

  • + 0 comments

    Simple C Solution

    int theLoveLetterMystery(char* s) {
        int i=0,j=strlen(s)-1,count=0;
        while(i<j){
        if(s[i]!=s[j]) count +=abs(s[i]-s[j]);
        i++;
        j--;
        }
        return count;
    }
    
  • + 0 comments

    Here is my c++ solution to the problem, explanation here : https://youtu.be/gIUqS2xO6lg

    int theLoveLetterMystery(string s) {
        int ans = 0 , start = 0, end = s.size() - 1;
        while(start < end){
            ans += abs(s[start] - s[end]);
            start++; end--;
        }
        return ans;
    }
    
  • + 0 comments

    Java

    int changeCount = 0;
    for (int i = 0; i < s.length() / 2; i++) {
        changeCount += Math.abs(s.charAt(i) - s.charAt(s.length() - i - 1));
    }
    
    return changeCount;
    
  • + 0 comments

    return sum(abs(ord(s[i]) - ord(s[-i -1])) for i in range(int(len(s) / 2)))

  • + 0 comments

    JS:

    function theLoveLetterMystery(s) {
        // Write your code here
        
            let count=0;
            let c=0;
            let d=s.length-1;
            while(c<d){
                 let k=s.charCodeAt(c);
                 let l=s.charCodeAt(d); 
                count+=Math.abs(l-k); 
                c++;
                d--;
            }        
            return count;
    }