-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhiddenString.java
More file actions
68 lines (57 loc) · 2.02 KB
/
hiddenString.java
File metadata and controls
68 lines (57 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* https://replit.com/@AntheaIp/MI453#Main.java
* Write a function named hidenp that takes two strings and returns 1
if the first string is hidden in the second one,
otherwise returns 0 followed by a newline.
Let s1 and s2 be strings. We say that s1 is hidden in s2 if it's possible to
find each character from s1 in s2, in the same order as they appear in s1.
Also, the empty string is hidden in any string.
s1, substring: isip
s2, string: Mississippi
= 1
2 pointers:
p1: i; find in s2; finds at index 1
p2: 1
p1: s: start at index 1 / p2, yes at index 2
p2: 2
p1: i, start at p2 = 2; yes at index 4
p2; 4
p1: p, start at p2 = 4, yes
Yes
*/
public class hiddenString {
public static boolean hidenp(String s1, String s2) {
if (s1.length() == 0) return true;
int p2 = 0;
for (int i = 0; i < s1.length(); i++) {
boolean found = false;
for (int j = p2; j < s2.length(); j++) {
System.out.println("48i: " + i);
System.out.println("51j: " + j);
System.out.println("52s1charAt(i): " + s1.charAt(i));
System.out.println("53s2charAt(j): " + s2.charAt(j));
if (s1.charAt(i) == s2.charAt(j)) {
System.out.println("55s1chari == s2charj");
p2 = j + 1;
System.out.println("57p2: " + p2);
found = true;
break;
}
System.out.println("60found: " + found);
}
if (!found) return false;
}
return true;
}
public static void main(String[] args) {
String i1a = "fgex.;";
String i1b = "tyf34gdgf;'ektufjhgdgex.;.;rtjynur6";
System.out.println(hidenp(i1a, i1b)); // expected value: True
String i2a = "abc";
String i2b = "btarc";
System.out.println(hidenp(i2a, i2b)); // expected value: False
String i3a = "";
String i3b = "long string ?ddl";
System.out.println(hidenp(i3a, i3b)); // expected value: True
}
}