Skip to content

Commit c8c4403

Browse files
committed
Feat/ Completed findWordsContaining method and gradle check is successful with only 1 file
1 parent e93f35c commit c8c4403

File tree

1 file changed

+37
-25
lines changed
  • lesson_11/arrays_java/arrays_app/src/main/java/com/codedifferently/lesson11

1 file changed

+37
-25
lines changed
Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.codedifferently.lesson11;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
public class Lesson11 {
@@ -10,46 +11,57 @@ public class Lesson11 {
1011
*/
1112
public static int[] getConcatenation(int[] nums) {
1213

13-
int n = nums.length; //Creates a variable named n that is the length of nums
14-
int[] ans = new int[2 * n]; //Creates a new array that is double the size of n.
14+
int n = nums.length; // Creates a variable named n that is the length of nums
15+
int[] ans = new int[2 * n]; // Creates a new array that is double the size of n.
1516

16-
17-
for (int i = 0; i < n; ++i) {
18-
ans[i] = nums[i]; //Gets the first half of the int array
19-
ans[i + n] = nums[i]; //Gets the second half of the int array
20-
}
21-
return ans;
17+
for (int i = 0; i < n; ++i) {
18+
ans[i] = nums[i]; // Gets the first half of the int array
19+
ans[i + n] = nums[i]; // Gets the second half of the int array
2220
}
21+
return ans;
22+
}
2323

24-
25-
public static void main(String[] args) {
26-
27-
//Creates an instance of the solution class
28-
//We need this because getConcatenation() is a method that is non-static meaning we need and object of Solution to use it.
29-
//This would be unneeded if getConcatenation() was static
30-
24+
public static void main(String[] args) {
3125

32-
int[] nums = {1, 2, 3, 4}; //Array of nums based on Test input
33-
int[] result = getConcatenation(nums); //calls the getConcatenation method giving it the array of nums.
26+
// Creates an instance of the solution class
27+
// We need this because getConcatenation() is a method that is non-static meaning we need and
28+
// object of Solution to use it.
29+
// This would be unneeded if getConcatenation() was static
3430

31+
int[] nums = {1, 2, 3, 4}; // Array of nums based on Test input
32+
int[] result =
33+
getConcatenation(nums); // calls the getConcatenation method giving it the array of nums.
3534

36-
//Prints result out.
35+
// Prints result out.
3736

38-
//You need to loop out the result because we want to display the contents of an array which means we need to call each instance in it. This is easily done by using the for else loop to run until it is returned false.
39-
for (int num: result) {
40-
System.out.print(num + " ");
41-
}
37+
// You need to loop out the result because we want to display the contents of an array which
38+
// means we need to call each instance in it. This is easily done by using the for else loop to
39+
// run until it is returned false.
40+
for (int num : result) {
41+
System.out.print(num + " ");
4242
}
43-
}
4443

44+
String[] words = {
45+
"leet", "code", "hello", "world"
46+
}; // Creates an Array named words that contains a series of strings.
4547

48+
char x = 'e'; // Calls for a specific char
49+
System.out.println(findWordsContaining(words, x)); // Prints out findWordsContaining method
4650
}
4751

4852
/**
4953
* Provide the solution to LeetCode 2942 here:
5054
* https://leetcode.com/problems/find-words-containing-character/
5155
*/
52-
public List<Integer> findWordsContaining(String[] words, char x) {
53-
return null;
56+
public static List<Integer> findWordsContaining(String[] words, char x) {
57+
ArrayList<Integer> elListo = new ArrayList<>(); // Create an arraylist named ElListo
58+
59+
// Runs a loop that will iterate through each character in the word.
60+
for (int i = 0; i < words.length; i++) {
61+
if (words[i].indexOf(x) != -1) { // will check to see if 'x' exists inside of the word
62+
elListo.add(i); // Stores the index if it is true that it has that character
63+
}
64+
}
65+
return elListo; // Will return elListo ArrayList to main
5466
}
5567
}

0 commit comments

Comments
 (0)