Skip to content

Commit 0b1c81d

Browse files
authored
Merge pull request #938 from SadafKausar2025/string
Added String topic in DSA
2 parents c0446e7 + 694dbce commit 0b1c81d

File tree

4 files changed

+395
-3
lines changed

4 files changed

+395
-3
lines changed

docs/dsa/strings/_category_.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "String",
3+
"position": 8,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "In data structures, a string is a sequence of characters used to represent text. Strings are commonly used for storing and manipulating textual data in computer programs. They can be manipulated using various operations like concatenation, substring extraction, and comparison."
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Practice Problems for Strings
2+
3+
To further practice and test your understanding of strings, consider solving the following problems from LeetCode:
4+
5+
## Easy
6+
7+
1. [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)
8+
2. [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)
9+
3. [Implement strStr()](https://leetcode.com/problems/implement-strstr/)
10+
4. [Count and Say](https://leetcode.com/problems/count-and-say/)
11+
5. [Reverse String](https://leetcode.com/problems/reverse-string/)
12+
6. [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/)
13+
7. [Valid Anagram](https://leetcode.com/problems/valid-anagram/)
14+
8. [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/)
15+
9. [Detect Capital](https://leetcode.com/problems/detect-capital/)
16+
10. [Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/)
17+
18+
## Medium
19+
20+
11. [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
21+
12. [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)
22+
13. [Group Anagrams](https://leetcode.com/problems/group-anagrams/)
23+
14. [Decode String](https://leetcode.com/problems/decode-string/)
24+
15. [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/)
25+
16. [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)
26+
17. [Simplify Path](https://leetcode.com/problems/simplify-path/)
27+
18. [Add Bold Tag in String](https://leetcode.com/problems/add-bold-tag-in-string/)
28+
19. [Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/)
29+
20. [Longest Palindromic Subsequence](https://leetcode.com/problems/longest-palindromic-subsequence/)
30+
31+
Engaging with these problems will help reinforce the concepts learned and provide practical experience in using strings effectively. By practicing these problems, you will enhance your problem-solving skills and deepen your understanding of string manipulation in various contexts.

docs/dsa/strings/strings-dsa.md

+352
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,352 @@
1+
---
2+
id: strings-in-dsa
3+
title: Strings in Data Structures and Algorithms
4+
sidebar_label: Strings
5+
sidebar_position: 1
6+
description: "A string is a sequence of characters. It is a data structure that represents a sequence of characters, either as a literal constant or as some kind of variable. In data structures and algorithms, strings are used in a wide range of applications such as text processing, pattern matching, and data serialization."
7+
tags:
8+
[
9+
dsa,
10+
data-structures,
11+
strings,
12+
string,
13+
string-data-structure,
14+
string-in-dsa,
15+
string-in-data-structure,
16+
string-in-algorithm,
17+
string-in-dsa-example,
18+
string-in-dsa-explanation,
19+
string-in-dsa-conclusion,
20+
string-in-dsa-importance,
21+
string-in-dsa-syntax,
22+
string-in-dsa-declaration,
23+
string-in-dsa-access,
24+
string-in-dsa-update,
25+
string-in-dsa-length,
26+
string-in-dsa-iterate,
27+
string-in-dsa-max-min,
28+
string-in-dsa-program,
29+
string-in-dsa-code,
30+
string-in-dsa-js,
31+
string-in-dsa-java,
32+
string-in-dsa-python,
33+
string-in-dsa-c,
34+
string-in-dsa-cpp,
35+
string-in-dsa-ts,
36+
]
37+
---
38+
39+
A string is a sequence of characters. It is a data structure that represents a sequence of characters, either as a literal constant or as some kind of variable. In data structures and algorithms, strings are used in a wide range of applications such as text processing, pattern matching, and data serialization.
40+
41+
## Why are Strings important?
42+
43+
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.
44+
45+
## How to declare a String?
46+
47+
A string can be declared in various programming languages using the following syntax:
48+
49+
# Strings in Data Structures and Algorithms (DSA)
50+
51+
## Table of Contents
52+
53+
1. [Introduction](#introduction)
54+
2. [Basic String Operations](#basic-string-operations)
55+
3. [Pattern Matching Algorithms](#pattern-matching-algorithms)
56+
4. [String Manipulation](#string-manipulation)
57+
5. [String Data Structures](#string-data-structures)
58+
6. [Common String Problems](#common-string-problems)
59+
7. [Advanced String Algorithms](#advanced-string-algorithms)
60+
8. [Resources and References](#resources-and-references)
61+
62+
## Introduction
63+
64+
Strings are sequences of characters and are a fundamental data type in computer science. They are used extensively in algorithms and data structures.
65+
66+
## Basic String Operations
67+
68+
- **Length**: Determine the length of a string.
69+
- **Concatenation**: Combine two or more strings.
70+
- **Substring**: Extract a portion of a string.
71+
- **Comparison**: Compare two strings lexicographically.
72+
- **Search**: Find the occurrence of a substring.
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+
286+
## Pattern Matching Algorithms
287+
288+
- **Naive Pattern Matching**: A straightforward approach with a time complexity of O(m\*n).
289+
- **Knuth-Morris-Pratt (KMP)**: An efficient pattern matching algorithm with a time complexity of O(m+n).
290+
- **Rabin-Karp Algorithm**: Uses hashing for pattern matching with a time complexity of O(m+n) on average.
291+
- **Boyer-Moore Algorithm**: A powerful algorithm with a worst-case time complexity of O(m\*n) but performs well in practice.
292+
293+
## String Manipulation
294+
295+
- **Reversal**: Reversing a string.
296+
- **Palindromes**: Checking if a string reads the same forwards and backwards.
297+
- **Anagrams**: Checking if two strings are permutations of each other.
298+
- **Rotation**: Rotating a string by a given number of characters.
299+
300+
## String Data Structures
301+
302+
- **Trie (Prefix Tree)**: A tree-like data structure that stores a dynamic set of strings, typically used for search operations.
303+
- **Suffix Tree**: A compressed trie of all suffixes of a given string, useful for pattern matching.
304+
- **Suffix Array**: An array of all suffixes of a string, sorted in lexicographical order.
305+
- **Aho-Corasick Algorithm**: A trie-based data structure for multiple pattern matching.
306+
307+
## Common String Problems
308+
309+
- **Longest Common Substring**: Finding the longest substring that appears in two or more strings.
310+
- **Longest Common Subsequence**: Finding the longest sequence that can be derived from two strings without changing the order of characters.
311+
- **Edit Distance (Levenshtein Distance)**: Measuring the minimum number of single-character edits required to change one string into another.
312+
- **String Compression**: Reducing the size of a string using algorithms like Run-Length Encoding (RLE).
313+
314+
## Advanced String Algorithms
315+
316+
- **Burrows-Wheeler Transform (BWT)**: A data transformation algorithm useful for data compression.
317+
- **Manacher’s Algorithm**: An efficient algorithm to find the longest palindromic substring in linear time.
318+
- **Z-Algorithm**: Finds occurrences of a pattern in a string in linear time.
319+
320+
## Resources and References
321+
322+
- **Books**:
323+
- "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein
324+
- "Algorithms" by Robert Sedgewick and Kevin Wayne
325+
- **Online Courses**:
326+
- Coursera: Data Structures and Algorithm Specialization
327+
- edX: Algorithms and Data Structures
328+
- **Websites**:
329+
- [GeeksforGeeks](https://www.geeksforgeeks.org)
330+
- [LeetCode](https://leetcode.com)
331+
- [HackerRank](https://www.hackerrank.com)
332+
333+
---
334+
335+
By understanding and mastering these string concepts and algorithms, you will be well-equipped to tackle a wide range of problems in data structures and algorithms.
336+
337+
## Conclusion
338+
339+
Strings are a vital data structure in the study of data structures and algorithms (DSA). They are sequences of characters used to represent text and are fundamental to various programming tasks. In this tutorial, we explored the essential operations related to strings, including declaration, access, modification, length determination, iteration, and searching in different programming languages like JavaScript, Java, Python, C, C++, and TypeScript.
340+
341+
Understanding strings is crucial for solving numerous problems in computer science, from simple text manipulation to complex algorithms in text processing, pattern matching, and more. The examples provided demonstrate how to work with strings efficiently, ensuring a robust foundation for tackling more advanced DSA concepts. Mastery of strings enhances your ability to handle data and perform operations crucial in both everyday programming and competitive coding.
342+
343+
Problems for Practice
344+
To further practice and test your understanding of strings, consider solving the following problems from LeetCode:
345+
346+
1. Longest Substring Without Repeating Characters
347+
2. Valid Anagram
348+
3. Longest Palindromic Substring
349+
4. Group Anagrams
350+
5. Minimum Window Substring
351+
352+
Engaging with these problems will help reinforce the concepts learned and provide practical experience in using strings effectively. By practicing these problems, you will enhance your problem-solving skills and deepen your understanding of string manipulation in various contexts.

0 commit comments

Comments
 (0)