Skip to content

Commit bfca907

Browse files
committed
02-06-2024
1 parent ee7d085 commit bfca907

File tree

2 files changed

+26
-35
lines changed

2 files changed

+26
-35
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
void reverseString(vector<char>& s) {
4+
5+
int n = s.size();
6+
7+
for(int i = 0 ; i < n/2 ; i++)
8+
{
9+
swap(s[i] , s[n-i-1]);
10+
}
11+
}
12+
};

readme.md

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,47 @@
1-
# 3110. Score of a String
1+
# 344. Reverse String
22

3-
## **Date**: June 1, 2024
3+
## **Date**: June 2, 2024
44
**Difficulty**: ![Easy](https://img.shields.io/badge/Easy-Green)
5-
**Related Topics**: ![String](https://img.shields.io/badge/String-blue)
5+
**Related Topics**: ![String](https://img.shields.io/badge/String-blue) ![TwoPointer](https://img.shields.io/badge/TwoPointer-blue)
66

77
<p align="left">
88
<a href="https://github.com/Hasheditz/Leetcode-CSES-GFG-Codeforces-Coding-Solutions?tab=readme-ov-file#score-of-a-string" style="margin-right: 5px;">
99
<img src="https://img.shields.io/badge/All%20Problem%20Solutions-green" alt="All Problem Solutions">
1010
</a>
11-
<a href="https://leetcode.com/problems/score-of-a-string/">
11+
<a href="https://leetcode.com/problems/reverse-string/">
1212
<img src="https://img.shields.io/badge/Link%20To%20The%20Question-blue" alt="Link To The Question">
1313
</a>
1414
</p>
1515

1616
## Editorial
1717

18-
This problem requires us to calculate a score based on the absolute differences between adjacent characters in a given string. The challenge is to achieve this in an efficient manner.
18+
This problem requires us to Write a function that reverses a string. The input string is given as an array of characters s.
1919

2020
### Solution Explanation
2121

22-
To solve this problem, we can iterate through the string and calculate the absolute difference between each pair of adjacent characters. Here’s a step-by-step explanation of the approach:
22+
To solve this problem, we can iterate through the string `n/2` times and `swap` the values.
2323

2424
#### Key Steps:
25-
1. **Initialization**: Initialize a variable `res` to 0 to store the cumulative score.
26-
2. **Iterate Through the String**: Traverse each character in the string except the last one.
27-
3. **Calculate Absolute Differences**: For each character, compute the absolute difference with the next character and add this difference to `res`.
28-
4. **Return the Result**: After processing all adjacent pairs, return `res` as the final score.
25+
1. **Start Iterating** from `index 0` to `n/2` and we can go to the last index by saying `last = n-i-1`.
26+
2. **Swap** the `curr` index with the `last` index.
27+
3. **Gven function is a void function so, we do not have to return anything just modify the `vector of chars`
2928

3029
### Code
3130

3231
```cpp
3332
class Solution {
3433
public:
35-
int scoreOfString(string s) {
36-
37-
int res = 0;
34+
void reverseString(vector<char>& s) {
35+
3836
int n = s.size();
3937

40-
for(int i = 0; i < n-1; i++) {
41-
res += abs(s[i] - s[i+1]);
38+
for(int i = 0 ; i < n/2 ; i++)
39+
{
40+
swap(s[i] , s[n-i-1]);
4241
}
43-
44-
return res;
4542
}
4643
};
4744
```
48-
### Explanation of Code
49-
50-
#### Initialization:
51-
- `res` is initialized to 0, which will hold the final score.
52-
- `n` is assigned the size of the input string `s`.
53-
54-
#### Iterate Through the String:
55-
- A `for` loop is used to iterate through the string from the first character to the second-to-last character.
56-
57-
#### Calculate Absolute Differences:
58-
- For each character in the string, the absolute difference between the current character `s[i]` and the next character `s[i+1]` is calculated using `abs(s[i] - s[i+1])`.
59-
- This difference is added to `res`.
60-
61-
#### Return the Result:
62-
- After the loop completes, `res` holds the total score, which is then returned.
63-
64-
This approach ensures an efficient calculation of the score based on the sum of the absolute differences between each pair of adjacent characters in the string.
65-
6645
## Like and Upvote
6746

6847
If you found this solution helpful, please consider liking 👍 and upvoting ⬆️. Your support helps me to keep providing high-quality solutions.

0 commit comments

Comments
 (0)