Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions solutions/3101-3200/3110-score-of-a-string/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 3110. Score of a String

[LeetCode problem 3110](https://leetcode.com/problems/score-of-a-string/)

- **Difficulty**: Easy
- **Tags**: string

## Description

Given a string `s`, its score is the sum of the absolute difference between
the ASCII values of every pair of adjacent characters.
19 changes: 19 additions & 0 deletions solutions/3101-3200/3110-score-of-a-string/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "solution.hpp"

#include <cstdlib>
#include <iterator>

namespace leetcode::p3110 {

int solve(const std::string& s) {
int score = 0;

for (int i = 0; i + 1 < std::ssize(s); ++i) {
score += std::abs(static_cast<unsigned char>(s.at(static_cast<std::size_t>(i))) -
static_cast<unsigned char>(s.at(static_cast<std::size_t>(i) + 1)));
}

return score;
}

} // namespace leetcode::p3110
11 changes: 11 additions & 0 deletions solutions/3101-3200/3110-score-of-a-string/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <string>

namespace leetcode::p3110 {

// optimized: O(n) time, O(1) space — sum |s[i] - s[i+1]| over adjacent
// characters.
int solve(const std::string& s);

} // namespace leetcode::p3110
8 changes: 8 additions & 0 deletions solutions/3101-3200/3110-score-of-a-string/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <catch2/catch_test_macros.hpp>

#include "solution.hpp"

TEST_CASE("Problem 3110", "[p3110]") {
CHECK(leetcode::p3110::solve("hello") == 13);
CHECK(leetcode::p3110::solve("zaz") == 50);
}
24 changes: 0 additions & 24 deletions swift/3110.swift

This file was deleted.

Loading