Solving Arrays & Strings Leet code questions [Java]
Master array and string problem-solving! Learn essential coding techniques for efficient data manipulation. Boost your programming skills.
Course Content
Solving Arrays & Strings Leet code questions [Java] is a beginner-level Udemy course taught by Monish Njs. It runs 1h 58m and is taught in English. Learners rate it 3.9 out of 5 from 25 reviews, with 1,597 students enrolled. It is free to enroll, first published February 2023.
Arrays in Programming: A Comprehensive Guide
Understanding arrays is fundamental to programming. An array is a basic data structure that stores a collection of similar elements, accessible through their index. Arrays are efficiently stored in computer memory, providing quick access to all their elements. All elements within an array hold the same data type. Crucially, the memory addresses of elements are stored consecutively. Arrays have a fixed size at declaration. Modifying the size by inserting or deleting elements in the middle can be computationally expensive.
Array Declaration & Initialization
You can declare and initialize arrays in several ways:
int[] nums = {1, 2, 4};// Direct initialization with values.int[] nums = new int[10];// Declaration with a specified size (initial values are 0).
Accessing Array Elements
Elements in an array are accessed using their index, starting from 0 up to length - 1.
int[] nums = {1, 2, 4};
nums[0] // Accesses the first element (value: 1)
nums[1] // Accesses the second element (value: 2)
nums[2] // Accesses the third element (value: 4)
Essential Array Methods
Arrays.sort(nums): Sorts the elements of an array in ascending order.array_name.length: Returns the number of elements in the array.Arrays.fill(nums, -1): Fills all elements of the array with a specified value (e.g., -1).
Searching in Arrays
Finding a specific element within an array can be done using two primary methods:
- Linear Search: Iterates through the entire array to check for the target element.
- Binary Search: Efficiently searches only in sorted arrays. Use
Arrays.binarySearch(array, valueToFind)to determine if thevalueToFindexists in the array and, if so, returns its index.
Iterating Through Arrays
There are two common ways to loop through the elements of an array:
forLoop:for (i = 0; i < nums.length; i++) { System.out.print(nums[i]); // Accessing each element }Enhanced
forLoop (For-Each Loop):for (int i : nums) { System.out.print(i); }
Strings in Java: A Detailed Explanation
Strings represent sequences of characters enclosed in double quotes (" "). In Java, strings are immutable, meaning their content cannot be changed after creation. Modifications require using classes like StringBuilder or StringBuffer.
String Declaration
String s = new String("Welcome");
Built-in String Methods
length(): Returns the length of the string.charAt(index): Returns the character at a specific index.contains(substring): Checks if the string contains a specified substring.toCharArray(): Converts the string into a character array.substring(startIndex, endIndex): Extracts a portion of the string.replace(oldString, newString): Replaces occurrences of a substring.replaceFirst(oldString, newString): Replaces only the first occurrence of a substring.concat(string2): Concatenates (joins) two strings.indexOf([character](/browse/animation-3d/animation-skills)): Returns the index of the first occurrence of a character.trim(): Removes leading and trailing whitespace.toUpperCase(): Converts the string to uppercase.toLowerCase(): Converts the string to lowercase.
StringBuilder: Mutable Strings
StringBuilder is a mutable class, allowing you to modify strings without creating new string objects for each change.
StringBuilder Methods
All string built-in methods are available in StringBuilder as well.
append(string): Appends a string to theStringBuilder.insert(startIndex, string): Inserts a string at a specific index.
Need a web app built? Hire the team behind TutorialSearch.io
We ship fast, production-grade full-stack apps (Next.js, React, edge infra) — the same stack that powers this site.