Skip to content

Commit 1ae0ee1

Browse files
[LEET-541] add 541
1 parent a1c0a1d commit 1ae0ee1

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

leetcode-algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Algorithms
44
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
55
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
6+
|541|[Reverse String II](https://leetcode.com/problems/reverse-string-ii/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/ReverseStringII.java) | O(n) |O(1) | Easy | String
67
|540|[Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/SingleElementinaSortedArray.java) | O(n) |O(1) | Medium |
78
|535|[Encode and Decode TinyURL](https://leetcode.com/problems/encode-and-decode-tinyurl/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/EncodeandDecodeTinyURL.java) | O(1) |O(n) | Medium | Design
89
|532|[K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/)|[Solution](../../master/leetcode-algorithms/src/main/java/com/stevesun/solutions/KdiffPairsinanArray.java) | O(n) |O(n) | Easy | HashMap
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.stevesun.solutions;
2+
3+
/**
4+
* Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string.
5+
* If there are less than k characters left, reverse all of them.
6+
* If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.
7+
8+
Example:
9+
Input: s = "abcdefg", k = 2
10+
Output: "bacdfeg"
11+
12+
Restrictions:
13+
The string consists of lower English letters only.
14+
Length of the given string and k will in the range [1, 10000]
15+
*/
16+
public class ReverseStringII {
17+
18+
public String reverseStr(String s, int k) {
19+
StringBuilder stringBuilder = new StringBuilder();
20+
for (int i = 0; i < s.length(); i = i + 2 * k) {
21+
if (s.length() >= (i + k)) stringBuilder.append(new StringBuilder(s.substring(i, i + k)).reverse());
22+
else {
23+
stringBuilder.append(new StringBuilder(s.substring(i)).reverse());
24+
break;
25+
}
26+
if ((i + 2 * k) <= s.length()) stringBuilder.append(s.substring(i + k, i + 2 * k));
27+
else stringBuilder.append(s.substring(i + k));
28+
}
29+
return stringBuilder.toString();
30+
}
31+
32+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions.ReverseStringII;
4+
import org.junit.Before;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static junit.framework.Assert.assertEquals;
9+
10+
public class ReverseStringIITest {
11+
private static ReverseStringII test;
12+
private static String expected;
13+
private static String actual;
14+
private static String s;
15+
private static int k;
16+
17+
@BeforeClass
18+
public static void setup(){
19+
test = new ReverseStringII();
20+
}
21+
22+
@Before
23+
public void setupForEachTest(){}
24+
25+
@Test
26+
public void test1(){
27+
s = "abcd";
28+
k = 5;
29+
expected = "dcba";
30+
actual = test.reverseStr(s, k);
31+
assertEquals(expected, actual);
32+
}
33+
34+
@Test
35+
public void test2(){
36+
s = "abcdefg";
37+
k = 2;
38+
expected = "bacdfeg";
39+
actual = test.reverseStr(s, k);
40+
assertEquals(expected, actual);
41+
}
42+
43+
@Test
44+
public void test3(){
45+
s = "abcd";
46+
k = 4;
47+
expected = "dcba";
48+
actual = test.reverseStr(s, k);
49+
assertEquals(expected, actual);
50+
}
51+
52+
@Test
53+
public void test4(){
54+
s = "abcdefg";
55+
k = 3;
56+
expected = "cbadefg";
57+
actual = test.reverseStr(s, k);
58+
assertEquals(expected, actual);
59+
}
60+
61+
@Test
62+
public void test5(){
63+
s = "abcd";
64+
k = 3;
65+
expected = "cbad";
66+
actual = test.reverseStr(s, k);
67+
assertEquals(expected, actual);
68+
}
69+
70+
@Test
71+
public void test6(){
72+
s = "hyzqyljrnigxvdtneasepfahmtyhlohwxmkqcdfehybknvdmfrfvtbsovjbdhevlfxpdaovjgunjqlimjkfnqcqnajmebeddqsgl";
73+
System.out.println("s.length() = " + s.length());
74+
k = 39;
75+
expected = "fdcqkmxwholhytmhafpesaentdvxginrjlyqzyhehybknvdmfrfvtbsovjbdhevlfxpdaovjgunjqllgsqddebemjanqcqnfkjmi";
76+
actual = test.reverseStr(s, k);
77+
assertEquals(expected, actual);
78+
}
79+
80+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy