Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
asahiocean
LeetCode
Commits
31b05053
Unverified
Commit
31b05053
authored
Jul 21, 2021
by
ASAHI OCEAN
Committed by
GitHub
Jul 21, 2021
Browse files
Merge pull request #209 from asahiocean/develop
30. Substring with Concatenation of All Words
parents
f3a8602a
cabd6b9d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Hash Table/Substring with Concatenation of All Words/substring-with-concatenation-of-all-words.playground/Contents.swift
0 → 100644
View file @
31b05053
import
Foundation
// 30. Substring with Concatenation of All Words
// https://leetcode.com/problems/substring-with-concatenation-of-all-words/
// Discuss: https://vk.cc/c47cH8
class
Solution
{
func
findSubstring
(
_
s
:
String
,
_
words
:
[
String
])
->
[
Int
]
{
guard
!
(
s
.
isEmpty
)
||
!
(
words
.
isEmpty
)
else
{
return
[]
}
let
n
=
s
.
count
,
chars
=
Array
(
s
)
let
size
=
words
[
0
]
.
count
,
length
=
words
.
count
*
size
guard
n
>=
length
else
{
return
[]
}
var
wordDict
=
[[
Character
]:
Int
]()
words
.
forEach
{
let
arr
=
Array
(
$0
)
wordDict
[
arr
,
default
:
0
]
=
(
wordDict
[
arr
]
??
0
)
+
1
}
var
result
=
Array
<
Int
>
()
(
0
..<
size
)
.
forEach
{
var
l
=
$0
while
l
<=
(
n
-
length
)
{
var
r
=
(
l
+
length
)
var
dict
=
[[
Character
]:
Int
]()
var
equal
:
Bool
=
true
while
l
<
r
{
let
temp
=
Array
(
chars
[(
r
-
size
)
..<
r
])
dict
[
temp
]
=
(
dict
[
temp
]
??
0
)
+
1
if
dict
[
temp
]
!
>
(
wordDict
[
temp
]
??
0
)
{
equal
=
false
break
}
r
-=
size
}
if
equal
{
if
dict
==
wordDict
{
result
.
append
(
l
)
}
l
+=
size
}
else
{
l
=
r
}
}
}
return
result
}
}
import
XCTest
// Executed 3 tests, with 0 failures (0 unexpected) in 0.157 (0.160) seconds
class
Tests
:
XCTestCase
{
private
let
s
=
Solution
()
func
test0
()
{
let
res
=
s
.
findSubstring
(
"barfoothefoobarman"
,
[
"foo"
,
"bar"
])
XCTAssertEqual
(
res
,
[
0
,
9
])
}
func
test1
()
{
let
res
=
s
.
findSubstring
(
"wordgoodgoodgoodbestword"
,
[
"word"
,
"good"
,
"best"
,
"word"
])
XCTAssertEqual
(
res
,
[])
}
func
test2
()
{
let
res
=
s
.
findSubstring
(
"barfoofoobarthefoobarman"
,
[
"bar"
,
"foo"
,
"the"
])
XCTAssertEqual
(
res
,
[
6
,
9
,
12
])
}
}
Tests
.
defaultTestSuite
.
run
()
Hash Table/Substring with Concatenation of All Words/substring-with-concatenation-of-all-words.playground/contents.xcplayground
0 → 100644
View file @
31b05053
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground
version=
'5.0'
target-platform=
'ios'
>
<timeline
fileName=
'timeline.xctimeline'
/>
</playground>
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment