-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lcci problems: No.01.04,01.05 (#2577)
- Loading branch information
Showing
6 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
func canPermutePalindrome(_ s: String) -> Bool { | ||
var cnt = [Character: Int]() | ||
for char in s { | ||
cnt[char, default: 0] += 1 | ||
} | ||
|
||
var sum = 0 | ||
for count in cnt.values { | ||
sum += count % 2 | ||
} | ||
|
||
return sum < 2 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class Solution { | ||
func oneEditAway(_ first: String, _ second: String) -> Bool { | ||
let m = first.count, n = second.count | ||
if m < n { | ||
return oneEditAway(second, first) | ||
} | ||
if m - n > 1 { | ||
return false | ||
} | ||
|
||
var cnt = 0 | ||
var firstIndex = first.startIndex | ||
var secondIndex = second.startIndex | ||
|
||
if m == n { | ||
while secondIndex != second.endIndex { | ||
if first[firstIndex] != second[secondIndex] { | ||
cnt += 1 | ||
if cnt > 1 { | ||
return false | ||
} | ||
} | ||
firstIndex = first.index(after: firstIndex) | ||
secondIndex = second.index(after: secondIndex) | ||
} | ||
return true | ||
} else { | ||
while firstIndex != first.endIndex { | ||
if secondIndex == second.endIndex || (secondIndex != second.endIndex && first[firstIndex] != second[secondIndex]) { | ||
cnt += 1 | ||
} else { | ||
secondIndex = second.index(after: secondIndex) | ||
} | ||
firstIndex = first.index(after: firstIndex) | ||
} | ||
} | ||
return cnt < 2 | ||
} | ||
} |