Lesson 4 - Strings (Classroom
Lesson 4 - Strings (Classroom
Programming
Strings in Java
WHAT WILL
YOU LEARN ● Introduction to Strings.
IN THIS
Commonly used methods
SESSION ? ●
in Strings.
CREDITS: This presentation template was
● Solving
created byindustry level
Slidesgo, including icons by
Flaticon, and infographics & images by Freepik
problems on Strings.
01
Introduction to
Strings
Introduction to String
In Java, a string is a sequence of characters. For example, "hello" is
a string containing a sequence of characters 'h', 'e', 'l', 'l', and 'o'.
// create a string
String type = "Java programming";
Commonly used
methods in Strings.
1. length()
String str =
"ABCDEFGHIJKL"; O/P:
String substr = EFGH
str.substring(4,8);
System.out.println(substr);
5. toLowerCase()
O/P:
the key to
success
6. toUpperCase()
O/P:
String str = "Hello, World!"; HELLO, WORLD!
O/P:
coding Ninjas
8. replace(char oldChar, char
newChar)
Description: Replaces all occurrences of the specified old character
with the specified new character.
O/P:
Jbvbtutoribl
9. replaceAll(String regex, String
replacement)
Description: Replaces each substring of this string that matches the
given regular expression with the given replacement.
O/P:
This is the string
10.split(String regex)
Description: Splits this string around matches of the given regular
expression.
O/P:
["Yes", "You Can Do It!"]
11. equals(Object another)
O/P:
true
false
12. equalsIgnoreCase(String
anotherString)
Description: Compares this String to another String, ignoring case
considerations.
O/P:
true
13. contains(CharSequence s)
Description: Returns true if, and only if, this string contains the
specified sequence of char values.
O/P:
true
14. indexOf(String str)
Description: Returns the index within this string of the first
occurrence of the specified substring.
O/P:
1
15. lastIndexOf(String str)
Description: Returns the index within this string of the last
occurrence of the specified substring.
O/P:
5
16. startsWith(String prefix)
O/P:
true
17. endsWith(String suffix)
Description: Tests if this string ends with the specified suffix.
O/P:
true
18. compareTo(String anotherString)
Description:Compares two strings lexicographically.
O/P:
0
19. valueOf(int i)
Description: It is used to convert any data type, such as int, double,
char, or char array, to its string representation by creating a new
String object.
String.valueOf(123);
“123”
O/P:
123
20. isEmpty()
O/P:
true
04
Solving industry
level problems on
strings
Strings :
1. Reverse a String
2. Valid Palindrome (Single Word)
3. Find the Index of the First Occurrence
in a String
4. Valid Anagram
5. Reverse Words in a String
Reverse a String
https://leetcode.com/problems/reverse-string/description/
Test Case 1:
Input: s = "hello"
Output: "olleh"
Test Case 2:
Input: s = "abcd"
Output: "dcba"
Reverse a String
Function reverseString(s):
Convert s to a character array arr
Initialize start = 0 and end = length
of arr - 1
While start < end:
Swap arr[start] and arr[end]
Increment start and decrement end
Return arr as a string
Reverse a String
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
return new String(arr);
}
Reverse a String
Function
Approach 4 :Using StringBuilder's reverseString(s):
Reverse Method Create a
StringBuilder object
● Create a StringBuilder from s
object from the string. Reverse the
StringBuilder
● Use the reverse() method Return the reversed
of StringBuilder. StringBuilder as a
string
Reverse a String
s = "Level"
Output:
true
Explanation:
Ignoring case, the word "Level" reads the
same backward, so it is a palindrome.
Valid Palindrome (Single Word)
}
left++;
right--;
}
return true;
}
Valid Palindrome (Single Word)
Problem Statement:
You are given two strings:
A main string haystack.
A target string needle.
Your task is to find the index of the first
occurrence of needle in haystack. If needle
does not exist in haystack, return -1.
Find the Index of the First Occurrence in a String
https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/
Test Cases:
Test Case 1:
Input: haystack = "hello", needle = "ll"
Output: 2
Explanation: The substring "ll" starts at
index 2.
Find the Index of the First Occurrence in a String
For i = 0 to n - m:
If haystack.substring(i, i + m) == needle:
Return i
Return -1
Find the Index of the First Occurrence in a String
Problem Statement:
Determine if two given strings, s and
t, are anagrams of each other.
Two strings are considered anagrams
if they contain the same characters
with the same frequency.
Valid Anagram
https://leetcode.com/problems/valid-anagram/description/
Examples
Input: s = "listen", t = "silent"
Output: true
Explanation: Both strings contain
the same characters with the same
frequency.
Valid Anagram
Approach 1: Sorting-Based
● Sort both strings.
char[] arr1 =
s.toCharArray();
Valid Anagram
counts[t.charAt(i) - 'a']--;
}
for (int count : counts)
if (count != 0) return
false;
return true;
}
Reverse Words in a String
https://leetcode.com/problems/reverse-words-in-a-string/description/
Problem Statement:
Given a string s, you need to reverse the
order of words in the string. A word is
defined as a sequence of non-space
characters. The reversed string should not
contain leading or trailing spaces, and
each word should be separated by exactly
one space.
Reverse Words in a String
https://leetcode.com/problems/reverse-words-in-a-string/description/
Example 1:
Input: "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: " hello world "
Output: "world hello"
Reverse Words in a String
function reverseWords(s):
# Step 1: Trim the string to remove
leading and trailing spaces
s = trim(s)
# Step 2: Split the string into words
based on spaces
words = split(s, " ")
Reverse Words in a String
function reverseWords(s):
# Step 1: Reverse the entire
string
s = reverse(s)
# Step 2: Reverse each word
start = 0
Reverse Words in a String
for i in range(len(s)):
if s[i] == " " or i == len(s) - 1:
# Reverse the word between
start and i
reverse(s, start, i)
start = i + 1
# Step 3: Return the reversed string
return s
Reverse Words in a String
if (i < s.length())
result.append(" ");
start = i + 1;
}
}
return
result.toString().trim();
}
THANK YOU
iGenuine
inprotected.com