[코테 풀이] Score of a String

시내·2024년 6월 10일
0

Q_3110) Score of a String

출처 : https://leetcode.com/problems/score-of-a-string/

You are given a string s. The score of a string is defined as the sum of the absolute difference between the ASCII values of adjacent characters.

Return the score of s.

class Solution {
    public int scoreOfString(String s) {
        int sum = 0;
        for (int i = 0; i < s.length() - 1; i++) {
            sum += Math.abs(s.charAt(i + 1) - s.charAt(i));
        }
        return sum;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글

관련 채용 정보