From 62573d72aa0dee5e0b822296e1d422831dcc5c3a Mon Sep 17 00:00:00 2001 From: Libin YANG <contact@yanglibin.info> Date: Sat, 15 Mar 2025 08:02:41 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3110 No.3110.Score of a String --- .../3110.Score of a String/README.md | 46 +++++++++++++++++++ .../3110.Score of a String/README_EN.md | 46 +++++++++++++++++++ .../3110.Score of a String/Solution.cs | 9 ++++ .../3110.Score of a String/Solution.php | 14 ++++++ .../3110.Score of a String/Solution.rs | 8 ++++ 5 files changed, 123 insertions(+) create mode 100644 solution/3100-3199/3110.Score of a String/Solution.cs create mode 100644 solution/3100-3199/3110.Score of a String/Solution.php create mode 100644 solution/3100-3199/3110.Score of a String/Solution.rs diff --git a/solution/3100-3199/3110.Score of a String/README.md b/solution/3100-3199/3110.Score of a String/README.md index ebb9d173910c2..a0ae43cb5b51c 100644 --- a/solution/3100-3199/3110.Score of a String/README.md +++ b/solution/3100-3199/3110.Score of a String/README.md @@ -138,6 +138,52 @@ function scoreOfString(s: string): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn score_of_string(s: String) -> i32 { + s.as_bytes() + .windows(2) + .map(|w| (w[0] as i32 - w[1] as i32).abs()) + .sum() + } +} +``` + +#### C# + +```cs +public class Solution { + public int ScoreOfString(string s) { + int ans = 0; + for (int i = 1; i < s.Length; ++i) { + ans += Math.Abs(s[i] - s[i - 1]); + } + return ans; + } +} +``` + +#### PHP + +```php +class Solution { + /** + * @param String $s + * @return Integer + */ + function scoreOfString($s) { + $ans = 0; + $n = strlen($s); + for ($i = 1; $i < $n; ++$i) { + $ans += abs(ord($s[$i]) - ord($s[$i - 1])); + } + return $ans; + } +} +``` + <!-- tabs:end --> <!-- solution:end --> diff --git a/solution/3100-3199/3110.Score of a String/README_EN.md b/solution/3100-3199/3110.Score of a String/README_EN.md index 8cf897eeecb94..b0c1302de6c0c 100644 --- a/solution/3100-3199/3110.Score of a String/README_EN.md +++ b/solution/3100-3199/3110.Score of a String/README_EN.md @@ -136,6 +136,52 @@ function scoreOfString(s: string): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn score_of_string(s: String) -> i32 { + s.as_bytes() + .windows(2) + .map(|w| (w[0] as i32 - w[1] as i32).abs()) + .sum() + } +} +``` + +#### C# + +```cs +public class Solution { + public int ScoreOfString(string s) { + int ans = 0; + for (int i = 1; i < s.Length; ++i) { + ans += Math.Abs(s[i] - s[i - 1]); + } + return ans; + } +} +``` + +#### PHP + +```php +class Solution { + /** + * @param String $s + * @return Integer + */ + function scoreOfString($s) { + $ans = 0; + $n = strlen($s); + for ($i = 1; $i < $n; ++$i) { + $ans += abs(ord($s[$i]) - ord($s[$i - 1])); + } + return $ans; + } +} +``` + <!-- tabs:end --> <!-- solution:end --> diff --git a/solution/3100-3199/3110.Score of a String/Solution.cs b/solution/3100-3199/3110.Score of a String/Solution.cs new file mode 100644 index 0000000000000..676e759b9054e --- /dev/null +++ b/solution/3100-3199/3110.Score of a String/Solution.cs @@ -0,0 +1,9 @@ +public class Solution { + public int ScoreOfString(string s) { + int ans = 0; + for (int i = 1; i < s.Length; ++i) { + ans += Math.Abs(s[i] - s[i - 1]); + } + return ans; + } +} diff --git a/solution/3100-3199/3110.Score of a String/Solution.php b/solution/3100-3199/3110.Score of a String/Solution.php new file mode 100644 index 0000000000000..b87ea9c5d9228 --- /dev/null +++ b/solution/3100-3199/3110.Score of a String/Solution.php @@ -0,0 +1,14 @@ +class Solution { + /** + * @param String $s + * @return Integer + */ + function scoreOfString($s) { + $ans = 0; + $n = strlen($s); + for ($i = 1; $i < $n; ++$i) { + $ans += abs(ord($s[$i]) - ord($s[$i - 1])); + } + return $ans; + } +} diff --git a/solution/3100-3199/3110.Score of a String/Solution.rs b/solution/3100-3199/3110.Score of a String/Solution.rs new file mode 100644 index 0000000000000..eb6ead66a23e1 --- /dev/null +++ b/solution/3100-3199/3110.Score of a String/Solution.rs @@ -0,0 +1,8 @@ +impl Solution { + pub fn score_of_string(s: String) -> i32 { + s.as_bytes() + .windows(2) + .map(|w| (w[0] as i32 - w[1] as i32).abs()) + .sum() + } +}