Skip to content

Commit 62ad3da

Browse files
committedMar 4, 2020
feat: add java solution to lcci problem: flipped-string
1 parent 59e45fa commit 62ad3da

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed
 

‎lcci/README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
├── 面试题 01.02. 判定是否互为字符重排
1515
│   ├── README.md
1616
│   └── Solution.py
17-
└── 面试题 01.03. URL化
17+
├── 面试题 01.03. URL化
18+
│   ├── README.md
19+
│   └── Solution.py
20+
└── 面试题 01.09. 字符串轮转
1821
├── README.md
19-
└── Solution.py
22+
└── Solution.java
2023
```
2124

2225
## 版权
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# [面试题 01.09. 字符串轮转](https://leetcode-cn.com/problems/flipped-string-lcci/)
2+
3+
## 题目描述
4+
字符串轮转。给定两个字符串 `s1``s2`,请编写代码检查 `s2` 是否为 `s1` 旋转而成(比如,`waterbottle``erbottlewat` 旋转后的字符串)。
5+
6+
**示例1:**
7+
8+
```
9+
输入:s1 = "waterbottle", s2 = "erbottlewat"
10+
输出:True
11+
```
12+
13+
**示例2:**
14+
```
15+
输入:s1 = "aa", "aba"
16+
输出:False
17+
```
18+
19+
**提示:**
20+
21+
1. 字符串长度在 `[0, 100000]` 范围内。
22+
23+
**说明:**
24+
25+
1. 你能只调用一次检查子串的方法吗?
26+
27+
## 解法
28+
### Python3
29+
```python
30+
31+
```
32+
33+
### Java
34+
```java
35+
class Solution {
36+
public boolean isFlipedString(String s1, String s2) {
37+
int len1 = s1.length(), len2 = s2.length();
38+
if (len1 != len2) {
39+
return false;
40+
}
41+
if ((len1 == 0 && len2 == 0) || (s1.equals(s2))) {
42+
return true;
43+
}
44+
45+
for (int i = 0; i < len1; ++i) {
46+
s1 = flip(s1);
47+
if (s1.equals(s2)) {
48+
return true;
49+
}
50+
}
51+
return false;
52+
53+
54+
}
55+
56+
private String flip(String s) {
57+
return s.substring(1) + s.charAt(0);
58+
}
59+
}
60+
```
61+
62+
### ...
63+
```
64+
65+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public boolean isFlipedString(String s1, String s2) {
3+
int len1 = s1.length(), len2 = s2.length();
4+
if (len1 != len2) {
5+
return false;
6+
}
7+
if ((len1 == 0 && len2 == 0) || (s1.equals(s2))) {
8+
return true;
9+
}
10+
11+
for (int i = 0; i < len1; ++i) {
12+
s1 = flip(s1);
13+
if (s1.equals(s2)) {
14+
return true;
15+
}
16+
}
17+
return false;
18+
19+
}
20+
21+
private String flip(String s) {
22+
return s.substring(1) + s.charAt(0);
23+
}
24+
}

0 commit comments

Comments
 (0)