Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.3110 #4160

Merged
merged 1 commit into from
Mar 15, 2025
Merged
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
46 changes: 46 additions & 0 deletions solution/3100-3199/3110.Score of a String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
Expand Down
46 changes: 46 additions & 0 deletions solution/3100-3199/3110.Score of a String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
Expand Down
9 changes: 9 additions & 0 deletions solution/3100-3199/3110.Score of a String/Solution.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
14 changes: 14 additions & 0 deletions solution/3100-3199/3110.Score of a String/Solution.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 8 additions & 0 deletions solution/3100-3199/3110.Score of a String/Solution.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}
Loading