radix - Strings 6. Strings String. Sequence of characters....

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

View Full Document Right Arrow Icon
Robert Sedgewick and Kevin Wayne • Copyright © 2006 • http://www.Princeton.EDU/~cos226 6. Strings 2 Strings String. Sequence of characters. Ex. Natural languages, Java programs, genomic sequences, … The digital information that underlies biochemistry, cell biology, and development can be represented by a simple string of G's, A's, T's and C's. This string is the root data structure of an organism's biology. -M. V. Olson 3 Using Strings in Java String concatenation. Append one string to end of another string. Substring. Extract a contiguous list of characters from a string. String s = "strings"; // s = "strings" char c = s . charAt ( 2 ); // c = 'r' String t = s . substring ( 2 , 6 ); // t = "ring" String u = s + t ; // u = "stringsring" s t r i n g s 0 1 2 3 4 5 6 4 Implementing Strings In Java Memory. 40 + 2N bytes for a virgin string! public final class String implements Comparable < String > { private char [] value ; // characters private int offset ; // index of first char into array private int count ; // length of string private int hash ; // cache of hashCode() private String ( int offset , int count , char [] value ) { this . offset = offset ; this . count = count ; this . value = value ; } public String substring ( int from , int to ) { return new String ( offset + from , to - from , value ); } } could use byte array instead of String to save space java.lang.String
Background image of page 1

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

View Full DocumentRight Arrow Icon
String vs. StringBuilder String . [immutable] Fast substring, slow concatenation. StringBuilder . [mutable] Slow substring, fast (amortized) append. public static String reverse ( String s ) { String rev = ""; for ( int i = s . length () - 1 ; i >= 0 ; i --) rev += s . charAt ( i ); return rev ; } quadratic time public static String reverse ( String s ) { StringBuilder rev = new StringBuilder (); for ( int i = s . length () - 1 ; i >= 0 ; i --) rev . append ( s . charAt ( i )); return rev . toString (); } linear time 6 public static String lcp ( String s , String t ) { int n = Math . min ( s . length (), t . length ()); for ( int i = 0 ; i < n ; i ++) { if ( s . charAt ( i ) != t . charAt ( i )) return s . substring ( 0 , i ); } return s . substring ( 0 , n ); } Longest Common Prefix Longest common prefix. Given two strings, find the common prefix that is as long as possible. p r e f i x 0 1 2 3 4 5 6 p r e f e t c 7 h Robert Sedgewick and Kevin Wayne • Copyright © 2006 • http://www.Princeton.EDU/~cos226 Radix Sorting Reference: Chapter 13, Algorithms in Java, 3 rd Edition, Robert Sedgewick. 8 Radix Sorting Radix sorting. ! Specialized sorting solution for strings. ! Same ideas for bits, digits, etc. Applications. ! Bioinformatics. ! Sorting strings. ! Full text indexing.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/10/2011.

Page1 / 11

radix - Strings 6. Strings String. Sequence of characters....

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