Skip to content

Commit 89c29ee

Browse files
committed
solved 344. Reverse String
Signed-off-by: rajput-hemant <[email protected]>
1 parent c6af622 commit 89c29ee

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

Diff for: README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
<img alt="Leetcode">
1515
</picture>
1616

17+
## 🏆 Curated solutions to Leetcode problems in multiple languages to ace the Coding Interviews.
18+
1719
### Press <kbd>Ctrl</kbd>+<kbd>F</kbd> or <kbd>⌘</kbd>+<kbd>F</kbd> to search for a specific problem
1820

1921
## Array
@@ -34,6 +36,7 @@
3436
| # | Solution | Tags | Difficulty | Remark |
3537
| :------: | :-----------------------------------------------------------: | :----------------------------------: | :---------: | :----: |
3638
| **1047** | [Remove All Adjacent Duplicates In String][1047] | String, Stack | ![][easy] | |
39+
| **0344** | [Reverse String][344] | Two Pointers, String, Recursion | ![][easy] | |
3740
| **1461** | [Check If a String Contains All Binary Codes of Size K][1461] | String, Hash Table, Bit Manipulation | ![][medium] | |
3841

3942
## Hash Table
@@ -155,6 +158,7 @@
155158
| **0004** | [Median of Two Sorted Arrays][4] | Array, Binary Search, Divide & Conquer | ![][hard] | |
156159
| **0075** | [Sort Colors][75] | Array, Two Pointers, Sorting | ![][medium] | |
157160
| **0283** | [Move Zeroes][283] | Array, Two Pointers | ![][easy] | |
161+
| **0344** | [Reverse String][344] | Two Pointers, String, Recursion | ![][easy] | |
158162
| **0653** | [Two Sum IV - Input is a BST ][653] | Tree, DFS, BST, Binary Tree | ![][easy] | |
159163
| **1537** | [Get the Maximum Score][1537] | Array, Two Pointer, Dynamic Programming, Greedy | ![][hard] | |
160164

@@ -256,9 +260,10 @@
256260

257261
## Recursion
258262

259-
| # | Solution | Tags | Difficulty | Remark |
260-
| :------: | :-----------------------: | :--------------------: | :---------: | :----: |
261-
| **0024** | [Swap Nodes in Pairs][24] | Linked List, Recursion | ![][medium] | |
263+
| # | Solution | Tags | Difficulty | Remark |
264+
| :------: | :-----------------------: | :-----------------------------: | :---------: | :----: |
265+
| **0024** | [Swap Nodes in Pairs][24] | Linked List, Recursion | ![][medium] | |
266+
| **0344** | [Reverse String][344] | Two Pointers, String, Recursion | ![][easy] | |
262267

263268
<!--
264269
## Trie
@@ -568,6 +573,7 @@
568573
[222]: ./src/0201-0300/222%20-%20Count%20Complete%20Tree%20Nodes/
569574
[230]: ./src/0201-0300/230%20-%20Kth%20Smallest%20Element%20in%20a%20BST/
570575
[283]: ./src/0201-0300/283%20-%20Move%20Zeroes/
576+
[344]: ./src/0301-0400/344%20-%20Reverse%20String/
571577
[369]: ./src/0301-0400/369%20-%20Plus%20One%20Linked%20List/
572578
[543]: ./src/0501-0600/543%20-%20Diameter%20of%20Binary%20Tree/
573579
[653]: ./src/0601-0700/653%20-%20Two%20Sum%20IV%20-%20Input%20is%20a%20BST/

Diff for: src/0301-0400/344 - Reverse String/README.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 344. Reverse String [![share]](https://leetcode.com/problems/reverse-string)
2+
3+
![][easy]
4+
5+
## Problem Statement:
6+
7+
Write a function that reverses a string. The input string is given as an array of characters s.
8+
9+
You must do this by modifying the input array `in-place` with `O(1)` extra memory.
10+
11+
### Example 1:
12+
13+
```
14+
Input: s = ["h","e","l","l","o"]
15+
Output: ["o","l","l","e","h"]
16+
```
17+
18+
### Example 2:
19+
20+
```
21+
Input: s = ["H","a","n","n","a","h"]
22+
Output: ["h","a","n","n","a","H"]
23+
```
24+
25+
### Constraints:
26+
27+
- 1 <= s.length <= 10<sup>5<sup>
28+
- s[i] is a printable ascii character.
29+
30+
## Solutions:
31+
32+
### [_Java_](./ReverseString.java)
33+
34+
```java
35+
public void reverseString(char[] s) {
36+
for (int left = 0, right = s.length - 1; left < right; left++, right--)
37+
swap(s, left, right);
38+
}
39+
40+
private void swap(char[] s, int a, int b) {
41+
char temp = s[a];
42+
s[a] = s[b];
43+
s[b] = temp;
44+
}
45+
```
46+
47+
### [_..._]()
48+
49+
```
50+
51+
```
52+
53+
<!----------------------------------{ link }--------------------------------->
54+
55+
[share]: https://img.icons8.com/external-anggara-blue-anggara-putra/20/000000/external-share-user-interface-basic-anggara-blue-anggara-putra-2.png
56+
[easy]: https://img.shields.io/badge/Difficulty-Easy-yellow.svg
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class ReverseString {
2+
public void reverseString(char[] s) {
3+
for (int left = 0, right = s.length - 1; left < right; left++, right--)
4+
swap(s, left, right);
5+
}
6+
7+
private void swap(char[] s, int a, int b) {
8+
char temp = s[a];
9+
s[a] = s[b];
10+
s[b] = temp;
11+
}
12+
}

Diff for: src/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<img alt="Leetcode">
77
</picture>
88

9+
## 🏆 Curated solutions to Leetcode problems in multiple languages to ace the Coding Interviews.
10+
911
### Press <kbd>Ctrl</kbd>+<kbd>F</kbd> or <kbd>⌘</kbd>+<kbd>F</kbd> to search for a specific problem
1012

1113
| # | Solution | Tag | Difficulty | Remark |
@@ -29,6 +31,7 @@
2931
| **0222** | [Count Complete Tree Nodes][222] | Binary Search, Tree, DFS, Binary Tree | ![][medium] | |
3032
| **0230** | [Kth Smallest Element in a BST][230] | Tree, DFS, BST, Binary Tree | ![][medium] | |
3133
| **0283** | [Move Zeroes][283] | Array, Two Pointers | ![][easy] | |
34+
| **0344** | [Reverse String][344] | Two Pointers, String, Recursion | ![][easy] | |
3235
| **0369** | [Plus One Linked List][369] | Linked List, Math | ![][medium] | 🔒 |
3336
| **0543** | [Diameter of Binary Tree][543] | Tree, DFS, Binary Tree | ![][easy] | |
3437
| **0653** | [Two Sum IV - Input is a BST][653] | Tree, DFS, BST, Binary Tree | ![][easy] | |
@@ -60,6 +63,7 @@
6063
[222]: ./0201-0300/222%20-%20Count%20Complete%20Tree%20Nodes/
6164
[230]: ./0201-0300/230%20-%20Kth%20Smallest%20Element%20in%20a%20BST/
6265
[283]: ./0201-0300/283%20-%20Move%20Zeroes/
66+
[344]: ./0301-0400/344%20-%20Reverse%20String/
6367
[369]: ./0301-0400/369%20-%20Plus%20One%20Linked%20List/
6468
[543]: ./0501-0600/543%20-%20Diameter%20of%20Binary%20Tree/
6569
[653]: ./0601-0700/653%20-%20Two%20Sum%20IV%20-%20Input%20is%20a%20BST/

0 commit comments

Comments
 (0)