Skip to content

Commit c98a31f

Browse files
committedMar 11, 2018
add 796
1 parent 7d2e3b2 commit c98a31f

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|796|[Rotate String](https://leetcode.com/problems/rotate-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_796.java) | O(n) | O(1) | |Easy|
2526
|791|[Custom Sort String](https://leetcode.com/problems/custom-sort-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_791.java) | O(n+m) | O(1) | |Medium|
2627
|788|[Rotated Digits](https://leetcode.com/problems/rotated-digits/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_788.java) | O(n*m) | O(1) | |Easy|
2728
|784|[Letter Case Permutation](https://leetcode.com/problems/letter-case-permutation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_784.java) | O(n*2^n) | O(n*2^n) | |Easy|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 796. Rotate String
5+
6+
We are given two strings, A and B.
7+
8+
A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can become B after some number of shifts on A.
9+
10+
Example 1:
11+
Input: A = 'abcde', B = 'cdeab'
12+
Output: true
13+
14+
Example 2:
15+
Input: A = 'abcde', B = 'abced'
16+
Output: false
17+
18+
Note:
19+
20+
A and B will have length at most 100.
21+
*/
22+
public class _796 {
23+
public static class Solution1 {
24+
public boolean rotateString(String A, String B) {
25+
if (A.length() != B.length()) {
26+
return false;
27+
}
28+
for (int i = 0; i < A.length(); i++) {
29+
if ((A.substring(i) + A.substring(0, i)).equals(B)) {
30+
return true;
31+
}
32+
}
33+
return false;
34+
}
35+
}
36+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._796;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _796Test {
10+
private static _796.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _796.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.rotateString("abcde", "cdeab"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(false, solution1.rotateString("abcde", "abced"));
25+
}
26+
}

0 commit comments

Comments
 (0)
Please sign in to comment.