Skip to content

Commit 102685d

Browse files
committed
solve problem Rotate String
1 parent 9dc83f1 commit 102685d

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ All solutions will be accepted!
119119
|203|[Remove Linked List Elements](https://leetcode-cn.com/problems/remove-linked-list-elements/description/)|[java/py/js](./algorithms/RemoveLinkedListElements)|Easy|
120120
|237|[Delete Node In A Linked List](https://leetcode-cn.com/problems/delete-node-in-a-linked-list/description/)|[java/py/js](./algorithms/DeleteNodeInALinkedList)|Easy|
121121
|405|[Convert A Number To Hexadecimal](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/description/)|[java/py/js](./algorithms/ConvertANumberToHexadecimal)|Easy|
122+
|796|[Rotate String](https://leetcode-cn.com/problems/rotate-string/description/)|[java/py/js](./algorithms/RotateString)|Easy|
122123

123124
# Database
124125
|#|Title|Solution|Difficulty|

algorithms/RotateString/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Rotate String
2+
This problem is easy to solve

algorithms/RotateString/Solution.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public boolean rotateString(String A, String B) {
3+
if (A.length() != B.length()) return false;
4+
5+
int i = 0;
6+
while (i <= A.length()) {
7+
if (A.equals(B)) return true;
8+
A = A.substring(1) + A.substring(0, 1);
9+
i++;
10+
}
11+
12+
return false;
13+
}
14+
}

algorithms/RotateString/solution.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {string} A
3+
* @param {string} B
4+
* @return {boolean}
5+
*/
6+
var rotateString = function(A, B) {
7+
if (A.length !== B.length) return false
8+
9+
let i = 0
10+
while (i <= A.length) {
11+
if (A === B) return true
12+
A = A.substr(1) + A.substr(0, 1)
13+
i++
14+
}
15+
16+
return false
17+
};

algorithms/RotateString/solution.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def rotateString(self, A, B):
3+
"""
4+
:type A: str
5+
:type B: str
6+
:rtype: bool
7+
"""
8+
if len(A) != len(B):
9+
return False
10+
11+
i = 0
12+
while i <= len(A):
13+
if A == B:
14+
return True
15+
A = A[1:] + A[0]
16+
i += 1
17+
18+
return False

0 commit comments

Comments
 (0)