Skip to content

Added String topic in DSA #938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/dsa/strings/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"label": "String",
"position": 8,
"link": {
"type": "generated-index",
"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."
}
}
31 changes: 31 additions & 0 deletions docs/dsa/strings/leetcode_practice_problems_strings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Practice Problems for Strings

To further practice and test your understanding of strings, consider solving the following problems from LeetCode:

## Easy

1. [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)
2. [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)
3. [Implement strStr()](https://leetcode.com/problems/implement-strstr/)
4. [Count and Say](https://leetcode.com/problems/count-and-say/)
5. [Reverse String](https://leetcode.com/problems/reverse-string/)
6. [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/)
7. [Valid Anagram](https://leetcode.com/problems/valid-anagram/)
8. [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/)
9. [Detect Capital](https://leetcode.com/problems/detect-capital/)
10. [Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/)

## Medium

11. [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
12. [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)
13. [Group Anagrams](https://leetcode.com/problems/group-anagrams/)
14. [Decode String](https://leetcode.com/problems/decode-string/)
15. [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/)
16. [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)
17. [Simplify Path](https://leetcode.com/problems/simplify-path/)
18. [Add Bold Tag in String](https://leetcode.com/problems/add-bold-tag-in-string/)
19. [Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/)
20. [Longest Palindromic Subsequence](https://leetcode.com/problems/longest-palindromic-subsequence/)

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.
352 changes: 352 additions & 0 deletions docs/dsa/strings/strings-dsa.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,352 @@
---
id: strings-in-dsa
title: Strings in Data Structures and Algorithms
sidebar_label: Strings
sidebar_position: 1
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."
tags:
[
dsa,
data-structures,
strings,
string,
string-data-structure,
string-in-dsa,
string-in-data-structure,
string-in-algorithm,
string-in-dsa-example,
string-in-dsa-explanation,
string-in-dsa-conclusion,
string-in-dsa-importance,
string-in-dsa-syntax,
string-in-dsa-declaration,
string-in-dsa-access,
string-in-dsa-update,
string-in-dsa-length,
string-in-dsa-iterate,
string-in-dsa-max-min,
string-in-dsa-program,
string-in-dsa-code,
string-in-dsa-js,
string-in-dsa-java,
string-in-dsa-python,
string-in-dsa-c,
string-in-dsa-cpp,
string-in-dsa-ts,
]
---

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.

## Why are Strings important?

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.

## How to declare a String?

A string can be declared in various programming languages using the following syntax:

# Strings in Data Structures and Algorithms (DSA)

## Table of Contents

1. [Introduction](#introduction)
2. [Basic String Operations](#basic-string-operations)
3. [Pattern Matching Algorithms](#pattern-matching-algorithms)
4. [String Manipulation](#string-manipulation)
5. [String Data Structures](#string-data-structures)
6. [Common String Problems](#common-string-problems)
7. [Advanced String Algorithms](#advanced-string-algorithms)
8. [Resources and References](#resources-and-references)

## Introduction

Strings are sequences of characters and are a fundamental data type in computer science. They are used extensively in algorithms and data structures.

## Basic String Operations

- **Length**: Determine the length of a string.
- **Concatenation**: Combine two or more strings.
- **Substring**: Extract a portion of a string.
- **Comparison**: Compare two strings lexicographically.
- **Search**: Find the occurrence of a substring.

## Why are Strings important?

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.

## How to declare a String?

A string can be declared in various programming languages using the following syntax:

<Tabs>
<TabItem value="js" label="JavaScipt" default>
<SolutionAuthor name="@Ajay-Dhangar"/>
```js
// Declare a string in JavaScript
let str = "Hello, world!";
```
</TabItem>
<TabItem value="java" label="Java">
<SolutionAuthor name="@Ajay-Dhangar"/>
```java
// Declare a string in Java
String str = "Hello, world!";
```
</TabItem>
<TabItem value="python" label="Python">
<SolutionAuthor name="@Ajay-Dhangar"/>
```python
# Declare a string in Python
str = "Hello, world!"
```
</TabItem>
<TabItem value="c" label="C">
<SolutionAuthor name="@Ajay-Dhangar"/>
```c
// Declare a string in C
char str[] = "Hello, world!";
```
</TabItem>
<TabItem value="cpp" label="C++">
<SolutionAuthor name="@Ajay-Dhangar"/>
```cpp
// Declare a string in C++
std::string str = "Hello, world!";
```
</TabItem>
<TabItem value="ts" label="TypeScript">
<SolutionAuthor name="@Ajay-Dhangar"/>
```ts
// Declare a string in TypeScript
let str: string = "Hello, world!";
```
</TabItem>
</Tabs>

## How to access a String?

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.

<Tabs>
<TabItem value="js" label="JavaScipt" default>
<SolutionAuthor name="@Ajay-Dhangar"/>
```js
// Access a string in JavaScript
let str = "Hello, world!";
console.log(str[0]); // H
console.log(str[1]); // e
console.log(str[2]); // l
```
</TabItem>
<TabItem value="java" label="Java">
<SolutionAuthor name="@Ajay-Dhangar"/>
```java
// Access a string in Java
String str = "Hello, world!";
System.out.println(str.charAt(0)); // H
System.out.println(str.charAt(1)); // e
System.out.println(str.charAt(2)); // l
```
</TabItem>
<TabItem value="python" label="Python">
<SolutionAuthor name="@Ajay-Dhangar"/>
```python
# Access a string in Python
str = "Hello, world!"
print(str[0]) # H
print(str[1]) # e
print(str[2]) # l
```
</TabItem>
<TabItem value="c" label="C">
<SolutionAuthor name="@Ajay-Dhangar"/>
```c
// Access a string in C
char str[] = "Hello, world!";
printf("%c\n", str[0]); // H
printf("%c\n", str[1]); // e
printf("%c\n", str[2]); // l
```
</TabItem>
<TabItem value="cpp" label="C++">
<SolutionAuthor name="@Ajay-Dhangar"/>
```cpp
// Access a string in C++
std::string str = "Hello, world!";
std::cout << str[0] << std::endl; // H
std::cout << str[1] << std::endl; // e
std::cout << str[2] << std::endl; // l
```
</TabItem>
<TabItem value="ts" label="TypeScript">
<SolutionAuthor name="@Ajay-Dhangar"/>
```ts
// Access a string in TypeScript
let str: string = "Hello, world!";
console.log(str[0]); // H
console.log(str[1]); // e
console.log(str[2]); // l
```
</TabItem>
</Tabs>

## How to update a String?

A string can be updated by creating a new string with the desired changes, as strings are immutable in many programming languages.

<Tabs>
<TabItem value="js" label="JavaScipt" default>
<SolutionAuthor name="@Ajay-Dhangar"/>
```js
// Update a string in JavaScript
let str = "Hello, world!";
str = "Hello, JavaScript!";
console.log(str); // Hello, JavaScript!
```
</TabItem>
<TabItem value="java" label="Java">
<SolutionAuthor name="@Ajay-Dhangar"/>
```java
// Update a string in Java
String str = "Hello, world!";
str = "Hello, Java!";
System.out.println(str); // Hello, Java!
```
</TabItem>
<TabItem value="python" label="Python">
<SolutionAuthor name="@Ajay-Dhangar"/>
```python
# Update a string in Python
str = "Hello, world!"
str = "Hello, Python!"
print(str) # Hello, Python!
```
</TabItem>
<TabItem value="c" label="C">
<SolutionAuthor name="@Ajay-Dhangar"/>
```c
// Update a string in C
char str[] = "Hello, world!";
strcpy(str, "Hello, C!");
printf("%s\n", str); // Hello, C!
```
</TabItem>
<TabItem value="cpp" label="C++">
<SolutionAuthor name="@Ajay-Dhangar"/>
```cpp
// Update a string in C++
std::string str = "Hello, world!";
str = "Hello, C++!";
std::cout << str << std::endl; // Hello, C++!
```
</TabItem>
<TabItem value="ts" label="TypeScript">
<SolutionAuthor name="@Ajay-Dhangar"/>
```ts
// Update a string in TypeScript
let str: string = "Hello, world!";
str = "Hello, TypeScript!";
console.log(str); // Hello, TypeScript!
```
</TabItem>
</Tabs>

## How to find the length of a String?

The length of a string can be found using the `length` property or method.

<Tabs>
<TabItem value="js" label="JavaScipt" default>
<SolutionAuthor name="@Ajay-Dhangar"/>
```js
// Find the length of a string in JavaScript
let str = "Hello, world!";
console.log(str.length); // 13
```
</TabItem>
<TabItem value="java" label="Java">
<SolutionAuthor name="@Ajay-Dhangar"/>
```java
// Find the length of a string in Java
String str = "Hello, world!";
System.out.println(str.length()); // 13
```
</TabItem>
<TabItem value="python" label="Python">
<SolutionAuthor name="@Ajay-Dhangar"/>
```python
# Find the length of a string in Python
str = "Hello, world!"
print(len(str)) # 13
```
</TabItem>
</Tabs>

## Pattern Matching Algorithms

- **Naive Pattern Matching**: A straightforward approach with a time complexity of O(m\*n).
- **Knuth-Morris-Pratt (KMP)**: An efficient pattern matching algorithm with a time complexity of O(m+n).
- **Rabin-Karp Algorithm**: Uses hashing for pattern matching with a time complexity of O(m+n) on average.
- **Boyer-Moore Algorithm**: A powerful algorithm with a worst-case time complexity of O(m\*n) but performs well in practice.

## String Manipulation

- **Reversal**: Reversing a string.
- **Palindromes**: Checking if a string reads the same forwards and backwards.
- **Anagrams**: Checking if two strings are permutations of each other.
- **Rotation**: Rotating a string by a given number of characters.

## String Data Structures

- **Trie (Prefix Tree)**: A tree-like data structure that stores a dynamic set of strings, typically used for search operations.
- **Suffix Tree**: A compressed trie of all suffixes of a given string, useful for pattern matching.
- **Suffix Array**: An array of all suffixes of a string, sorted in lexicographical order.
- **Aho-Corasick Algorithm**: A trie-based data structure for multiple pattern matching.

## Common String Problems

- **Longest Common Substring**: Finding the longest substring that appears in two or more strings.
- **Longest Common Subsequence**: Finding the longest sequence that can be derived from two strings without changing the order of characters.
- **Edit Distance (Levenshtein Distance)**: Measuring the minimum number of single-character edits required to change one string into another.
- **String Compression**: Reducing the size of a string using algorithms like Run-Length Encoding (RLE).

## Advanced String Algorithms

- **Burrows-Wheeler Transform (BWT)**: A data transformation algorithm useful for data compression.
- **Manacher’s Algorithm**: An efficient algorithm to find the longest palindromic substring in linear time.
- **Z-Algorithm**: Finds occurrences of a pattern in a string in linear time.

## Resources and References

- **Books**:
- "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein
- "Algorithms" by Robert Sedgewick and Kevin Wayne
- **Online Courses**:
- Coursera: Data Structures and Algorithm Specialization
- edX: Algorithms and Data Structures
- **Websites**:
- [GeeksforGeeks](https://www.geeksforgeeks.org)
- [LeetCode](https://leetcode.com)
- [HackerRank](https://www.hackerrank.com)

---

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.

## Conclusion

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.

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.

Problems for Practice
To further practice and test your understanding of strings, consider solving the following problems from LeetCode:

1. Longest Substring Without Repeating Characters
2. Valid Anagram
3. Longest Palindromic Substring
4. Group Anagrams
5. Minimum Window Substring

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.
Loading
Loading