I come from a java background and String
slicing in swift appears very verbose and difficult to me. I am trying to answer this leet code question with an algorithm that works with subscripts.
Given a string s, find the length of the longest substring without repeating characters.
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Here is my code:
func lengthOfLongestSubstring(_ s: String) -> Int {
var maximum = 0
var start = 0
var end = 0
var set = Set<Character>()
while end < s.count{
if set.contains(s[end]){
set.remove(s[start])
start =1
}
else{
set.insert(s[end])
end =1
maximum = max(maximum, end-start)
}
}
}
I worked with Int subscripts. But I get this error message: 'subscript(_:)' is unavailable: cannot subscript String with an Int, use a String.Index instead
. How can I solve it without getting too verbose?
CodePudding user response:
Other languages make simplifying assumptions about Unicode strings that Swift does not. You can't be sure how many code-points a given character takes to store, so each time you index to the nth character of a unicode string it is an O(n)
operation. Thus code that uses random access integer indexing into Unicode has unexpected O(n²)
performance. For that reason Swift does not offer integer indexing.
The simple way to get integer indexing into the characters of a Swift string, as Matt suggests in his comment, is to convert the String to an Array of characters. You'll pay the O(n)
cost for that conversion ONCE (as well as memory costs for extra storage) and then have fixed-time access to characters after that.
Alternately you can learn how to index into Swift strings using a String.Index
, but that will require you to change your current approach.
CodePudding user response:
Why is it so difficult to accessing characters in a string?
If strings would be made of fixed sized characters stored in arrays, finding the n-th character would be straightforward and efficient using an integer subscript.
But popular string encodings such as UTF-8 and UTF-16 use a variable number of bytes/words to store characters. So a direct access to the n-th character requires counting characters one by one from the start or other costly strategies, to deal correctly with strings such as "Abçd