Udemy
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Solving Arrays & Strings Leet code questions [Java]
Rating: 4.0 out of 5(26 ratings)
1,721 students

Solving Arrays & Strings Leet code questions [Java]

Problem solving in Array and String
Created byMonish Njs
Last updated 2/2023
English
English [Auto],

What you'll learn

  • Solving Arrays and String questions
  • Using two pointer approach
  • Finding missing and duplicate elements in Array / strings
  • Searching an element in Array / String

Course content

3 sections11 lectures1h 58m total length
  • What is Array and How do we work with Arrays6:21

    Explore what an array is, how it stores elements of the same data type, and how to declare, access by index, manage its length, and use sort and fill.

  • How to work with Strings7:58
  • What is List and it's types8:00

    List:

    size is dynamic

    They store in the order we provide and it can have duplicates

    Declaration

    List<Data type> varName = new ArrayList<>();

    List<Data type> varName = new LinkedList<>();

    1. ArrayList

    List<String> a1 = new ArrayList();
    a1.add(“Zara”);
    a1.add(“Mahnaz”);
    a1.add(“Ayan”);
    System.out.println(“ ArrayList Elements”);
    System.out.print(“\t” + a1);

    Output

    ArrayList Elements
    [Zara, Mahnaz, Ayan]

    2. Linked List

    List<String> l1 = new LinkedList<>();
    l1.add(“Zara”);
    l1.add(“Mahnaz”);
    l1.add(“Ayan”);
    System.out.println();
    System.out.println(“ LinkedList Elements”);
    System.out.print(“\t” + l1);

    output:
    LinkedList Elements
    [Zara, Mahnaz, Ayan]

    Built in methods in List-
    add(value), add(index, value),

    remove(value),

    get(key),

    contains(value) returns true or false
    isEmpty() return true or false,
    clear(),
    size() returns int
    removeLast()
    removeFirst()

    Difference between Array List & Linked List:

    Array List stores values sequentially, so if we add or remove a value it needs more time to shift its elements and adjust

    LinkedList stores randomly in memory and links by pointers. inserting and deleting takes less time here

  • Set and it's types6:54

    Set:

    Set can’t have duplicates

    Types of Set

    HashSet — stores values in random order and it is unsorted

    LinekedHashset — stores in order we give
    Treeset — stores in asc order

    When to use Set

    1. When we want to find union / intersection / difference in two set of data, we can use sets.

    2. Whenever we want to find a missing or a duplicate element we can first about Sets

    Declaration and initialisation:

    Set<Integer> set = new HashSet<>();

    set.add(5);

    set.add(18);

    Build in methods -

    add(value) — returns t/f,
    remove(value),
    contains(value) — returns t/f,
    isEmpty() return true or false,
    clear(),
    size() returns int

    Union — set1.addAll(set2);

    Intersection — set1.retainAll(set2);

    Difference between sets — set1.removeAll(set2);

Requirements

  • It is good to know about Java collection framework such as List, set, Hashmap etc

Description

Array is a very basic data structure representing a group of similar elements, accessed by index. Array data structure can be effectively stored inside the computer and provides fast access to the all its elements


Arrays store values of the same data type. Address of elements are stored consecutively in memory
length is fixed. inserting or deleting an element in 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 seach 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 array or not.

Arrays.binarySearch(array,value to find) -> returns index


Iterating Arrays

Let us consider we loop the elements in an integer array, we can do it like 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 sequence of character values. They will be is 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 particular substring — contains()

Converting string to char array — toCharArray()

taking substrings — substring()

Replacing in string — replace()

Replacing first occurence — replaceFirst()

joining two strings — string1.concat(string2)

getting index of a character — string.indexOf(character)

removes beginnind 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 particular index

string.delete(startIndex, endIndex) — deleted string from the start to 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