Leetcode — Score of a String
It’s an easy problem from leet code with the description being:
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.Example 1:
Input: s = “hello”
Output: 13Explanation:
The ASCII values of the characters in s are: 'h' = 104, 'e' = 101, 'l' = 108, 'o' = 111. So, the score of s would be |104 - 101| |101 - 108| |108 - 108| |108 - 111| = 3 7 0 3 = 13.Example 2:
Input: s = “zaz”
Output: 50Explanation:
The ASCII values of the characters in s are: 'z' = 122, 'a' = 97. So, the score of s would be |122 - 97| |97 - 122| = 25 25 = 50.
Constraints:2 <= s.length <= 100
s consists only of lowercase English letters.
There is no much to do into this, you basically have iterate and grab two characters of the string and subtract first to the second and get the absolute of it and add into a sum variable.
It’s important to start on second index to avoid out of bounds exception.
To get char as ascii you basically cast as integer and that would do.
class Solution { public int scoreOfString(String s) { int sum = 0; for(int i=1;i<s.length();i++){ final int first = (int) s.charAt(i - 1); final int second = (int) s.charAt(i); sum += Math.abs(first - second); } return sum; } }
Runtime: 1 ms, faster than 99.37% of Java online submissions for Score of a String.
Memory Usage: 42 MB, less than 30.18% of Java online submissions for Score of a String.
—
That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.
Until next post! :)
The above is the detailed content of Leetcode — Score of a String. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...
