12_strings.v5

12_strings.v5 - CS161 String Operations David Kauchak • Basic string operations Let Σ be an alphabet e.g Σ = a,b,c,z A string is any member of

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS161 - String Operations David Kauchak • Basic string operations Let Σ be an alphabet, e.g. Σ = ( a,b,c,...,z ) A string is any member of Σ * , i.e. any sequence of 0 or more characters of Σ Given strings s 1 of length n and s 2 of length m , here are some string functions we might use: – Equality - Is s 1 = s 2 (can also consider case insensitive). O ( n ) where n is the length of the shortest string. – Concatenate (append) - Create string s 1 s 2 . Θ( n + m ) – Substitute - Exchange all occurrences of a particular character with another character. For example Substitute (’this is a string’ , i , x) = ’thxs xs a strxng’. Θ( n ) – Length - return the number of characters in the string. Length ( s 1 ) = n- Θ(1) or Θ( n ) depending on how the string is stored. – Prefix - Get the first j characters in the string. prefix (’this is a string’ , 5) = ’this ’. Θ( j ) – Suffix - Get the last j characters in the string. suffix (’this is a string’ , 6) = ’string’. Θ( j ) – Substring - Get the characters between i and j inclusive. substring (’this is a string’ , 4 , 8) = ’s is ’. Θ( j- i ) • Edit Distance (Levenshtein distance) The edit distance between two strings is the minimum number of in- sertions, deletions and substitutions required to transform string s 1 into string s 2 . 1 Insertion: ABACED → ABACCED Deletion: ABACED → ABAED Substitution: ABACED → ABADED Some examples: – Edit (Kitten, Mitten) = 1 – Edit (Happy, Hilly) = 3 – Edit (Banana, Car) = 5 – Edit (Simple, Apple) = 3 Edit distance is symmetric, that is: Edit ( s 1 ,s 2 ) = Edit ( s 2 ,s 1 ) Why?...
View Full Document

This document was uploaded on 05/25/2011.

Page1 / 8

12_strings.v5 - CS161 String Operations David Kauchak • Basic string operations Let Σ be an alphabet e.g Σ = a,b,c,z A string is any member of

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online