Skip to content

Commit 56350f9

Browse files
author
Harry Dulaney
committed
Add Ex 22.03
1 parent 89f2b34 commit 56350f9

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

ch_22/Exercise22_02.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* __________________________________________________________________________________________
1313
* ----------------------------- Time Complexity Analysis -----------------------------------
1414
* __________________________________________________________________________________________
15-
* Linear Growth Rate (Linear Algorithm)
15+
* Time complexity is: O(n) (Linear Time)
1616
* For example:
1717
* If input string is length 20:
1818
* ---- T(n) = 20 * (input string loop) = O(n) -----

ch_22/Exercise22_03.java

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package ch_22;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* *22.3 (Pattern matching) Write a program that prompts the user to enter two strings
7+
* and tests whether the second string is a substring of the first string. Suppose
8+
* the neighboring characters in the string are distinct. (Don’t use the indexOf
9+
* method in the String class.) Analyze the time complexity of your algorithm.
10+
* Your algorithm needs to be at least O(n) time.
11+
* Here is a sample run of the program:
12+
* <p>
13+
* Enter a string s1: Welcome to Java
14+
* Enter a string s2: come
15+
* matched at index 3
16+
* <p>
17+
* __________________________________________________________________________________________
18+
* ----------------------------- Time Complexity Analysis -----------------------------------
19+
* __________________________________________________________________________________________
20+
* Time complexity is: O(n) (Linear Time)
21+
*
22+
* If s1.length is 100 and s2.length is 4:
23+
* Worst case is that the loop will execute over all values of s1 and check if index
24+
* of s1 matches the starting char in s2.
25+
*
26+
* T(n) = 100 * (s1 String loop) = O(n)
27+
*
28+
* So, Time complexity is O(n) because
29+
* as the length of the input grows, the time will increase at a linear rate
30+
* based on the size of s1.
31+
* The size of s2 is not relevant since we are only check that the string have the same starting char
32+
* and then if they do checking for equality of s1 substring and s2;
33+
*
34+
* --------------------------------- Additional efficiency ---------------------------------
35+
* This could be made more efficient by utilizing the constraint of neighboring characters must be
36+
* distinct, by performing a fast-forward after a negative match on s1 substring and s2, you move s1's index pointer
37+
* forward to the next possible match for s2 starting character before resuming the loop.
38+
*
39+
* Fast-forwarding would result in better time complexity of O(n/m) where n is length of s1 and m is length of s2;
40+
* __________________________________________________________________________________________
41+
*/
42+
public class Exercise22_03 {
43+
public static void main(String[] args) {
44+
Scanner scanner = new Scanner(System.in);
45+
System.out.print("Enter a string s1: ");
46+
String s1 = scanner.nextLine();
47+
System.out.print("Enter a string s2: ");
48+
String s2 = scanner.nextLine();
49+
String res = getMatchedIndex(s1, s2);
50+
System.out.println(res);
51+
scanner.close();
52+
}
53+
54+
private static String getMatchedIndex(String s1, String s2) {
55+
int i1 = 0;
56+
int matchLength = s2.length();
57+
char s2Start = s2.charAt(0);
58+
while (i1 < s1.length()) {
59+
if (s1.charAt(i1) == s2Start) { // Try to match first char in s2
60+
String s1Sub = s1.substring(i1, i1 + matchLength);
61+
if (s1Sub.equals(s2)) {
62+
return "matched at index " + i1;
63+
} else {
64+
// fast-forward here
65+
}
66+
}
67+
i1++;
68+
}
69+
return "s2 is not a substring of s1";
70+
}
71+
72+
}

0 commit comments

Comments
 (0)