File tree 5 files changed +123
-0
lines changed
solution/3100-3199/3110.Score of a String
5 files changed +123
-0
lines changed Original file line number Diff line number Diff line change @@ -138,6 +138,52 @@ function scoreOfString(s: string): number {
138
138
}
139
139
```
140
140
141
+ #### Rust
142
+
143
+ ``` rust
144
+ impl Solution {
145
+ pub fn score_of_string (s : String ) -> i32 {
146
+ s . as_bytes ()
147
+ . windows (2 )
148
+ . map (| w | (w [0 ] as i32 - w [1 ] as i32 ). abs ())
149
+ . sum ()
150
+ }
151
+ }
152
+ ```
153
+
154
+ #### C#
155
+
156
+ ``` cs
157
+ public class Solution {
158
+ public int ScoreOfString (string s ) {
159
+ int ans = 0 ;
160
+ for (int i = 1 ; i < s .Length ; ++ i ) {
161
+ ans += Math .Abs (s [i ] - s [i - 1 ]);
162
+ }
163
+ return ans ;
164
+ }
165
+ }
166
+ ```
167
+
168
+ #### PHP
169
+
170
+ ``` php
171
+ class Solution {
172
+ /**
173
+ * @param String $s
174
+ * @return Integer
175
+ */
176
+ function scoreOfString($s) {
177
+ $ans = 0;
178
+ $n = strlen($s);
179
+ for ($i = 1; $i < $n; ++$i) {
180
+ $ans += abs(ord($s[$i]) - ord($s[$i - 1]));
181
+ }
182
+ return $ans;
183
+ }
184
+ }
185
+ ```
186
+
141
187
<!-- tabs: end -->
142
188
143
189
<!-- solution: end -->
Original file line number Diff line number Diff line change @@ -136,6 +136,52 @@ function scoreOfString(s: string): number {
136
136
}
137
137
```
138
138
139
+ #### Rust
140
+
141
+ ``` rust
142
+ impl Solution {
143
+ pub fn score_of_string (s : String ) -> i32 {
144
+ s . as_bytes ()
145
+ . windows (2 )
146
+ . map (| w | (w [0 ] as i32 - w [1 ] as i32 ). abs ())
147
+ . sum ()
148
+ }
149
+ }
150
+ ```
151
+
152
+ #### C#
153
+
154
+ ``` cs
155
+ public class Solution {
156
+ public int ScoreOfString (string s ) {
157
+ int ans = 0 ;
158
+ for (int i = 1 ; i < s .Length ; ++ i ) {
159
+ ans += Math .Abs (s [i ] - s [i - 1 ]);
160
+ }
161
+ return ans ;
162
+ }
163
+ }
164
+ ```
165
+
166
+ #### PHP
167
+
168
+ ``` php
169
+ class Solution {
170
+ /**
171
+ * @param String $s
172
+ * @return Integer
173
+ */
174
+ function scoreOfString($s) {
175
+ $ans = 0;
176
+ $n = strlen($s);
177
+ for ($i = 1; $i < $n; ++$i) {
178
+ $ans += abs(ord($s[$i]) - ord($s[$i - 1]));
179
+ }
180
+ return $ans;
181
+ }
182
+ }
183
+ ```
184
+
139
185
<!-- tabs: end -->
140
186
141
187
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public int ScoreOfString ( string s ) {
3
+ int ans = 0 ;
4
+ for ( int i = 1 ; i < s . Length ; ++ i ) {
5
+ ans += Math . Abs ( s [ i ] - s [ i - 1 ] ) ;
6
+ }
7
+ return ans ;
8
+ }
9
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ /**
3
+ * @param String $s
4
+ * @return Integer
5
+ */
6
+ function scoreOfString ($s ) {
7
+ $ans = 0 ;
8
+ $n = strlen ($s );
9
+ for ($i = 1 ; $i < $n ; ++ $i ) {
10
+ $ans += abs (ord ($s [$i ]) - ord ($s [$i - 1 ]));
11
+ }
12
+ return $ans ;
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn score_of_string ( s : String ) -> i32 {
3
+ s. as_bytes ( )
4
+ . windows ( 2 )
5
+ . map ( |w| ( w[ 0 ] as i32 - w[ 1 ] as i32 ) . abs ( ) )
6
+ . sum ( )
7
+ }
8
+ }
You can’t perform that action at this time.
0 commit comments