File tree Expand file tree Collapse file tree 6 files changed +269
-2
lines changed
solution/1000-1099/1055.Shortest Way to Form String Expand file tree Collapse file tree 6 files changed +269
-2
lines changed Original file line number Diff line number Diff line change 49
49
50
50
<!-- 这里可写通用的实现逻辑 -->
51
51
52
+ ** 方法一:双指针**
53
+
54
+ 我们可以使用双指针的方法,用指针 $j$ 指向目标字符串 ` target ` ,然后遍历源字符串 ` source ` ,用指针 $i$ 指向源字符串 ` source ` ,如果 $source[ i] = target[ j] $,则 $i$ 和 $j$ 同时向后移动一位,否则只移动 $i$ 指针。当指针 $i$ 和 $j$ 都到达字符串末尾时,如果没有找到相等的字符,则返回 $-1$,否则子序列数量加一,然后将指针 $i$ 置为 $0$,继续遍历。
55
+
56
+ 遍历结束后,返回子序列数量即可。
57
+
58
+ 时间复杂度 $O(m \times n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为字符串 ` source ` 和 ` target ` 的长度。
59
+
52
60
<!-- tabs:start -->
53
61
54
62
### ** Python3**
55
63
56
64
<!-- 这里可写当前语言的特殊实现逻辑 -->
57
65
58
66
``` python
59
-
67
+ class Solution :
68
+ def shortestWay (self , source : str , target : str ) -> int :
69
+ def f (i , j ):
70
+ while i < m and j < n:
71
+ if source[i] == target[j]:
72
+ j += 1
73
+ i += 1
74
+ return j
75
+
76
+ m, n = len (source), len (target)
77
+ ans = j = 0
78
+ while j < n:
79
+ k = f(0 , j)
80
+ if k == j:
81
+ return - 1
82
+ j = k
83
+ ans += 1
84
+ return ans
60
85
```
61
86
62
87
### ** Java**
63
88
64
89
<!-- 这里可写当前语言的特殊实现逻辑 -->
65
90
66
91
``` java
92
+ class Solution {
93
+ public int shortestWay (String source , String target ) {
94
+ int m = source. length(), n = target. length();
95
+ int ans = 0 , j = 0 ;
96
+ while (j < n) {
97
+ int i = 0 ;
98
+ boolean ok = false ;
99
+ while (i < m && j < n) {
100
+ if (source. charAt(i) == target. charAt(j)) {
101
+ ok = true ;
102
+ ++ j;
103
+ }
104
+ ++ i;
105
+ }
106
+ if (! ok) {
107
+ return - 1 ;
108
+ }
109
+ ++ ans;
110
+ }
111
+ return ans;
112
+ }
113
+ }
114
+ ```
115
+
116
+ ### ** C++**
117
+
118
+ ``` cpp
119
+ class Solution {
120
+ public:
121
+ int shortestWay(string source, string target) {
122
+ int m = source.size(), n = target.size();
123
+ int ans = 0, j = 0;
124
+ while (j < n) {
125
+ int i = 0;
126
+ bool ok = false;
127
+ while (i < m && j < n) {
128
+ if (source[ i] == target[ j] ) {
129
+ ok = true;
130
+ ++j;
131
+ }
132
+ ++i;
133
+ }
134
+ if (!ok) {
135
+ return -1;
136
+ }
137
+ ++ans;
138
+ }
139
+ return ans;
140
+ }
141
+ };
142
+ ```
67
143
144
+ ### **Go**
145
+
146
+ ```go
147
+ func shortestWay(source string, target string) int {
148
+ m, n := len(source), len(target)
149
+ ans, j := 0, 0
150
+ for j < n {
151
+ ok := false
152
+ for i := 0; i < m && j < n; i++ {
153
+ if source[i] == target[j] {
154
+ ok = true
155
+ j++
156
+ }
157
+ }
158
+ if !ok {
159
+ return -1
160
+ }
161
+ ans++
162
+ }
163
+ return ans
164
+ }
68
165
```
69
166
70
167
### ** ...**
Original file line number Diff line number Diff line change 48
48
### ** Python3**
49
49
50
50
``` python
51
-
51
+ class Solution :
52
+ def shortestWay (self , source : str , target : str ) -> int :
53
+ def f (i , j ):
54
+ while i < m and j < n:
55
+ if source[i] == target[j]:
56
+ j += 1
57
+ i += 1
58
+ return j
59
+
60
+ m, n = len (source), len (target)
61
+ ans = j = 0
62
+ while j < n:
63
+ k = f(0 , j)
64
+ if k == j:
65
+ return - 1
66
+ j = k
67
+ ans += 1
68
+ return ans
52
69
```
53
70
54
71
### ** Java**
55
72
56
73
``` java
74
+ class Solution {
75
+ public int shortestWay (String source , String target ) {
76
+ int m = source. length(), n = target. length();
77
+ int ans = 0 , j = 0 ;
78
+ while (j < n) {
79
+ int i = 0 ;
80
+ boolean ok = false ;
81
+ while (i < m && j < n) {
82
+ if (source. charAt(i) == target. charAt(j)) {
83
+ ok = true ;
84
+ ++ j;
85
+ }
86
+ ++ i;
87
+ }
88
+ if (! ok) {
89
+ return - 1 ;
90
+ }
91
+ ++ ans;
92
+ }
93
+ return ans;
94
+ }
95
+ }
96
+ ```
97
+
98
+ ### ** C++**
99
+
100
+ ``` cpp
101
+ class Solution {
102
+ public:
103
+ int shortestWay(string source, string target) {
104
+ int m = source.size(), n = target.size();
105
+ int ans = 0, j = 0;
106
+ while (j < n) {
107
+ int i = 0;
108
+ bool ok = false;
109
+ while (i < m && j < n) {
110
+ if (source[ i] == target[ j] ) {
111
+ ok = true;
112
+ ++j;
113
+ }
114
+ ++i;
115
+ }
116
+ if (!ok) {
117
+ return -1;
118
+ }
119
+ ++ans;
120
+ }
121
+ return ans;
122
+ }
123
+ };
124
+ ```
57
125
126
+ ### **Go**
127
+
128
+ ```go
129
+ func shortestWay(source string, target string) int {
130
+ m, n := len(source), len(target)
131
+ ans, j := 0, 0
132
+ for j < n {
133
+ ok := false
134
+ for i := 0; i < m && j < n; i++ {
135
+ if source[i] == target[j] {
136
+ ok = true
137
+ j++
138
+ }
139
+ }
140
+ if !ok {
141
+ return -1
142
+ }
143
+ ans++
144
+ }
145
+ return ans
146
+ }
58
147
```
59
148
60
149
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int shortestWay (string source, string target) {
4
+ int m = source.size (), n = target.size ();
5
+ int ans = 0 , j = 0 ;
6
+ while (j < n) {
7
+ int i = 0 ;
8
+ bool ok = false ;
9
+ while (i < m && j < n) {
10
+ if (source[i] == target[j]) {
11
+ ok = true ;
12
+ ++j;
13
+ }
14
+ ++i;
15
+ }
16
+ if (!ok) {
17
+ return -1 ;
18
+ }
19
+ ++ans;
20
+ }
21
+ return ans;
22
+ }
23
+ };
Original file line number Diff line number Diff line change
1
+ func shortestWay (source string , target string ) int {
2
+ m , n := len (source ), len (target )
3
+ ans , j := 0 , 0
4
+ for j < n {
5
+ ok := false
6
+ for i := 0 ; i < m && j < n ; i ++ {
7
+ if source [i ] == target [j ] {
8
+ ok = true
9
+ j ++
10
+ }
11
+ }
12
+ if ! ok {
13
+ return - 1
14
+ }
15
+ ans ++
16
+ }
17
+ return ans
18
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int shortestWay (String source , String target ) {
3
+ int m = source .length (), n = target .length ();
4
+ int ans = 0 , j = 0 ;
5
+ while (j < n ) {
6
+ int i = 0 ;
7
+ boolean ok = false ;
8
+ while (i < m && j < n ) {
9
+ if (source .charAt (i ) == target .charAt (j )) {
10
+ ok = true ;
11
+ ++j ;
12
+ }
13
+ ++i ;
14
+ }
15
+ if (!ok ) {
16
+ return -1 ;
17
+ }
18
+ ++ans ;
19
+ }
20
+ return ans ;
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def shortestWay (self , source : str , target : str ) -> int :
3
+ def f (i , j ):
4
+ while i < m and j < n :
5
+ if source [i ] == target [j ]:
6
+ j += 1
7
+ i += 1
8
+ return j
9
+
10
+ m , n = len (source ), len (target )
11
+ ans = j = 0
12
+ while j < n :
13
+ k = f (0 , j )
14
+ if k == j :
15
+ return - 1
16
+ j = k
17
+ ans += 1
18
+ return ans
You can’t perform that action at this time.
0 commit comments