Lecture 2 - Computational Complexity / Parsing and String Manipulation


Computational Complexity

Big-O Notation

When is O(n²) Better than O(n)?

Big-O Examples

Big-O Sanity-Checking


Amortized Time


Parsing and String Manipulation

Java String Functions: Access

Code Explanation
char charAt(int index) Return the character at the given position.
int codePointAt(int index) Return the Unicode code point at the specified position.
int length() Return the length of the string.
int codePointCount(int b, int e) Return the number of Unicode code points in the range from index b to e.

Java String Functions: Testing

Code Explanation
s.contains(CharSequence cs) Return True if cs is a subsequence of s.
s.startsWith(String pre) Return True if s starts with the prefix pre.
s.endsWith(String suf) Return True if s ends with the suffix suf.
int s1.indexOf(int ch) Return the index of the first occurrence of ch.
int s1.indexOf(String s2) Return the index of the first occurrence of the string s2.
int s1.lastIndexOf(ch/s2) Return the index of the last occurrence of ch or s2.

Java String Functions: Modification

Code Explanation
s.replace(c1, c2) Replace all occurrences of c1 with c2.
s.toLowerCase() Convert all characters in s to lowercase.
s.toUpperCase() Convert all characters in s to uppercase.

Java String Functions: Assembly/Disassembly

Code Explanation
s1.concat(s2)
s1 += s2
Append s2 to the end of s1.
String.valueOf(char[] arr) Return a new string containing the characters in arr.
s.substring(int b, int e) Return a new string containing the substring from index b to e.
s.trim() Return s without leading and trailing whitespace.
String[] s.split(String regex) Split s around occurrences of the regex pattern.

Java String Functions: Comparison

Code Explanation
string1 == string2 Returns True if string1 and string2 reference the same instance.
string1.equals(string2) Returns True if string1 and string2 have the same content.
string1.compareTo(string2) Returns 0 if equal, -1 if string1 is lexically before string2, +1 if after.
string1.compareToIgnoreCase(string2) Returns 0 if string1 and string2 have the same content, ignoring case.
s.matches(String regex) Returns True if s matches the regular expression regex.

Regular Expressions

Building Blocks of Regular Expressions

  1. Character Literal: Matches a specific character

  2. Character Wildcard (.): Matches any single character

  3. Character Set ([...]): Matches any one of the characters within the brackets

  4. Repetition:

  5. Concatenation: Combines multiple expressions

  6. Alternation (|): Matches either the expression before or after |

Anchors

Extended Regular Expressions

  1. Finer Control Over Repetition:

  2. Grouping and Capture:

Special Use of Backslash (\)

  1. Escape Special Characters: Makes a special character literal

  2. Referencing Matches:

  3. Shortcut Expressions:

  4. Literal Backslash:

Regular Expression Examples

Expression Matches
abc abc only
ab*c ac, abc, abbc, abbbc, abbbbc, etc.
.\. Any character followed by a period
(a\|bc)d acd and bcd
(a\|(bc))d ad and bcd
x[aeiou]?z xz, xaz, xez, xiz, xoz, and xuz
b{3,4} bbb and bbbb
^a a only — multiple leading carets all anchor to start
^\^a ^a
[A-Z] A, B, ..., Z
[^0-9] Any character except a digit
\d{3}-\d{2}-\d{4} 123-45-6789
\bword\b word
X+ X, XX, XXX, etc.
^abc$ abc
foo\|bar foo or bar
a(bc)*d ad, abcd, abcbcd, etc.
\w+@\w+\.\w+ example@domain.com
\s A single whitespace character
(\d{3}) Captures three digits (e.g., 123)

Regex Functions in Java


Back to Home Next Lecture