-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3083-ExistenceOfASubstringInAStringAndItsReverse.go
More file actions
59 lines (50 loc) · 1.83 KB
/
3083-ExistenceOfASubstringInAStringAndItsReverse.go
File metadata and controls
59 lines (50 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
// 3083. Existence of a Substring in a String and Its Reverse
// Given a string s, find any substring of length 2 which is also present in the reverse of s.
// Return true if such a substring exists, and false otherwise.
// Example 1:
// Input: s = "leetcode"
// Output: true
// Explanation: Substring "ee" is of length 2 which is also present in reverse(s) == "edocteel".
// Example 2:
// Input: s = "abcba"
// Output: true
// Explanation: All of the substrings of length 2 "ab", "bc", "cb", "ba" are also present in reverse(s) == "abcba".
// Example 3:
// Input: s = "abcd"
// Output: false
// Explanation: There is no substring of length 2 in s, which is also present in the reverse of s.
// Constraints:
// 1 <= s.length <= 100
// s consists only of lowercase English letters.
import "fmt"
import "strings"
func isSubstringPresent(s string) bool {
reverse := ""
for i := len(s) - 1; i >= 0; i-- {// 翻转
reverse += string(s[i])
}
for i := 0; i < len(reverse) - 1; i++ {
if strings.Contains(s, reverse[i:i+2]) { // 判断是否包含其中一部分
return true
}
}
return false
}
func main() {
// Example 1:
// Input: s = "leetcode"
// Output: true
// Explanation: Substring "ee" is of length 2 which is also present in reverse(s) == "edocteel".
fmt.Println(isSubstringPresent("leetcode")) // true
// Example 2:
// Input: s = "abcba"
// Output: true
// Explanation: All of the substrings of length 2 "ab", "bc", "cb", "ba" are also present in reverse(s) == "abcba".
fmt.Println(isSubstringPresent("abcba")) // true
// Example 3:
// Input: s = "abcd"
// Output: false
// Explanation: There is no substring of length 2 in s, which is also present in the reverse of s.
fmt.Println(isSubstringPresent("abcd")) // false
}