Skip to content

Commit 42aebad

Browse files
committed
[String] Optimize the code style of Word Pattern and add a new solution to Find Duplicate File in System
1 parent 2d894ff commit 42aebad

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* [Microsoft](#microsoft)
2929

3030
## Progress
31-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 255 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
31+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 256 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3232

3333

3434
## Array
@@ -99,6 +99,7 @@
9999
[Valid Anagram](https://leetcode.com/problems/valid-anagram/)| [Swift](./String/ValidAnagram.swift)| Easy| O(nlogn)| O(1)|
100100
[Ransom Note](https://leetcode.com/problems/ransom-note/)| [Swift](./String/RansomNote.swift)| Easy| O(n)| O(n)|
101101
[Group Anagrams](https://leetcode.com/problems/anagrams/)| [Swift](./String/GroupAnagrams.swift)| Medium| O(nmlogm + nlogn)| O(n)
102+
[Find Duplicate File in System](https://leetcode.com/problems/find-duplicate-file-in-system/)| [Swift](./String/FindDuplicateFileInSystem.swift)| Medium| O(nm)| O(n)
102103
[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)| [Swift](./String/LongestCommonPrefix.swift)| Easy| O(nm)| O(m)|
103104
[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)| [Swift](./String/LongestSubstringWithoutRepeatingCharacters.swift)| Medium| O(n)| O(n)|
104105
[One Edit Distance](https://leetcode.com/problems/one-edit-distance/)| [Swift](./String/OneEditDistance.swift)| Medium| O(n)| O(n)|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/find-duplicate-file-in-system/
3+
* Primary idea: Iterate the paths array and categories paths with the same content
4+
*
5+
* Time Complexity: O(nm), n stands for number of paths, m stands for file number in a path
6+
* Space Complexity: O(n)
7+
*/
8+
9+
class FindDuplicateFileInSystem {
10+
func findDuplicate(_ paths: [String]) -> [[String]] {
11+
var contentToFiles = [String: [String]]()
12+
13+
for path in paths {
14+
let params = path.split(separator: " ")
15+
16+
guard let dir = params.first else {
17+
continue
18+
}
19+
20+
for i in 1..<params.count {
21+
let fileParams = params[i].split(separator: "(")
22+
23+
guard let fileName = fileParams.first, let fileContentWithExtraInfo = fileParams.last else {
24+
continue
25+
}
26+
27+
let fileContent = String(describing: fileContentWithExtraInfo.dropLast())
28+
let filePath = String(describing: dir) + "/" + String(describing: fileName)
29+
30+
contentToFiles[fileContent] = contentToFiles[fileContent, default: []] + [filePath]
31+
}
32+
}
33+
34+
return contentToFiles.values.filter { $0.count >= 2 }
35+
}
36+
}

String/WordPattern.swift

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,24 @@
55
*/
66

77
class WordPattern {
8-
func wordPattern(pattern: String, _ str: String) -> Bool {
9-
var wordDict = [String: Character]()
10-
var charDict = [Character: String]()
11-
let strs = str.characters.split{$0 == " "}.map(String.init)
12-
let patterns = [Character](pattern.characters)
8+
func wordPattern(_ pattern: String, _ str: String) -> Bool {
9+
let strs = str.split(separator: " ").map(String.init)
1310

14-
guard patterns.count == strs.count else {
11+
guard pattern.count == strs.count else {
1512
return false
1613
}
1714

18-
for i in 0..<strs.count {
19-
let currentWord = strs[i]
20-
let currentChar = patterns[i]
15+
var patternToWord = [Character: String]()
16+
var wordToPattern = [String: Character]()
2117

22-
if wordDict[currentWord] == nil && charDict[currentChar] == nil{
23-
wordDict[currentWord] = currentChar
24-
charDict[currentChar] = currentWord
18+
for (i, char) in pattern.enumerated() {
19+
let word = strs[i]
20+
21+
if patternToWord[char] == nil && wordToPattern[word] == nil {
22+
wordToPattern[word] = char
23+
patternToWord[char] = word
2524
} else {
26-
if wordDict[currentWord] != currentChar {
25+
if patternToWord[char] != word || wordToPattern[word] != char {
2726
return false
2827
}
2928
}

0 commit comments

Comments
 (0)