A string is a character sequence in computer science. It’s one of the most used data types in programming, and it’s used to represent text like names, addresses, and messages.
Storage:
A string is normally stored in memory as a contiguous block of memory holding the character sequence. Depending on the character encoding used, each character in the string is saved as a single byte or many bytes. It is vital to remember that because strings are normally immutable, any changes to a string result in the creation of a new string object in memory.
char myString[] = "Hello, world!";
This creates a character array named myString
that contains the string “Hello, world!”.
String
class. To declare a string variable, use the following syntax:String myString = "Hello, world!";
This creates a String
object named myString
that contains the string “Hello, world!”.
str
class. To declare a string variable, use the following syntax:my_string = "Hello, world!"
This creates a str
object named my_string
that contains the string “Hello, world!”.
Here are some common string operations and their corresponding code examples:
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
String str = "Hello, World!";
int length = str.length();
String str1 = "Hello";
String str2 = "hello";
boolean isEqual = str1.equalsIgnoreCase(str2);
String str = "Hello, World!";
String substr = str.substring(7, 12);
String str = "Hello, World!";
int index = str.indexOf("World");
String str = "Hello, World!";
String newStr = str.replace("World", "Universe");
Note: few concepts used further-
There are also several library functions for Strings used in different programming languages(Java, C, C++, Python, etc).
Given a string S, check if it is palindrome or not.
Example:
Input: S = "abba" Output: 1 Explanation: S is a palindrome
Solution:
class Solution {
int isPalindrome(String S) {
// code here
String n="";
for(int i=S.length()-1;i>=0;i--){
n=n+S.charAt(i);
}
if(n.compareTo(S)==0)
return 1;
else
return 0;
}
};
Time Complexity: O(Length of S)
Space Complexity: O(1)
The function works as follows:
n
.S
in reverse order, appending each character to n
to create a reversed copy of S
.n
to the original string S
.S
is a palindrome).S
is not a palindrome).Given a string, The task is to count the number of alphabets present in the string.
Example:
Input: S = "adjfjh23" Output: 6 Explanation: only last 2 are not alphabets.
Solution:
class Solution{
static int Count(String S)
{int count=0;
// code here
for(int i=0;i<S.length();i++){
char c=S.charAt(i);
if(Character.isLetter(c))
count++;
}
return count;
}
}
Time Complexity: O(Length of S)
Space Complexity: O(1)
The function works as follows:
Count
takes a string S
as input and returns an integer value.count
to zero to count the number of letters.S
.Character.isLetter()
method.count
variable.count
value.Bingu was testing all the strings he had at his place and found that most of them were prone to a vicious attack by Banju, his arch-enemy. Bingu decided to encrypt all the strings he had, by the following method.
Every substring of identical letters is replaced by a single instance of that letter followed by the number of occurrences of that letter. Then, the string thus obtained is further encrypted by reversing it.
Example:
Input: s = "aabc" Output: 1c1b2a Explanation: aabc Step1: a2b1c1 Step2: 1c1b2a
Solution:
class Solution
{
String encryptString(String s)
{
StringBuilder sb = new StringBuilder();
int count = 1;
char f = s.charAt(0);
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == f) {
count++;
} else {
sb.append(f).append(count);
f = s.charAt(i);
count = 1;
}
}
sb.append(f).append(count);
sb.reverse();
return sb.toString();
}
}
Time Complexity: O(Length of S)
Space Complexity: O(1)
encryptString
takes a string s
as input and creates a StringBuilder object sb
.f
stores the first character of the input string and the variable count
is initialized to 1.f
, the count value is incremented.f
, the character f
and its count value are appended to the StringBuilder object sb
. Then, the value of f
is updated to the current character and the count value is reset to 1.f
and its count value are appended to the StringBuilder object sb
.sb
is reversed and converted to a String using the toString()
method.Given two strings a and b. Check whether they contain any common subsequence (non empty) or not.
Example:
Input: a = "ABEF" b = "CADE" Output: 1 Explanation: Subsequence "AE" occurs in both the strings.
Solution:
class Sol
{
Boolean commonSubseq (String a, String b)
{for (int i = 0; i < a.length(); i++) {
for (int j = 0; j < b.length(); j++) {
if (a.charAt(i) == b.charAt(j)) {
return true;
}
}
}
return false;// your code here
}
}
Time Complexity: O(n * m), where n is the length of string a
and m is the length of string b
.
Space Complexity: O(1)
Note: also read about Bucket Sort
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…