my swift solution


  • 0
    W
    class Solution {
        func repeatedSubstringPattern(_ str: String) -> Bool {
        var arr:[UnicodeScalar] = Array(str.unicodeScalars)
        var subArr = [UnicodeScalar]()
        var lastC = arr[0]
        var count = 0
        for (index,ch) in arr.enumerated() {
            if count < subArr.count && subArr[count] == ch && lastC == arr.last!{
                for i in index..<arr.count {
                    if subArr[count] == arr[i] {
                        count += 1
                        if count == subArr.count {
                            count = 0
                        }
                    }else {
                        count = 0
                        subArr.append(ch)
                        break;
                    }
                    
                    if i == arr.count - 1 && count == 0{
                        return true
                    }
                }
                
            }else {
                count = 0
                subArr.append(ch)
            }
            lastC = ch
            if index > arr.count / 2 + 1 {
                return false
            }
        }
        return false
    }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.