diff --git a/Assignment1/Q1/.idea/compiler.xml b/Assignment1/Q1/.idea/compiler.xml deleted file mode 100644 index 96cc43e..0000000 --- a/Assignment1/Q1/.idea/compiler.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Assignment1/Q1/.idea/copyright/profiles_settings.xml b/Assignment1/Q1/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf3..0000000 --- a/Assignment1/Q1/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/Assignment1/Q1/.idea/description.html b/Assignment1/Q1/.idea/description.html deleted file mode 100644 index db5f129..0000000 --- a/Assignment1/Q1/.idea/description.html +++ /dev/null @@ -1 +0,0 @@ -Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/Assignment1/Q1/.idea/misc.xml b/Assignment1/Q1/.idea/misc.xml deleted file mode 100644 index e29b6f0..0000000 --- a/Assignment1/Q1/.idea/misc.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/Assignment1/Q1/.idea/modules.xml b/Assignment1/Q1/.idea/modules.xml deleted file mode 100644 index dba418f..0000000 --- a/Assignment1/Q1/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/Assignment1/Q1/.idea/project-template.xml b/Assignment1/Q1/.idea/project-template.xml deleted file mode 100644 index 1f08b88..0000000 --- a/Assignment1/Q1/.idea/project-template.xml +++ /dev/null @@ -1,3 +0,0 @@ - \ No newline at end of file diff --git a/Assignment1/Q1/.idea/uiDesigner.xml b/Assignment1/Q1/.idea/uiDesigner.xml deleted file mode 100644 index e96534f..0000000 --- a/Assignment1/Q1/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Assignment1/Q1/.idea/workspace.xml b/Assignment1/Q1/.idea/workspace.xml deleted file mode 100644 index 0c9f617..0000000 --- a/Assignment1/Q1/.idea/workspace.xml +++ /dev/null @@ -1,440 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1495115303135 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Assignment1/Q1/src/Main.java b/Assignment1/Q1/Main.java similarity index 90% rename from Assignment1/Q1/src/Main.java rename to Assignment1/Q1/Main.java index 3685359..38d8283 100644 --- a/Assignment1/Q1/src/Main.java +++ b/Assignment1/Q1/Main.java @@ -34,7 +34,7 @@ public static void main(String[] args) }//if else { - if(checkPermutation(string1.toLowerCase(), string2.toLowerCase())) + if(checkPermutation(string1, string2)) { System.out.println("\" " + string1 + " \" is a permutation of \" " + string2 + " \"!"); }//if @@ -49,6 +49,10 @@ public static void main(String[] args) // Check if the number of characters of each type are the same for both strings public static boolean checkPermutation(String s1, String s2) { + // Transform s1 and s2 into low cases + s1 = s1.toLowerCase(); + s2 = s2.toLowerCase(); + char[] charS1 = s1.toCharArray(); char[] charS2 = s2.toCharArray(); diff --git a/Assignment1/Q1/src/Main.class b/Assignment1/Q1/src/Main.class deleted file mode 100644 index fd0866f..0000000 Binary files a/Assignment1/Q1/src/Main.class and /dev/null differ diff --git a/Assignment1/Q2/.idea/compiler.xml b/Assignment1/Q2/.idea/compiler.xml deleted file mode 100644 index 96cc43e..0000000 --- a/Assignment1/Q2/.idea/compiler.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Assignment1/Q2/.idea/copyright/profiles_settings.xml b/Assignment1/Q2/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf3..0000000 --- a/Assignment1/Q2/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/Assignment1/Q2/.idea/misc.xml b/Assignment1/Q2/.idea/misc.xml deleted file mode 100644 index c6d8fb7..0000000 --- a/Assignment1/Q2/.idea/misc.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Assignment1/Q2/.idea/modules.xml b/Assignment1/Q2/.idea/modules.xml deleted file mode 100644 index a22f264..0000000 --- a/Assignment1/Q2/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/Assignment1/Q2/.idea/workspace.xml b/Assignment1/Q2/.idea/workspace.xml deleted file mode 100644 index 5b50305..0000000 --- a/Assignment1/Q2/.idea/workspace.xml +++ /dev/null @@ -1,548 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1495140747076 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - No facets are configured - - - - - - - - - - - - - - - 1.8 - - - - - - - - Q2 - - - - - - - - 1.8 - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Assignment1/Q2/src/Main.java b/Assignment1/Q2/Main.java similarity index 100% rename from Assignment1/Q2/src/Main.java rename to Assignment1/Q2/Main.java diff --git a/Assignment1/Q2/Q2.iml b/Assignment1/Q2/Q2.iml deleted file mode 100644 index c90834f..0000000 --- a/Assignment1/Q2/Q2.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/Assignment1/Q2/src/Singly_Linked_List.java b/Assignment1/Q2/Singly_Linked_List.java similarity index 94% rename from Assignment1/Q2/src/Singly_Linked_List.java rename to Assignment1/Q2/Singly_Linked_List.java index 78b693c..5880b27 100644 --- a/Assignment1/Q2/src/Singly_Linked_List.java +++ b/Assignment1/Q2/Singly_Linked_List.java @@ -70,8 +70,4 @@ int getElement(int k) return -1; }//getElement -/* int getSize() - { - return size; - }//getSize*/ }//Singly_List_List \ No newline at end of file diff --git a/Assignment1/Q2/src/Main.class b/Assignment1/Q2/src/Main.class deleted file mode 100644 index 5830728..0000000 Binary files a/Assignment1/Q2/src/Main.class and /dev/null differ diff --git a/Assignment1/Q2/src/Node.class b/Assignment1/Q2/src/Node.class deleted file mode 100644 index 9e28ff4..0000000 Binary files a/Assignment1/Q2/src/Node.class and /dev/null differ diff --git a/Assignment1/Q2/src/Singly_Linked_List.class b/Assignment1/Q2/src/Singly_Linked_List.class deleted file mode 100644 index 2c386d5..0000000 Binary files a/Assignment1/Q2/src/Singly_Linked_List.class and /dev/null differ diff --git a/Assignment2/Q1/src/BinaryTree.java b/Assignment2/Q1/BinaryTree.java similarity index 100% rename from Assignment2/Q1/src/BinaryTree.java rename to Assignment2/Q1/BinaryTree.java diff --git a/Assignment2/Q1/src/Main.java b/Assignment2/Q1/Main.java similarity index 100% rename from Assignment2/Q1/src/Main.java rename to Assignment2/Q1/Main.java diff --git a/Assignment2/Q1/src/NodeBinaryTree.java b/Assignment2/Q1/NodeBinaryTree.java similarity index 100% rename from Assignment2/Q1/src/NodeBinaryTree.java rename to Assignment2/Q1/NodeBinaryTree.java diff --git a/Assignment2/Q2/src/BinaryTree.java b/Assignment2/Q2/BinaryTree.java similarity index 100% rename from Assignment2/Q2/src/BinaryTree.java rename to Assignment2/Q2/BinaryTree.java diff --git a/Assignment2/Q2/src/Main.java b/Assignment2/Q2/Main.java similarity index 100% rename from Assignment2/Q2/src/Main.java rename to Assignment2/Q2/Main.java diff --git a/Assignment2/Q2/src/NodeBinaryTree.java b/Assignment2/Q2/NodeBinaryTree.java similarity index 100% rename from Assignment2/Q2/src/NodeBinaryTree.java rename to Assignment2/Q2/NodeBinaryTree.java diff --git a/Assignment3/Dictionary.java b/Assignment3/Dictionary.java new file mode 100644 index 0000000..e7ba0c8 --- /dev/null +++ b/Assignment3/Dictionary.java @@ -0,0 +1,18 @@ +/** + * Created by Alice Colt. + * + * + * Interface Word Dictionary + */ + +public interface Dictionary +{ + + // Returns if the given 'str' is a valid word + boolean isWord(String str); + + // Returns if the given 'str' is a prefix of at least one word in the dictionary + boolean isPrefix(String str); + + +}// Dictionary interface diff --git a/Assignment3/WordSearch.java b/Assignment3/WordSearch.java new file mode 100644 index 0000000..84f3017 --- /dev/null +++ b/Assignment3/WordSearch.java @@ -0,0 +1,113 @@ +import java.util.*; + +/** + * Created by Alice Colt. + */ +public class WordSearch { + + + // Search for a WORD - using list + + static List wordSearchGrid(char[][] characters, + int row, + int column, + Dictionary dictionary){ + + + // List for keeping all the solutions + List solution = new LinkedList<>(); + + boolean[][] visited = new boolean[row][column]; + + + for (int index = 0; index < row; index++) + { + for (int indexj = 0; indexj < column; indexj++) + { + Set validWord = wordGetter(Character.toString(characters[index][indexj]), + index, indexj, + row, column, + characters,visited,dictionary); + + // Add to solution list + solution.addAll(validWord); + }//for + }//for + + + // Return solution list + return new ArrayList<>(solution); + + }// List wordSearchInGrid + + + + // Get word - using hashes + + static Set wordGetter(String prefix, + int indexRow, int row, + int indexColumn, int column, + char[][] characters, + boolean[][] visited, + Dictionary dictionary) + { + + // Set for keeping all solutions + Set solution = new HashSet<>(); + + // If prefix is part of a dictionary word, add to solutions. + if(dictionary.isWord((prefix))) + { + solution.add(prefix); + }// if + // Else return solution word without adding anything + else + { + return solution; + }// else + + + + int newRow = 0; + int newCol = 0; + + for(int index = -1; index < 2; index++) + { + for (int indexj = -1; indexj < 2; indexj++) + { + newRow = indexRow + index; + newCol = indexColumn + indexj; + + //if not out of grid + if((newRow >= 0) && (newRow < indexRow ) && + newCol >= 0 && (newCol < indexColumn) && + !visited[newRow][newCol]) + { + + String newSol = prefix + characters[newRow][newCol]; + + /* + Mark the visited one after getting the word in words set + Does not need to be taken again in the same wordsSolution set + */ + visited[indexRow][indexColumn] = true; + + + Set wordsSolution = wordGetter(newSol, newRow, indexRow, + newCol, indexColumn, + characters, visited, dictionary); + + + visited[newRow][newCol] = false; + solution.addAll(wordsSolution); + + }// if + }//for + }//for + + return solution; + + }// wordGetter + + +}//WordSearch \ No newline at end of file diff --git a/Assignment4/CountingIslands.java b/Assignment4/CountingIslands.java new file mode 100644 index 0000000..33a3cd3 --- /dev/null +++ b/Assignment4/CountingIslands.java @@ -0,0 +1,58 @@ +import java.util.*; +import java.lang.*; +import java.io.*; + +public class CountingIslands +{ + int CountingIslands(int noRows, int noCols, boolean[][] landOrWater) + { + int index, indexj; + // Make a bool array to mark visited cells. + boolean visited[][] = new boolean[noRows][noCols]; + + // Count the islands + int count = 0; + + for (index = 0; index < noRows; index++) + for (indexj = 0; indexj < noCols; indexj++) + if (landOrWater[index][indexj] == true && !visited[index][indexj]) + { + DFS(index, indexj,landOrWater, visited); + count = count++; + } + + return count; + } + + + // Check if cell: (indexRow, indexCol) can be made + boolean isSafe(int indexRow, int indexCol, boolean landOrWater[][], + boolean visited[][]) + { + // Check if indexRow and indexCol numbers are in the landOrWater range + // Check if current position is land or water + return (indexRow < noRows) && (indexRow >= 0) && + (indexCol < noCols) && (indexCol >= 0) && + (landOrWater[indexRow][indexCol] == true) && (!visited[indexRow][indexCol]); + } + + + // DFS taking the 4 neighbors + void DFS(int indexRow, int indexCol, boolean landOrWater[][], boolean visited[][]) + { + // Mark cell - visited + visited[indexRow][indexCol] = true; + + + // Array with neighbors + int rowNeighb[] = new int[] {0, -1, 0, 1}; + int colNeighb[] = new int[] {-1, 0, 1, 0}; + + + // DFS for all neighbours + for (int index = 0; index < 4; index++) + if (isSafe(indexRow + rowNeighb[index], indexCol + colNeighb[index], landOrWater, visited) ) + DFS(indexRow + rowNeighb[index], indexCol + colNeighb[index],landOrWater, visited); + } + +} \ No newline at end of file diff --git a/README.md b/README.md index b1dcf57..b6ee02c 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,56 @@ This repository is for the completed assignments given by Google as part of Code There will be a total of six assignments completed in Java. + +****************************** +Assignment 1 - Getting Started + Q1 Task: + "Given two strings, write a method to decide if one is a permutation of the other." + e.g. Listen, Silent ⇒ True Triangle, Integral ⇒ True Apple, Pabble ⇒ False + Q2 Task: + "Implement an algorithm to find the kth to last element of a singly linked list." + e.g. k=1 +****************************** + +****************************** +Assignment 2 - Ancestors + Q1 - Print Ancestors: + "Given a Binary Tree and a key, write a function that prints all the ancestors of the key in the given binary tree." + + Q2 - Common Ancestor + "Design an algorithm and write code to find the lowest common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure." +****************************** + +****************************** +Assignment 3 - Word Search + + "Given a grid of letters and a dictionary, find all the words from the dictionary that can be formed in the grid. + The rules for forming a word: + ● You can start at any position. + ● You can move to one of the 8 adjacent cells (horizontally/vertically/diagonally). + ● You can't visit the same cell twice in the same word. + Assume you have a dictionary class with these two methods: + ● isWord(string): Returns whether the given string is a valid word. + ● isPrefix(string): Returns whether the given string is a prefix of at least one word in the + dictionary. + + Visual example with 2 rows, 3 columns: + The dictionary is made up of entries [CAR, CARD, CART, CAT], the list of prefixes is [C, CA, CAR, CARD, CART, CAT]. The correct output in this case would be [CAR, CARD, CAT]." +****************************** + +****************************** +Assignment 4 - Counting Islands + + Given a 2-dimensional map of tiles. Each tile is either land or water. Write a function that counts the number of islands. + Two tiles belong to the same island if they are both land and are adjacent horizontally or vertically, but not diagonally. + The input to your function is the number of rows, number of columns, and a 2-dimensional array of booleans, where false means water and true means land. The function should simply return the number of islands. +****************************** + +****************************** +Assignment 5 +****************************** + +****************************** +Assignment 6 +****************************** +