String in Data Structure
Last Updated :
11 Dec, 2024
Improve
A string is a sequence of characters. The following facts make string an interesting data structure.
- Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.
- Strings are immutable in programming languages like Java, Python, JavaScript and C#.
- Many String Problems can optimized using the fact that the character set size is small. For example sorting can be done faster, counting frequencies of items is faster and many interesting interview questions are based on this.
Basics of Strings:
String in different language:
- Strings in C
- String Class in C++
- String in Java
- Python String
- C# | String
- JavaScript String
- PHP | Strings
Basic Operations of String:
- Length of a String
- Check if two strings are same
- Search a Character
- Insert a Character at a Position
- Remove a Character at a Position
- Insert a character at Multiple Positions
- Remove all occurrences of a character
- Concatenating Two Strings
- Reverse a string
- Generate all Substrings
Please refer String Problems Topic Wise for the list of questions on different topics like binary string, subsequence and substring, pattern searching and palindrome.
Easy Problems on String:
- Check for Binary
- Camel Case Conversion
- Binary Substrings with Corners as 1s
- Check for Panagram
- Check for Palindrome
- Check for Substring
- Check for Subsequence
- Check for Anagrams
- Check for K-Anagrams
- Duplicates in a String
- Longest Substring Between Equals
- Left Rotate
- Check for Rotation
- Validate an IP address
- Count strings with consecutive 1’s
- Generate all binary strings from given pattern
- Smallest and Largest Word in a String
- Count number of equal pairs in a string
- Second most repeated word in a sequence
- All strings by placing spaces
- Sort string of characters
- Char Frequencies in Order of Occurrence
- Char Frequencies in Alphabetical order
- URLify a String
Medium Problems on String:
- First Repeating Character
- First Non-Repeating Character
- K’th Non-repeating Character
- Implement atoi
- Add 2 Binary Strings
- Add n Binary Strings
- Multiply 2 Strings
- Divide large number
- Isomorphic Strings
- Remove Adjacent
- Roman to Integer
- Interleaved Strings
- Permutations of a String
- Longest Palindromic Substring
- Queries for characters in a repeated string
- Count anagram substrings
- Count binary strings without consecutive 1’s
- Lexicographically next string
- Split String into four distinct strings
- Word Break Problem
- Length of Longest Balanced Subsequence
- Minimum Swaps for Bracket Balancing
- String to mobile numeric keypad sequence
- Minimum number from given sequence
- Shortest path to print a string on screen
- Mirror characters of a string
Hard Problems on String:
- Lexicographic Rank of a String
- Multiply Large Numbers
- Ways to increase LCS length of 2 strings by one
- Min rotations required to get the same string
- Find if an array of strings can be chained to form a circle
- Alien Dictionary
- Make two strings Anagram
- Make two Strings Anagram Without Deletion
- Palindrome Substring Queries
- Powet Set in Lexicographic order
- Minimum Word Break
- Word Search - 8 Directions and Straight Movement
- Word Search - 4 Directions and Zig-Zag Allowed
- Minimum number of bracket reversals needed to make an expression balanced
- Word Wrap problem ( Space optimized solution )
- Decode a string recursively encoded as count followed by substring