From a3b677bb100217aa09d27ed658a3e2f088059d50 Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Tue, 28 May 2024 07:55:04 +0100 Subject: [PATCH 1/2] Swift implementation for LCOF 50 --- .../README.md" | 23 +++++++++++++++++++ .../Solution.swift" | 18 +++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23050. \347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246/Solution.swift" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23050. \347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23050. \347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246/README.md" index 0a67201958f71..eb55361a7a0e7 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23050. \347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23050. \347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246/README.md" @@ -193,6 +193,29 @@ public class Solution { } ``` +#### Swift + +```swift +class Solution { + func firstUniqChar(_ s: String) -> Character { + var count = [Int](repeating: 0, count: 26) + let aAsciiValue = Int(Character("a").asciiValue!) + + for char in s { + count[Int(char.asciiValue!) - aAsciiValue] += 1 + } + + for char in s { + if count[Int(char.asciiValue!) - aAsciiValue] == 1 { + return char + } + } + + return " " + } +} +``` + diff --git "a/lcof/\351\235\242\350\257\225\351\242\23050. \347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23050. \347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246/Solution.swift" new file mode 100644 index 0000000000000..3bede9a82792e --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23050. \347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246/Solution.swift" @@ -0,0 +1,18 @@ +class Solution { + func firstUniqChar(_ s: String) -> Character { + var count = [Int](repeating: 0, count: 26) + let aAsciiValue = Int(Character("a").asciiValue!) + + for char in s { + count[Int(char.asciiValue!) - aAsciiValue] += 1 + } + + for char in s { + if count[Int(char.asciiValue!) - aAsciiValue] == 1 { + return char + } + } + + return " " + } +} \ No newline at end of file From 66c54f2b7712bd245e225d0718700ac83a726e39 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 28 May 2024 09:38:17 +0000 Subject: [PATCH 2/2] style: format code and docs with prettier --- .../README.md" | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23050. \347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23050. \347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246/README.md" index eb55361a7a0e7..bd133216c1647 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23050. \347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23050. \347\254\254\344\270\200\344\270\252\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\345\255\227\347\254\246/README.md" @@ -200,17 +200,17 @@ class Solution { func firstUniqChar(_ s: String) -> Character { var count = [Int](repeating: 0, count: 26) let aAsciiValue = Int(Character("a").asciiValue!) - + for char in s { count[Int(char.asciiValue!) - aAsciiValue] += 1 } - + for char in s { if count[Int(char.asciiValue!) - aAsciiValue] == 1 { return char } } - + return " " } }