|
1 | | -# 3110. Score of a String |
| 1 | +# 344. Reverse String |
2 | 2 |
|
3 | | -## **Date**: June 1, 2024 |
| 3 | +## **Date**: June 2, 2024 |
4 | 4 | **Difficulty**:  |
5 | | -**Related Topics**:  |
| 5 | +**Related Topics**:   |
6 | 6 |
|
7 | 7 | <p align="left"> |
8 | 8 | <a href="https://github.com/Hasheditz/Leetcode-CSES-GFG-Codeforces-Coding-Solutions?tab=readme-ov-file#score-of-a-string" style="margin-right: 5px;"> |
9 | 9 | <img src="https://img.shields.io/badge/All%20Problem%20Solutions-green" alt="All Problem Solutions"> |
10 | 10 | </a> |
11 | | - <a href="https://leetcode.com/problems/score-of-a-string/"> |
| 11 | + <a href="https://leetcode.com/problems/reverse-string/"> |
12 | 12 | <img src="https://img.shields.io/badge/Link%20To%20The%20Question-blue" alt="Link To The Question"> |
13 | 13 | </a> |
14 | 14 | </p> |
15 | 15 |
|
16 | 16 | ## Editorial |
17 | 17 |
|
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. |
19 | 19 |
|
20 | 20 | ### Solution Explanation |
21 | 21 |
|
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. |
23 | 23 |
|
24 | 24 | #### 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` |
29 | 28 |
|
30 | 29 | ### Code |
31 | 30 |
|
32 | 31 | ```cpp |
33 | 32 | class Solution { |
34 | 33 | public: |
35 | | - int scoreOfString(string s) { |
36 | | - |
37 | | - int res = 0; |
| 34 | + void reverseString(vector<char>& s) { |
| 35 | + |
38 | 36 | int n = s.size(); |
39 | 37 |
|
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]); |
42 | 41 | } |
43 | | - |
44 | | - return res; |
45 | 42 | } |
46 | 43 | }; |
47 | 44 | ``` |
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 | | - |
66 | 45 | ## Like and Upvote |
67 | 46 |
|
68 | 47 | If you found this solution helpful, please consider liking 👍 and upvoting ⬆️. Your support helps me to keep providing high-quality solutions. |
|
0 commit comments