swift 在swift中设置字符串,使正则表达式搜索在极端空间的O(1)中进行。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 在swift中设置字符串,使正则表达式搜索在极端空间的O(1)中进行。相关的知识,希望对你有一定的参考价值。

//struct that holds the set of all possible subtrings
//does this process directly in the initialization of struct

struct StringPowerSet {
    let string:String
    var substrs:Set<String>
    
    init(string:String){
        self.string = string
        self.substrs = Set<String>()
        let length = self.string.characters.count
        let chars = Array(string.characters)
        //for loop to put in single characters
        for elem in chars {
            self.substrs.insert(String(elem))
        }
        
        //repeating loop to get substrs of other sizes.
        for i in 1..<length {
            var substart = 0
            var subend = i
            //intermediate loop to cut substrs of specific sizes
            while subend < length {
                self.substrs.insert(String(chars[substart...subend]))
                substart += 1
                subend += 1
            }
        }
    }
    
    //checking function to determine if substr is in string in O(1) time
    func hasString(string:String) -> Bool {
        return self.substrs.contains(string)
    }
}


let text = StringPowerSet(string:"i love apples and oranges because they are awesome")
text.hasString("apps")
//false

以上是关于swift 在swift中设置字符串,使正则表达式搜索在极端空间的O(1)中进行。的主要内容,如果未能解决你的问题,请参考以下文章

减少数组以在 Swift 中设置

Swift 3 - 根据单击的单元格在 TableViewController swift 文件中设置变量

如何在 Swift 中设置自定义视图的内在内容大小?

swift Swift - 在代码中设置自动旋转

swift 在Swift中设置计划任务

在 Swift 中使用特殊字符转义正则表达式