File tree Expand file tree Collapse file tree 6 files changed +221
-2
lines changed
solution/1600-1699/1624.Largest Substring Between Two Equal Characters Expand file tree Collapse file tree 6 files changed +221
-2
lines changed Original file line number Diff line number Diff line change 52
52
53
53
<!-- 这里可写通用的实现逻辑 -->
54
54
55
+ ** 方法一:数组/哈希表**
56
+
57
+ 用数组或哈希表记录每个字符第一次出现的位置。
58
+
59
+ 遍历字符串每个字符 $c$,若 $c$ 在数组中的值为 $-1$,则更新为当前位置,否则答案更新为当前位置与数组中的值的差值的最大值。
60
+
61
+ 时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串长度,而 $C$ 为字符集大小。
62
+
55
63
<!-- tabs:start -->
56
64
57
65
### ** Python3**
58
66
59
67
<!-- 这里可写当前语言的特殊实现逻辑 -->
60
68
61
69
``` python
62
-
70
+ class Solution :
71
+ def maxLengthBetweenEqualCharacters (self , s : str ) -> int :
72
+ d = {}
73
+ ans = - 1
74
+ for i, c in enumerate (s):
75
+ if c in d:
76
+ ans = max (ans, i - d[c] - 1 )
77
+ else :
78
+ d[c] = i
79
+ return ans
63
80
```
64
81
65
82
### ** Java**
66
83
67
84
<!-- 这里可写当前语言的特殊实现逻辑 -->
68
85
69
86
``` java
87
+ class Solution {
88
+ public int maxLengthBetweenEqualCharacters (String s ) {
89
+ int [] d = new int [26 ];
90
+ Arrays . fill(d, - 1 );
91
+ int ans = - 1 ;
92
+ for (int i = 0 ; i < s. length(); ++ i) {
93
+ int j = s. charAt(i) - ' a' ;
94
+ if (d[j] == - 1 ) {
95
+ d[j] = i;
96
+ } else {
97
+ ans = Math . max(ans, i - d[j] - 1 );
98
+ }
99
+ }
100
+ return ans;
101
+ }
102
+ }
103
+ ```
104
+
105
+ ### ** C++**
106
+
107
+ ``` cpp
108
+ class Solution {
109
+ public:
110
+ int maxLengthBetweenEqualCharacters(string s) {
111
+ vector<int > d(26, -1);
112
+ int ans = -1;
113
+ for (int i = 0; i < s.size(); ++i) {
114
+ int j = s[ i] - 'a';
115
+ if (d[ j] == -1) {
116
+ d[ j] = i;
117
+ } else {
118
+ ans = max(ans, i - d[ j] - 1);
119
+ }
120
+ }
121
+ return ans;
122
+ }
123
+ };
124
+ ```
70
125
126
+ ### **Go**
127
+
128
+ ```go
129
+ func maxLengthBetweenEqualCharacters(s string) int {
130
+ d := make([]int, 26)
131
+ for i := range d {
132
+ d[i] = -1
133
+ }
134
+ ans := -1
135
+ for i, c := range s {
136
+ c -= 'a'
137
+ if d[c] == -1 {
138
+ d[c] = i
139
+ } else {
140
+ ans = max(ans, i-d[c]-1)
141
+ }
142
+ }
143
+ return ans
144
+ }
145
+
146
+ func max(a, b int) int {
147
+ if a > b {
148
+ return a
149
+ }
150
+ return b
151
+ }
71
152
```
72
153
73
154
### ** ...**
Original file line number Diff line number Diff line change 47
47
### ** Python3**
48
48
49
49
``` python
50
-
50
+ class Solution :
51
+ def maxLengthBetweenEqualCharacters (self , s : str ) -> int :
52
+ d = {}
53
+ ans = - 1
54
+ for i, c in enumerate (s):
55
+ if c in d:
56
+ ans = max (ans, i - d[c] - 1 )
57
+ else :
58
+ d[c] = i
59
+ return ans
51
60
```
52
61
53
62
### ** Java**
54
63
55
64
``` java
65
+ class Solution {
66
+ public int maxLengthBetweenEqualCharacters (String s ) {
67
+ int [] d = new int [26 ];
68
+ Arrays . fill(d, - 1 );
69
+ int ans = - 1 ;
70
+ for (int i = 0 ; i < s. length(); ++ i) {
71
+ int j = s. charAt(i) - ' a' ;
72
+ if (d[j] == - 1 ) {
73
+ d[j] = i;
74
+ } else {
75
+ ans = Math . max(ans, i - d[j] - 1 );
76
+ }
77
+ }
78
+ return ans;
79
+ }
80
+ }
81
+ ```
82
+
83
+ ### ** C++**
84
+
85
+ ``` cpp
86
+ class Solution {
87
+ public:
88
+ int maxLengthBetweenEqualCharacters(string s) {
89
+ vector<int > d(26, -1);
90
+ int ans = -1;
91
+ for (int i = 0; i < s.size(); ++i) {
92
+ int j = s[ i] - 'a';
93
+ if (d[ j] == -1) {
94
+ d[ j] = i;
95
+ } else {
96
+ ans = max(ans, i - d[ j] - 1);
97
+ }
98
+ }
99
+ return ans;
100
+ }
101
+ };
102
+ ```
56
103
104
+ ### **Go**
105
+
106
+ ```go
107
+ func maxLengthBetweenEqualCharacters(s string) int {
108
+ d := make([]int, 26)
109
+ for i := range d {
110
+ d[i] = -1
111
+ }
112
+ ans := -1
113
+ for i, c := range s {
114
+ c -= 'a'
115
+ if d[c] == -1 {
116
+ d[c] = i
117
+ } else {
118
+ ans = max(ans, i-d[c]-1)
119
+ }
120
+ }
121
+ return ans
122
+ }
123
+
124
+ func max(a, b int) int {
125
+ if a > b {
126
+ return a
127
+ }
128
+ return b
129
+ }
57
130
```
58
131
59
132
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int maxLengthBetweenEqualCharacters (string s) {
4
+ vector<int > d (26 , -1 );
5
+ int ans = -1 ;
6
+ for (int i = 0 ; i < s.size (); ++i) {
7
+ int j = s[i] - ' a' ;
8
+ if (d[j] == -1 ) {
9
+ d[j] = i;
10
+ } else {
11
+ ans = max (ans, i - d[j] - 1 );
12
+ }
13
+ }
14
+ return ans;
15
+ }
16
+ };
Original file line number Diff line number Diff line change
1
+ func maxLengthBetweenEqualCharacters (s string ) int {
2
+ d := make ([]int , 26 )
3
+ for i := range d {
4
+ d [i ] = - 1
5
+ }
6
+ ans := - 1
7
+ for i , c := range s {
8
+ c -= 'a'
9
+ if d [c ] == - 1 {
10
+ d [c ] = i
11
+ } else {
12
+ ans = max (ans , i - d [c ]- 1 )
13
+ }
14
+ }
15
+ return ans
16
+ }
17
+
18
+ func max (a , b int ) int {
19
+ if a > b {
20
+ return a
21
+ }
22
+ return b
23
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int maxLengthBetweenEqualCharacters (String s ) {
3
+ int [] d = new int [26 ];
4
+ Arrays .fill (d , -1 );
5
+ int ans = -1 ;
6
+ for (int i = 0 ; i < s .length (); ++i ) {
7
+ int j = s .charAt (i ) - 'a' ;
8
+ if (d [j ] == -1 ) {
9
+ d [j ] = i ;
10
+ } else {
11
+ ans = Math .max (ans , i - d [j ] - 1 );
12
+ }
13
+ }
14
+ return ans ;
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def maxLengthBetweenEqualCharacters (self , s : str ) -> int :
3
+ d = {}
4
+ ans = - 1
5
+ for i , c in enumerate (s ):
6
+ if c in d :
7
+ ans = max (ans , i - d [c ] - 1 )
8
+ else :
9
+ d [c ] = i
10
+ return ans
You can’t perform that action at this time.
0 commit comments