You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/dsa/strings/strings-dsa.md
+212
Original file line number
Diff line number
Diff line change
@@ -71,6 +71,218 @@ Strings are sequences of characters and are a fundamental data type in computer
71
71
-**Comparison**: Compare two strings lexicographically.
72
72
-**Search**: Find the occurrence of a substring.
73
73
74
+
## Why are Strings important?
75
+
76
+
Strings are important because they are used to store and manipulate text. They are used in many applications such as text processing, pattern matching, and data serialization.
77
+
78
+
## How to declare a String?
79
+
80
+
A string can be declared in various programming languages using the following syntax:
81
+
82
+
<Tabs>
83
+
<TabItem value="js" label="JavaScipt" default>
84
+
<SolutionAuthor name="@Ajay-Dhangar"/>
85
+
```js
86
+
// Declare a string in JavaScript
87
+
let str = "Hello, world!";
88
+
```
89
+
</TabItem>
90
+
<TabItem value="java" label="Java">
91
+
<SolutionAuthor name="@Ajay-Dhangar"/>
92
+
```java
93
+
// Declare a string in Java
94
+
String str = "Hello, world!";
95
+
```
96
+
</TabItem>
97
+
<TabItem value="python" label="Python">
98
+
<SolutionAuthor name="@Ajay-Dhangar"/>
99
+
```python
100
+
# Declare a string in Python
101
+
str = "Hello, world!"
102
+
```
103
+
</TabItem>
104
+
<TabItem value="c" label="C">
105
+
<SolutionAuthor name="@Ajay-Dhangar"/>
106
+
```c
107
+
// Declare a string in C
108
+
char str[] = "Hello, world!";
109
+
```
110
+
</TabItem>
111
+
<TabItem value="cpp" label="C++">
112
+
<SolutionAuthor name="@Ajay-Dhangar"/>
113
+
```cpp
114
+
// Declare a string in C++
115
+
std::string str = "Hello, world!";
116
+
```
117
+
</TabItem>
118
+
<TabItem value="ts" label="TypeScript">
119
+
<SolutionAuthor name="@Ajay-Dhangar"/>
120
+
```ts
121
+
// Declare a string in TypeScript
122
+
let str: string = "Hello, world!";
123
+
```
124
+
</TabItem>
125
+
</Tabs>
126
+
127
+
## How to access a String?
128
+
129
+
A string can be accessed using the index of the character. The index of the first character is 0, the index of the second character is 1, and so on.
130
+
131
+
<Tabs>
132
+
<TabItem value="js" label="JavaScipt" default>
133
+
<SolutionAuthor name="@Ajay-Dhangar"/>
134
+
```js
135
+
// Access a string in JavaScript
136
+
let str = "Hello, world!";
137
+
console.log(str[0]); // H
138
+
console.log(str[1]); // e
139
+
console.log(str[2]); // l
140
+
```
141
+
</TabItem>
142
+
<TabItem value="java" label="Java">
143
+
<SolutionAuthor name="@Ajay-Dhangar"/>
144
+
```java
145
+
// Access a string in Java
146
+
String str = "Hello, world!";
147
+
System.out.println(str.charAt(0)); // H
148
+
System.out.println(str.charAt(1)); // e
149
+
System.out.println(str.charAt(2)); // l
150
+
```
151
+
</TabItem>
152
+
<TabItem value="python" label="Python">
153
+
<SolutionAuthor name="@Ajay-Dhangar"/>
154
+
```python
155
+
# Access a string in Python
156
+
str = "Hello, world!"
157
+
print(str[0]) # H
158
+
print(str[1]) # e
159
+
print(str[2]) # l
160
+
```
161
+
</TabItem>
162
+
<TabItem value="c" label="C">
163
+
<SolutionAuthor name="@Ajay-Dhangar"/>
164
+
```c
165
+
// Access a string in C
166
+
char str[] = "Hello, world!";
167
+
printf("%c\n", str[0]); // H
168
+
printf("%c\n", str[1]); // e
169
+
printf("%c\n", str[2]); // l
170
+
```
171
+
</TabItem>
172
+
<TabItem value="cpp" label="C++">
173
+
<SolutionAuthor name="@Ajay-Dhangar"/>
174
+
```cpp
175
+
// Access a string in C++
176
+
std::string str = "Hello, world!";
177
+
std::cout << str[0] << std::endl; // H
178
+
std::cout << str[1] << std::endl; // e
179
+
std::cout << str[2] << std::endl; // l
180
+
```
181
+
</TabItem>
182
+
<TabItem value="ts" label="TypeScript">
183
+
<SolutionAuthor name="@Ajay-Dhangar"/>
184
+
```ts
185
+
// Access a string in TypeScript
186
+
let str: string = "Hello, world!";
187
+
console.log(str[0]); // H
188
+
console.log(str[1]); // e
189
+
console.log(str[2]); // l
190
+
```
191
+
</TabItem>
192
+
</Tabs>
193
+
194
+
## How to update a String?
195
+
196
+
A string can be updated by creating a new string with the desired changes, as strings are immutable in many programming languages.
197
+
198
+
<Tabs>
199
+
<TabItem value="js" label="JavaScipt" default>
200
+
<SolutionAuthor name="@Ajay-Dhangar"/>
201
+
```js
202
+
// Update a string in JavaScript
203
+
let str = "Hello, world!";
204
+
str = "Hello, JavaScript!";
205
+
console.log(str); // Hello, JavaScript!
206
+
```
207
+
</TabItem>
208
+
<TabItem value="java" label="Java">
209
+
<SolutionAuthor name="@Ajay-Dhangar"/>
210
+
```java
211
+
// Update a string in Java
212
+
String str = "Hello, world!";
213
+
str = "Hello, Java!";
214
+
System.out.println(str); // Hello, Java!
215
+
```
216
+
</TabItem>
217
+
<TabItem value="python" label="Python">
218
+
<SolutionAuthor name="@Ajay-Dhangar"/>
219
+
```python
220
+
# Update a string in Python
221
+
str = "Hello, world!"
222
+
str = "Hello, Python!"
223
+
print(str) # Hello, Python!
224
+
```
225
+
</TabItem>
226
+
<TabItem value="c" label="C">
227
+
<SolutionAuthor name="@Ajay-Dhangar"/>
228
+
```c
229
+
// Update a string in C
230
+
char str[] = "Hello, world!";
231
+
strcpy(str, "Hello, C!");
232
+
printf("%s\n", str); // Hello, C!
233
+
```
234
+
</TabItem>
235
+
<TabItem value="cpp" label="C++">
236
+
<SolutionAuthor name="@Ajay-Dhangar"/>
237
+
```cpp
238
+
// Update a string in C++
239
+
std::string str = "Hello, world!";
240
+
str = "Hello, C++!";
241
+
std::cout << str << std::endl; // Hello, C++!
242
+
```
243
+
</TabItem>
244
+
<TabItem value="ts" label="TypeScript">
245
+
<SolutionAuthor name="@Ajay-Dhangar"/>
246
+
```ts
247
+
// Update a string in TypeScript
248
+
let str: string = "Hello, world!";
249
+
str = "Hello, TypeScript!";
250
+
console.log(str); // Hello, TypeScript!
251
+
```
252
+
</TabItem>
253
+
</Tabs>
254
+
255
+
## How to find the length of a String?
256
+
257
+
The length of a string can be found using the `length` property or method.
258
+
259
+
<Tabs>
260
+
<TabItem value="js" label="JavaScipt" default>
261
+
<SolutionAuthor name="@Ajay-Dhangar"/>
262
+
```js
263
+
// Find the length of a string in JavaScript
264
+
let str = "Hello, world!";
265
+
console.log(str.length); // 13
266
+
```
267
+
</TabItem>
268
+
<TabItem value="java" label="Java">
269
+
<SolutionAuthor name="@Ajay-Dhangar"/>
270
+
```java
271
+
// Find the length of a string in Java
272
+
String str = "Hello, world!";
273
+
System.out.println(str.length()); // 13
274
+
```
275
+
</TabItem>
276
+
<TabItem value="python" label="Python">
277
+
<SolutionAuthor name="@Ajay-Dhangar"/>
278
+
```python
279
+
# Find the length of a string in Python
280
+
str = "Hello, world!"
281
+
print(len(str)) # 13
282
+
```
283
+
</TabItem>
284
+
</Tabs>
285
+
74
286
## Pattern Matching Algorithms
75
287
76
288
-**Naive Pattern Matching**: A straightforward approach with a time complexity of O(m\*n).
0 commit comments