Solving Arrays & Strings Leet code questions [Java]
Problem solving in Array and String
IT & Software,IT certifications,Coding Interview
Lectures -8
Duration -1.5 hours
30-days Money-Back Guarantee
Get your team access to 10000+ top Tutorials Point courses anytime, anywhere.
Course Description
The array is a very basic data structure representing a group of similar elements, accessed by index. The array data structure can be effectively stored inside the computer and provides fast access to all its elements
Arrays store values of the same data type. The address of elements are stored consecutively in memory
length is fixed. inserting or deleting an element in the middle is not easy
Declaration:
int[] nums = {1,2,4} //initializing with values
int[] nums = new int[10] //initializing with size. 0 will be initialized in all indexes if we declare without variables
Accessing an element:
Array index starts from 0..length-1
int[] nums = {1,2,4}
nums[0] — 1
nums[1] — 2
nums[2] — 4
Built-in methods to use in Stack
Sorting an element - Arrays.sort(nums);
Finding the size of array - array_name . length
Filling arary element with some values -
Arrays.fill(nums, -1) -> this will assign all values in the array as -1
Searching
When we need to search a particular element in an array, we can do that in two ways such as
1. Linear search - We need to traverse the array completely and check if we find the element
2. Binary search — We can do binary search only when the array is sorted. Below is the command to use binary search and find if the element is present in the array or not.
Arrays.binarySearch(array,value to find) -> returns index
Iterating Arrays
Let us consider looping the elements in an integer array, we can do it like the below
1. For loop:
for (i = 0; i < nums.length; i++) {
System.out.print(nums[i]); // accessing each element of array
}
2. For each loop
for (int i : nums) {
System.out.print(i);
}
Strings
Strings are nothing but a sequence of character values. They will be written inside “ ”.
Declaration
String s=new String(“Welcome”);
The strings created like this are not mutable. If we want to add or delete anything from this string, we need to use StringBuilder or String Buffer class only
Built-in methods:
length of a string — length()
seeing character at some position — charAt()
if a string contains a particular substring — contains()
Converting string to char array — toCharArray()
taking substrings — substring()
Replacing in string — replace()
Replacing first occurrence — replaceFirst()
joining two strings — string1.concat(string2)
getting the index of a character — string.indexOf(character)
removes beginning and ending space in string — trim()
converting lower and capital letters inside string — toUpperCase() and toLowerCase()
StringBuilder:
StringBuilder is mutable.
Declaration:
StringBuilder string = new StringBuilder(“Welcome”);
Built-in methods used in String Builder:
All string built-in methods can be used here as well
string.append(“strings”) — to append to a string
string.insert(startIndex,” string”) — to insert a string from a particular index
string.delete(startIndex, endIndex) — deleted string from the start to the given end index
string.reverse() — to reverse the array
Iterating String:
For each loop:
for (Char c: string.toCharArray()){
{
System.out.println(c);
}
For loop:
for(int i = 0; i < string.length();i++)
{
System.out.println(string.charAt(i));
}
Who this course is for:
- Beginners who start work on their Data structures and Algorithms skills
Goals
- Solving Arrays and String questions
- Using two pointer approach
- Finding missing and duplicate elements in Array/strings
- Searching an element in Array / String
Prerequisites
It is good to know about Java collection frameworks such as List, set, Hashmap, etc
![Solving Arrays & Strings Leet code questions [Java] Solving Arrays & Strings Leet code questions [Java]](https://d3mxt5v3yxgcsr.cloudfront.net/courses/16820/course_16820_image.jpg)
Curriculum
Check out the detailed breakdown of what’s inside the course
Basic things to know before practicing DSA
2 Lectures
-
What is Array and How do we work with Arrays 06:21 06:21
-
How to work with Strings 07:58 07:58
Arrays
3 Lectures
![Tutorialspoint](/market/public/assets/images/loader.gif)
Strings
3 Lectures
![Tutorialspoint](/market/public/assets/images/loader.gif)
Instructor Details
![Monish Njs](https://www.tutorialspoint.com/assets/profiles/623465/profile/200_4110020-1679397540.jpeg)
Monish Njs
I am a Senior Software Engineer in PayPal. I enjoy coding, especially solving problems on Leetcode. I want to share what I know by creating a course, and I'm excited to present my course on Data Structures and Algorithms in Java. I've put a lot of effort into making my courses easy to understand, explaining concepts in the simplest way I can.
Course Certificate
Use your certificate to make a career change or to advance in your current career.
![sample Tutorialspoint certificate](/market/public/assets/newDesign/img/primePacks/V-certificate.png)
Our students work
with the Best
![adobe logo](/market/public/assets/newDesign/img/adobe.png)
![adp logo](/market/public/assets/newDesign/img/adp.png)
![apple logo](/market/public/assets/newDesign/img/apple.png)
![armani logo](/market/public/assets/newDesign/img/armani.jpg)
![bosch logo](/market/public/assets/newDesign/img/bosch.png)
![capegemini logo](/market/public/assets/newDesign/img/capegemini.png)
![cisco logo](/market/public/assets/newDesign/img/cisco.png)
![hdfc bank logo](/market/public/assets/newDesign/img/hdfcbank.png)
![ibm logo](/market/public/assets/newDesign/img/ibm.png)
![intel logo](/market/public/assets/newDesign/img/intel.png)
![mastercard logo](/market/public/assets/newDesign/img/mastercard.png)
![netflix logo](/market/public/assets/newDesign/img/netflix.png)
![qualcomm logo](/market/public/assets/newDesign/img/qualcomm.png)
![samsung logo](/market/public/assets/newDesign/img/samsung.png)
![sony logo](/market/public/assets/newDesign/img/sony.png)
![unilever logo](/market/public/assets/newDesign/img/unilever.png)
![walmart logo](/market/public/assets/newDesign/img/walmart.png)
![adobe logo](/market/public/assets/newDesign/img/adobe.png)
![adp logo](/market/public/assets/newDesign/img/adp.png)
![apple logo](/market/public/assets/newDesign/img/apple.png)
![armani logo](/market/public/assets/newDesign/img/armani.jpg)
![bosch logo](/market/public/assets/newDesign/img/bosch.png)
![capegemini logo](/market/public/assets/newDesign/img/capegemini.png)
![cisco logo](/market/public/assets/newDesign/img/cisco.png)
![hdfcbank logo](/market/public/assets/newDesign/img/hdfcbank.png)
![ibm logo](/market/public/assets/newDesign/img/ibm.png)
![intel logo](/market/public/assets/newDesign/img/intel.png)
![mastercard logo](/market/public/assets/newDesign/img/mastercard.png)
![netflix logo](/market/public/assets/newDesign/img/netflix.png)
![qualcomm logo](/market/public/assets/newDesign/img/qualcomm.png)
![samsung logo](/market/public/assets/newDesign/img/samsung.png)
![sony logo](/market/public/assets/newDesign/img/sony.png)
![unilever logo](/market/public/assets/newDesign/img/unilever.png)
![walmart logo](/market/public/assets/newDesign/img/walmart.png)
Related Video Courses
View MoreAnnual Membership
Become a valued member of Tutorials Point and enjoy unlimited access to our vast library of top-rated Video Courses
Subscribe now![Annual Membership Annual Membership](/market/public/assets/newDesign/img/membership-large-small.jpg)
Online Certifications
Master prominent technologies at full length and become a valued certified professional.
Explore Now![Online Certifications Online Certifications](/market/public/assets/newDesign/img/traning-for-a-team.png)