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 1 commit
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.
140 changes: 140 additions & 0 deletions docs/dsa/strings/strings-dsa.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
---
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.

## 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.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
id: Letter Combinations of a Phone Number
id: letter-combinations-of-a-phone-number
title: Letter Combinations of a Phone Number (LeetCode)
sidebar_label: 0017-Letter-Combinations-of-a-Phone-Number
sidebar_label: 0017 Letter Combinations of a Phone Number
tags:
- Back Tracking
- Mapping
Expand Down Expand Up @@ -39,6 +39,7 @@ Given a string containing digits from 2-9 inclusive, return all possible letter
- **Output:** `["a","b","c"]`

### Constraints:

- `0 ≤ digits.length ≤ 4`
- `0 ≤ digits.length ≤ 4digits[𝑖]`
- `digits[i] is a digit in the range ['2', '9'].`
Expand Down Expand Up @@ -313,4 +314,4 @@ Here's a step-by-step algorithm for generating all possible letter combinations
- Call the backtracking function with the initial index set to 0 and an empty string as the initial combination.
- Return the list of combinations.

This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking.
This algorithm ensures that all possible combinations are generated by exploring all valid paths through backtracking.
Loading