-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path55. Check if string contains substring .js
More file actions
165 lines (136 loc) · 4.68 KB
/
55. Check if string contains substring .js
File metadata and controls
165 lines (136 loc) · 4.68 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// check if string contains substring :
// --------------------------------------------------------------------------------------------------------------::
// Check if main string contains substring
const mainString = "hello world programming";
const searchFor = "world";
let found = false;
// Calculate string lengths manually
let mainLength = 0;
while (mainString[mainLength] !== undefined) {
mainLength = mainLength + 1;
}
let searchLength = 0;
while (searchFor[searchLength] !== undefined) {
searchLength = searchLength + 1;
}
// Check each possible starting position in main string
for (let i = 0; i <= mainLength - searchLength; i++) {
let matches = true;
// Check if substring matches at this position
for (let j = 0; j < searchLength; j++) {
if (mainString[i + j] !== searchFor[j]) {
matches = false;
break; // Stop checking this position
}
}
// If all characters matched, we found it
if (matches === true) {
found = true;
break; // Stop searching
}
}
console.log(found);
/*
EXPLANATION:
Check if smaller string exists inside bigger string
STEPS:
1. Calculate lengths of both strings manually
2. Try each possible starting position in main string
3. At each position, check if all characters of substring match
4. If complete match found, set found = true
5. Result: true if substring exists, false if not
EXAMPLE:
Main: "hello world"
Search: "world"
- Position 0: "hello" ≠ "world"
- Position 1: "ello " ≠ "world"
- Position 6: "world" = "world" ✓ FOUND!
WHY WE STOP AT mainLength - searchLength:
- If main string is 10 chars and search is 5 chars
- Last possible start position is 5 (positions 5,6,7,8,9)
- Position 6 would go beyond string bounds
*/
// Using basic logic
// ---------------------------------------------------------------------
const text = "javascript programming";
const target = "script";
let exists = false;
// Simple nested loop approach
for (let i = 0; i < text.length; i++) {
let match = true;
// Check if target string starts at position i
for (let j = 0; j < target.length; j++) {
// Check if we're within bounds and characters match
if (i + j >= text.length || text[i + j] !== target[j]) {
match = false;
break;
}
}
if (match === true) {
exists = true;
break;
}
}
console.log(`Exists: ${exists}`);
/*
EXPLANATION:
Cleaner approach using .length property but still manual logic
STEPS:
1. Try each starting position in main text
2. At each position, check if target matches character by character
3. Include bounds checking: i + j < text.length
4. If complete match found, set exists = true
BOUNDS CHECKING:
- i + j >= text.length prevents reading beyond string
- Ensures we don't access undefined characters
- More robust than first version
*/
// Ignore uppercase/lowercase differences
// ---------------------------------------------------------------------
const sentence = "Hello JavaScript World";
const pattern = "javascript";
let foundIgnoreCase = false;
for (let i = 0; i <= sentence.length - pattern.length; i++) {
let allMatch = true;
for (let j = 0; j < pattern.length; j++) {
// Convert both characters to lowercase for comparison
let mainChar = sentence[i + j];
let patternChar = pattern[j];
// Manual lowercase conversion
if (mainChar >= "A" && mainChar <= "Z") {
// Convert uppercase to lowercase by adding 32 to ASCII value
mainChar = String.fromCharCode(mainChar.charCodeAt(0) + 32);
}
if (patternChar >= "A" && patternChar <= "Z") {
patternChar = String.fromCharCode(patternChar.charCodeAt(0) + 32);
}
if (mainChar !== patternChar) {
allMatch = false;
break;
}
}
if (allMatch === true) {
foundIgnoreCase = true;
break;
}
}
console.log(`Sentence: "${sentence}"`);
console.log(`Pattern: "${pattern}"`);
console.log(`Found (ignore case): ${foundIgnoreCase}`);
/*
EXPLANATION:
Convert characters to same case before comparing
MANUAL LOWERCASE CONVERSION:
- Check if character is uppercase: char >= "A" && char <= "Z"
- ASCII value of 'A' is 65, 'a' is 97
- Difference is 32, so add 32 to convert uppercase to lowercase
- String.fromCharCode() converts ASCII number back to character
EXAMPLE:
"Hello JavaScript" searching for "javascript"
- H → h, e → e, l → l, l → l, o → o (no match with "javas")
- J → j, a → a, v → v, a → a, S → s, c → c, r → r, i → i, p → p, t → t ✓
WHY MANUAL CONVERSION:
- Avoids using .toLowerCase() built-in method
- Shows understanding of ASCII values
- More control over character handling
*/