Skip to content

Feat/Adds a getConcatenation and findWordsContaining method in Java and TS #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.codedifferently.lesson11;

import java.util.ArrayList;
import java.util.List;

public class Lesson11 {
Expand All @@ -8,15 +9,59 @@ public class Lesson11 {
* Provide the solution to LeetCode 1929 here:
* https://leetcode.com/problems/concatenation-of-array
*/
public int[] getConcatenation(int[] nums) {
return null;
public static int[] getConcatenation(int[] nums) {

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

for (int i = 0; i < n; ++i) {
ans[i] = nums[i]; // Gets the first half of the int array
ans[i + n] = nums[i]; // Gets the second half of the int array
}
return ans;
}

public static void main(String[] args) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not have this here. What purpose does it serve?


// Creates an instance of the solution class
// We need this because getConcatenation() is a method that is non-static meaning we need and
// object of Solution to use it.
// This would be unneeded if getConcatenation() was static

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

// Prints result out.

// 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.
for (int num : result) {
System.out.print(num + " ");
}

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

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

/**
* Provide the solution to LeetCode 2942 here:
* https://leetcode.com/problems/find-words-containing-character/
*/
public List<Integer> findWordsContaining(String[] words, char x) {
return null;
public static List<Integer> findWordsContaining(String[] words, char x) {
ArrayList<Integer> elListo = new ArrayList<>(); // Create an arraylist named ElListo

// Runs a loop that will iterate through each character in the word.
for (int i = 0; i < words.length; i++) {
if (words[i].indexOf(x) != -1) { // will check to see if 'x' exists inside of the word
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments should be on the line above the code it annotates, not after the line.

elListo.add(i); // Stores the index if it is true that it has that character
}
}
return elListo; // Will return elListo ArrayList to main
}
}
51 changes: 43 additions & 8 deletions lesson_11/arrays_ts/src/lesson11.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@

/**
* Provide the solution to LeetCode 1929 here:
* https://leetcode.com/problems/concatenation-of-array
*/
* Provide the solution to LeetCode 1929 here:
* https://leetcode.com/problems/concatenation-of-array
*/
export function getConcatenation(nums: number[]): number[] {
return [];
const n = nums.length; //Creates a variable named n that is the length of nums
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to fix code formatting here, looks wrong.

const ans: number[] = new Array(2 * n); //Creates a new array that is double the size of n


for(let i = 0; i < n; ++i) {
ans[i] = nums[i];
ans[i + n] = nums[i];


}
return ans;
}


/**
* Provide the solution to LeetCode 2942 here:
* https://leetcode.com/problems/find-words-containing-character/
*/
* Provide the solution to LeetCode 2942 here:
* https://leetcode.com/problems/find-words-containing-character/
*/
export function findWordsContaining(words: string[], x: string): number[] {
return [];
const elListo: number[] = [] //creates a new array named elListo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is elListo? Fix variable name please to something more clear.



//Runs a loop that will iterate through each character in the String.
for (let i = 0; i < words.length; ++i) {
if (words[i].includes(x)) { //Checks to see if the word inclues the char 'x'
elListo.push(i); //Pushes the index of i if it is true that the word contains that character

}
} return elListo; //Will return the Array elListo
}


//Test Cases
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, remove this and the stuff below. This code should be in formal test files.



const nums: number[] = [1, 2 , 3, 4]; //Array of numbers based on test case
const result: number[] = getConcatenation(nums);
console.log(result.join(' '))//Prints the test result out for the fet concatentaition method


const words: string[] = ['leet', 'code', 'hello', 'world']; //Array of strings based on test case
const x = 'e'; //Calls for a specific char
console.log(findWordsContaining(words, x));