Guest User

Untitled

a guest
Oct 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. class LetterCasePermutation {
  2. func letterCasePermutation(_ S: String) -> [String] {
  3. var res:[String] = [S]
  4. helper(res: &res, s: S, num: 0)
  5. return S.count == 0 ? [""] : res
  6. }
  7.  
  8. func helper(res:inout [String] ,s:String, num:Int){
  9. var temp:[String] = s.map({String($0)})
  10. for i in num..<temp.count{
  11. if temp[i].unicodeScalars.first!.value > 64 && temp[i].unicodeScalars.first!.value < 91{
  12. let range = Range<String.Index>(s.index(s.startIndex, offsetBy: i)..<s.index(s.startIndex, offsetBy: i+1))
  13. res.append(s.replacingOccurrences(of: String(temp[i]), with: String(temp[i]).lowercased(), options: .regularExpression, range: range))
  14. helper(res: &res, s: res.last!, num: i+1)
  15.  
  16. }else if temp[i].unicodeScalars.first!.value > 96 && temp[i].unicodeScalars.first!.value < 123{
  17. let range = Range(s.index(s.startIndex, offsetBy: i)..<s.index(s.startIndex, offsetBy: i+1))
  18. res.append(s.replacingOccurrences(of: String(temp[i]), with: String(temp[i]).uppercased(), options: .regularExpression, range: range))
  19. helper(res: &res, s: res.last!, num: i+1)
  20. }
  21. }
  22. }
  23. }
Add Comment
Please, Sign In to add comment