Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
102 changes: 102 additions & 0 deletions GabijaBern/assignment1/Anagrams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package Task1;
/*Q1: Given two strings, determine if one is an anagram of the other.
*Parts: 1)stringSensitive()
* 2)stringInsensitive()
* 3)sentences()
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Anagrams {

/* stringSensitive is a method for checking if two given words are anagrams of each other
* @return returns true if given words are anagrams, upper case letter is not the same as lower case letter
* @param firstWord String holding first word
* @param secondWord String holding second word for comparings
*/
public static boolean stringSensitive(String firstWord, String secondWord)
{
//we could first check if those words are the same length
if(firstWord.length()!=secondWord.length()){
return false;
}

Map<Character, Integer> firstMap = createCharMap(firstWord);
Map<Character, Integer> secondMap = createCharMap(secondWord);

return firstMap.equals(secondMap);
}

/*stringInsensitive method for checking if two words are anagrams
* @return boolean returns true if given two words are anagrams of each other ignoring upper case letters
*/
public static boolean stringInsensitive(String firstWord, String secondWord){

//we don't want to distinguish lower and upper case letter, so we
//just turn all of them to lower
firstWord = firstWord.toLowerCase();
secondWord = secondWord.toLowerCase();

return stringSensitive(firstWord, secondWord);
}

/*
* Method for checking if given two sentences are anagrams of each other
*
* @return returns true if two sentences are anagrams of each other
*/
public static boolean sentences(String firstSentence, String secondSentence){

int howManyWordsMatched = 0;

String[] firstSentenceWords = firstSentence.split("\\W+");
String[] secondSentenceWords = secondSentence.split("\\W+");

List<Map<Character, Integer>> firstListOfMaps = new ArrayList<Map<Character, Integer>>();
List<Map<Character, Integer>> secondListOfMaps = new ArrayList<Map<Character, Integer>>();

if(firstSentenceWords.length==firstSentenceWords.length){
//we know that sentences have the same length
for(int index=0; index < firstSentenceWords.length; index++){
firstListOfMaps.add(createCharMap(firstSentenceWords[index]));
secondListOfMaps.add(createCharMap(secondSentenceWords[index]));
}

for(int index=0; index<firstSentenceWords.length; index++){
secondListOfMaps.remove(firstListOfMaps.get(index));
howManyWordsMatched++;
}

if(firstSentenceWords.length==howManyWordsMatched) {
return true;
}
}
return false;
}

/*
* Method helper for creating a hashmap for a word
* @return Map<Character, Integer> returns a map with characters and their frequency
*/
public static Map<Character, Integer> createCharMap(String word){
//variable for map, though, not needed :))
Map<Character, Integer> mapCreated = new HashMap<Character,Integer>();

//char array for characters in string
char[] charInString = word.toCharArray();

for(char charKey : charInString){
//if the character already appears in the string, we increase its value in the map
if(mapCreated.containsKey(charKey)){
mapCreated.put(charKey, mapCreated.get(charKey)+1);
}
else {
mapCreated.put(charKey, 1);
}
}

return mapCreated;
}//createCharMap method
}
32 changes: 32 additions & 0 deletions GabijaBern/assignment1/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package Task1;
/* Node class for creating a list
* it holds a reference to next Node and has a variable for value of current node
*/
public class Node<T>
{
public Node<T> next;
private final T value;

public Node(T data)
{
this.value = data;
}

/* method for connecting to next node
* @param Node<T> next node that needs to be added to the list
*/
public void setNext(Node<T> nextNode)
{
this.next = nextNode;
}

public T getValue()
{
return value;
}

public void printNode()
{
System.out.println("Node value is: " + value);
}
}
62 changes: 62 additions & 0 deletions GabijaBern/assignment1/SingleLinkList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package Task1;
/*SingleLinkList class for implement an algorithm to find the kth to last element of a singly linked list
*
*/
public class SingleLinkList<T>
{
private Node<T> head = null;
int numberOfNodes=0;

/* main method for testing the program
*
*/
public static void main(String[] args)
{
SingleLinkList<String> test1 = new SingleLinkList<String>();

test1.addAtHead(new Node<String>("Last"));
test1.addAtHead(new Node<String>("4th"));
test1.addAtHead(new Node<String>("3th"));
test1.addAtHead(new Node<String>("2th"));
test1.addAtHead(new Node<String>("1th"));


Node<String> nodeTest = test1.getTheElementFromEnd(test1.head, 3);
nodeTest.printNode();
}

/* addHead method for adding nodes in front of the list
* @param newHead given Node reference, this node is "put" in front of the list
*/
public void addAtHead(Node<T> newHead)
{
newHead.setNext(head);
head = newHead;
}

/* getTheElementFromEnd recursive method for getting the element from the back
*we go to the end of the list and then "move" back
*and check if the index we are looking matches the numberOfNodes
*which is a variable that we increment as we go back to the
*beginning of the list
*
*@param head the head of the linkedlist from which the element should be returned
*@param index indicates the kth index from the back of the list that we want to return
*
*@return Node<T> method returns the kth element from the back of the list
*/
public Node<T> getTheElementFromEnd(Node<T> head, int index)
{
Node<T> resultNode = null;

if(head!=null)
{
resultNode = getTheElementFromEnd(head.next, index);

if(numberOfNodes++==index)
resultNode = head;
}
return resultNode;
}

}