Udemybeginneren

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.

3.9(25)
1,597students
1h 58m
Monish Njs
Monish NjsVice President @ BNY

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:

  1. Linear Search: Iterates through the entire array to check for the target element.
  2. Binary Search: Efficiently searches only in sorted arrays. Use Arrays.binarySearch(array, valueToFind) to determine if the valueToFind exists 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:

  1. for Loop:

    for (i = 0; i < nums.length; i++) {
        System.out.print(nums[i]); // Accessing each element
    }
    
  2. Enhanced for Loop (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 the StringBuilder.
  • insert(startIndex, string): Inserts a string at a specific index.
$ whoami — the devs behind tutorialsearch.io

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.

5.0(1,006)
34m
expert
5.0(23)
1h 48m
intermediate

Problem Solving Course Creation

by Reframe ONE Learning

5.0(430)
43m
intermediate
5.0(8)
1h 30m
all