From 02b87e9fc8e2924207c00a3058a557b820a330fa Mon Sep 17 00:00:00 2001 From: Peng Yue Date: Thu, 7 Jun 2018 11:48:14 +0100 Subject: [PATCH] Add BFS/DFS tree, regex and digitsum --- Java/.DS_Store | Bin 0 -> 6148 bytes Java/src/.DS_Store | Bin 0 -> 6148 bytes Java/src/com/.DS_Store | Bin 0 -> 6148 bytes Java/src/com/godepth/.DS_Store | Bin 0 -> 6148 bytes Java/src/com/godepth/Anagrams/Main.java | 111 +++++++++++++++++ Java/src/com/godepth/BFS/BFSNode.java | 35 ++++++ Java/src/com/godepth/BFS/BFSTree.java | 41 +++++++ Java/src/com/godepth/BFS/QueueBFSTree.java | 29 +++++ Java/src/com/godepth/DFS/DFSNode.java | 35 ++++++ Java/src/com/godepth/DFS/DFSTraversal.java | 42 +++++++ Java/src/com/godepth/DFS/StackDSTTree.java | 28 +++++ .../com/godepth/DiagonalDifference/Main.java | 33 ++++++ Java/src/com/godepth/DigitSum/Main.java | 58 +++++++++ Java/src/com/godepth/Main.java | 112 +++++++++++++++++- Java/src/com/godepth/regex/Regex.java | 55 +++++++++ .../com/godepth/regex/SimpleRegexEngine.java | 22 ++++ .../regex/SimpleRegexEngineByStep.java | 52 ++++++++ Java/src/src.iml | 12 ++ 18 files changed, 663 insertions(+), 2 deletions(-) create mode 100644 Java/.DS_Store create mode 100644 Java/src/.DS_Store create mode 100644 Java/src/com/.DS_Store create mode 100644 Java/src/com/godepth/.DS_Store create mode 100644 Java/src/com/godepth/Anagrams/Main.java create mode 100644 Java/src/com/godepth/BFS/BFSNode.java create mode 100644 Java/src/com/godepth/BFS/BFSTree.java create mode 100644 Java/src/com/godepth/BFS/QueueBFSTree.java create mode 100755 Java/src/com/godepth/DFS/DFSNode.java create mode 100644 Java/src/com/godepth/DFS/DFSTraversal.java create mode 100644 Java/src/com/godepth/DFS/StackDSTTree.java create mode 100644 Java/src/com/godepth/DiagonalDifference/Main.java create mode 100644 Java/src/com/godepth/DigitSum/Main.java create mode 100644 Java/src/com/godepth/regex/Regex.java create mode 100644 Java/src/com/godepth/regex/SimpleRegexEngine.java create mode 100644 Java/src/com/godepth/regex/SimpleRegexEngineByStep.java create mode 100644 Java/src/src.iml diff --git a/Java/.DS_Store b/Java/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..95ff2b3f50ed20be30fb399d89f6a02e563f2d73 GIT binary patch literal 6148 zcmeHK&2AGh5FRH1oFGIFsPwoOBn}lyDix?Iq*Vxq-jEi-0Z_Y}M9oUub+o%7q9JHs z1Kxl~;R$#g_{MfBO3Ia1Xhs@; zL0r7$+RM0_CfO*enKSo{vl0Pvjc3 zZtM0@cYC?)Ki%Ei33gXI!Lq;CgZcQ${%Y0M_aF419Uh;4JX?NReg0ynNeVx_mAf9- z@D+w9;X_p9naNM!Sx2v<1cd=%Kp41E2K=U^Z(r$G$l$_&FmN3i;Qb+hGKL;&hj!~g zVV?j%zD8Ps&DTQC2|R`#YlmooD3=O!smebwluL&{@NuEX+M!D)w7 z-hfBp33wd%W_PFvg=?jt8EN*Lot;^GzqNKfL}W(u@sOxbL>9`}>Z19J@Hp!wX^E!; zG(3kp$qJXD$J4270t38vZ8{-G)5BtI{~lxH?hh}1Arh4>sx#T;Z`oOqSM^}<-L%@B zYuC4|?b+VV-rMBVEs{E|=hY~kz2@HYv_8$me&JkkVW-*4e6l#|-+t=KI?c<;%qQpL z2}0hy%FD4^jNH5&SAL!(3|QN>-J|}_ayfjsx4#?ht#+g3aDM>v{)0!WRo8Cc89q5Y zo__eaJYRkK9CpIsBlU7i@E3f6AA023X2>|5ZNH4Jc&p$9?2QU_F z0>T3^Ar%->jg%NhY6lb2F&;~N6ELKc@lVD)@?;|wijmU8L_VBM98g zzW<;7yZ&EK(w;J)4E$FNn9f0RFv61LZoRZPzH1}sF_eYlnt&H6F#J`FSiXvPpkCmQ X*Z{_YO+a`c`Xk_J&_)@!QU-nkhrDIU literal 0 HcmV?d00001 diff --git a/Java/src/com/.DS_Store b/Java/src/com/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..515b17b94306322a8fb3a1a2b8b98837a2b37838 GIT binary patch literal 6148 zcmeHK%}T>S5T32oZYyFBf*x}bJoL~C{vkrBdhjMh^q``NDK-$ZrA;kbD|rolBcH&> z(V5+CQL9HqWd>%y$^2}xU&3w%07ShT)&R-?AW;c(MI1g6ij$5>&Uz*gg`T4YDKz(D zbrHm4+3fg@4A8qPz$GXMp#g8+p9(NSpT#EX(u)?f#(aAJf&opEd()A1D=%Y(F@R8ojgf+_p+mKS~;HT^xlC zOgTAErGHY=Nk7#@#<=Sf}u}Us6GOQYAGcp6r05kCC7_b|eoc?o9&JWBC zFazJs0PPPBDxqsJGpM%?9CQnSm`Ar3wCSgjY8;EM#mpc^P=qN(G^N6}7{ZjJ-?4eF z#mt~72Vt8JVec$#ha&XdaehaqgK!P<$P6$8?-`)p52Z}!|L)iQzfR(T8DIwfCj%nC zR&ei literal 0 HcmV?d00001 diff --git a/Java/src/com/godepth/.DS_Store b/Java/src/com/godepth/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9f740500f48d96268bc418a39d5b716d723113e0 GIT binary patch literal 6148 zcmeHK%Sr<=6g{z3>H{fm)ZHvy39dyLr3ylI(;u*PYJH53_`>CE{0DzQ{1`vR572XO zf_5?ru0-TsNKPiX=iZr#rDhYMXO8GJ#ufTF$MtSr z9M-(8!xS(D{+a@^c3arQ1y}9}W9xU27XyyT#<0$^@}|ZX&_D+}=*cSo^m2=l*8;sx zG-{#IbA3L-5r)h&WZxl%QH+NuMoT#(#<@Wcw>XddUnBoEG8FV1AZLc0ybGMMm1+AL zH&y;bKjJU)yM&WrpK&rWclfRa_q1*8?S_aFxgPB$iZ-QQIW=I!YUaj9QtVgR&0aL&f_)tLhhlE8i4On{At%H?b z0f left = new HashMap<>(); + left.put("a", "0"); + left.put("b", "0"); + left.put("c", "0"); + left.put("d", "0"); + left.put("e", "0"); + left.put("f", "0"); + left.put("g", "0"); + left.put("h", "0"); + left.put("i", "0"); + left.put("j", "0"); + left.put("k", "0"); + left.put("l", "0"); + left.put("m", "0"); + left.put("n", "0"); + left.put("o", "0"); + left.put("p", "0"); + left.put("q", "0"); + left.put("r", "0"); + left.put("s", "0"); + left.put("t", "0"); + left.put("u", "0"); + left.put("v", "0"); + left.put("w", "0"); + left.put("x", "0"); + left.put("y", "0"); + left.put("z", "0"); + + Map right = new HashMap<>(); + right.put("a", "0"); + right.put("b", "0"); + right.put("c", "0"); + right.put("d", "0"); + right.put("e", "0"); + right.put("f", "0"); + right.put("g", "0"); + right.put("h", "0"); + right.put("i", "0"); + right.put("j", "0"); + right.put("k", "0"); + right.put("l", "0"); + right.put("m", "0"); + right.put("n", "0"); + right.put("o", "0"); + right.put("p", "0"); + right.put("q", "0"); + right.put("r", "0"); + right.put("s", "0"); + right.put("t", "0"); + right.put("u", "0"); + right.put("v", "0"); + right.put("w", "0"); + right.put("x", "0"); + right.put("y", "0"); + right.put("z", "0"); + + for (char item : leftString.toCharArray()) { + left.put(Character.toString(item), Integer.toString(Integer.parseInt(left.get(Character.toString(item))) + 1)); + System.out.println(Character.toString(item) + ":" + left.get(Character.toString(item))); + } + + System.out.println("====================================================="); + + for (char item : rightString.toCharArray()) { + right.put(Character.toString(item), Integer.toString(Integer.parseInt(right.get(Character.toString(item))) + 1)); + System.out.println(Character.toString(item) + ":" + right.get(Character.toString(item))); + } + + String list[] = {"a","b","c", "d", "e" , "f", "g", "h" , "i", "j", "k" , "l", "m", "n" , "o", "p", "q" , "r", "s", "t" , "u", "v", "w" , "x", "y", "z"}; + + for (String element : list) { + if (Integer.parseInt(left.get(element)) >= 1 && Integer.parseInt(right.get(element)) >= 1) { + anagramString += element; + } else if (Integer.parseInt(left.get(element)) >= 1 && Integer.parseInt(right.get(element)) == 0) { + leftRemoveString += element; + } else if (Integer.parseInt(left.get(element)) == 0 && Integer.parseInt(right.get(element)) >= 1) { + rightRemoveString += element; + } + } + + System.out.println("Anagrams: " + anagramString); + System.out.println("Anagrams remove chars (left): " + leftRemoveString); + System.out.println("Anagrams remove char number (left): " + leftRemoveString.length()); + System.out.println("Anagrams remove chars (right): " + rightRemoveString); + System.out.println("Anagrams remove char number (right): " + rightRemoveString.length()); + System.out.println("Anagrams remove char number (total): " + ((leftRemoveString.length()) + (rightRemoveString.length()))); + + } +} diff --git a/Java/src/com/godepth/BFS/BFSNode.java b/Java/src/com/godepth/BFS/BFSNode.java new file mode 100644 index 0000000..a606eb8 --- /dev/null +++ b/Java/src/com/godepth/BFS/BFSNode.java @@ -0,0 +1,35 @@ +package com.godepth.BFS; + +public class BFSNode { + private T data = null; + private BFSNode left, right; + + public BFSNode(T item) { + data = item; + left = right = null; + } + + public BFSNode getLeft() { + return left; + } + + public void setLeft(BFSNode node) { + left = node; + } + + public BFSNode getRight() { + return right; + } + + public void setRight(BFSNode node) { + right = node; + } + + public T getData() { + return data; + } + + public void setData(T item) { + data = item; + } +} diff --git a/Java/src/com/godepth/BFS/BFSTree.java b/Java/src/com/godepth/BFS/BFSTree.java new file mode 100644 index 0000000..64e36d8 --- /dev/null +++ b/Java/src/com/godepth/BFS/BFSTree.java @@ -0,0 +1,41 @@ +package com.godepth.BFS; + +public class BFSTree { + public BFSNode root; + + public BFSTree() { + root = null; + } + + public void printLevelOrder() { + int height, i; + height = height(root); + + for (i = 1; i <= height; i++) { + printGivenLevel(root, i); + } + } + + private int height(BFSNode root) { + if (root == null) { + return 0; + } else { + int leftHeight = height(root.getLeft()); + int rightHeight = height(root.getRight()); + return ((leftHeight > rightHeight ? leftHeight : rightHeight) + 1); + } + } + + private void printGivenLevel(BFSNode root, int level) { + if (root == null) { + return; + } + + if (level == 1) { + System.out.print(root.getData() + " "); + } else if (level > 1) { + printGivenLevel(root.getLeft(), level - 1); + printGivenLevel(root.getRight(), level - 1); + } + } +} \ No newline at end of file diff --git a/Java/src/com/godepth/BFS/QueueBFSTree.java b/Java/src/com/godepth/BFS/QueueBFSTree.java new file mode 100644 index 0000000..6b4c7b4 --- /dev/null +++ b/Java/src/com/godepth/BFS/QueueBFSTree.java @@ -0,0 +1,29 @@ +package com.godepth.BFS; + +import java.util.Queue; +import java.util.LinkedList; + +public class QueueBFSTree { + + public BFSNode root; + + public void printLevelOrder() + { + Queue> queue = new LinkedList<>(); + queue.add(root); + + while (!queue.isEmpty()) { + + BFSNode temp = queue.poll(); + System.out.print(temp.getData()); + + if (temp.getLeft() != null) { + queue.add(temp.getLeft()); + } + + if (temp.getRight() != null) { + queue.add(temp.getRight()); + } + } + } +} diff --git a/Java/src/com/godepth/DFS/DFSNode.java b/Java/src/com/godepth/DFS/DFSNode.java new file mode 100755 index 0000000..6d4057d --- /dev/null +++ b/Java/src/com/godepth/DFS/DFSNode.java @@ -0,0 +1,35 @@ +package com.godepth.DFS; + +public class DFSNode { + private T data = null; + private DFSNode left, right; + + public DFSNode(T item) { + data = item; + left = right = null; + } + + public DFSNode getLeft() { + return left; + } + + public void setLeft(DFSNode node) { + left = node; + } + + public DFSNode getRight() { + return right; + } + + public void setRight(DFSNode node) { + right = node; + } + + public T getData() { + return data; + } + + public void setData(T item) { + data = item; + } +} \ No newline at end of file diff --git a/Java/src/com/godepth/DFS/DFSTraversal.java b/Java/src/com/godepth/DFS/DFSTraversal.java new file mode 100644 index 0000000..db7baf2 --- /dev/null +++ b/Java/src/com/godepth/DFS/DFSTraversal.java @@ -0,0 +1,42 @@ +package com.godepth.DFS; + +public class DFSTraversal { + + public DFSNode root; + + public void printInorder(DFSNode node) + { + if (node == null) + return; + + printInorder(node.getLeft()); + + System.out.print(node.getData() + " "); + + printInorder(node.getRight()); + } + + public void printPreorder(DFSNode node) + { + if (node == null) + return; + + System.out.print(node.getData() + " "); + + printPreorder(node.getLeft()); + + printPreorder(node.getRight()); + } + + public void printPostorder(DFSNode node) + { + if (node == null) + return; + + printPostorder(node.getLeft()); + + printPostorder(node.getRight()); + + System.out.print(node.getData() + " "); + } +} diff --git a/Java/src/com/godepth/DFS/StackDSTTree.java b/Java/src/com/godepth/DFS/StackDSTTree.java new file mode 100644 index 0000000..36f5717 --- /dev/null +++ b/Java/src/com/godepth/DFS/StackDSTTree.java @@ -0,0 +1,28 @@ +package com.godepth.DFS; + +import java.util.Stack; + +public class StackDSTTree { + + public DFSNode root; + + public void printDepthOrder() + { + Stack> stack = new Stack<>(); + stack.add(root); + + while (!stack.isEmpty()){ + + DFSNode temp = stack.pop(); + System.out.print(temp.getData()); + + if (temp.getLeft() != null) { + stack.push(temp.getLeft()); + } + + if (temp.getRight() != null) { + stack.push(temp.getRight()); + } + } + } +} diff --git a/Java/src/com/godepth/DiagonalDifference/Main.java b/Java/src/com/godepth/DiagonalDifference/Main.java new file mode 100644 index 0000000..04467d5 --- /dev/null +++ b/Java/src/com/godepth/DiagonalDifference/Main.java @@ -0,0 +1,33 @@ +package com.godepth.DiagonalDifference; + +class AbsoluteDifference { + + private int[][] square = { + {3}, + {11, 2, 4}, + {4, 5, 6}, + {10, 8, -12} + }; + + public int count() + { + int length = square[0][0]; + int i; + int left = 0; + int right = 0; + + for (i = 0; i < length ; i++) { + left = left + square[i+1][i]; + right = right + square[i+1][length-i-1]; + } + + return Math.abs(left - right); + } +} + +public class Main { + public static void main(String[] args) { + AbsoluteDifference diff = new AbsoluteDifference(); + System.out.print("Disgonal diff absolute value is " + diff.count()); + } +} diff --git a/Java/src/com/godepth/DigitSum/Main.java b/Java/src/com/godepth/DigitSum/Main.java new file mode 100644 index 0000000..5e27cd4 --- /dev/null +++ b/Java/src/com/godepth/DigitSum/Main.java @@ -0,0 +1,58 @@ +package com.godepth.DigitSum; + +public class Main { + + public static void main(String[] args) + { + DigitSum digitSum = new DigitSum(); + digitSum.calculate(); + } +} + + +class DigitSum +{ + private int[] data = {9875, 3}; + + public void calculate() + { + int finalResult = 0; + int number = data[0]; + int cnt = data[1]; + + //String numberStr = Integer.toString(number); + //char digit[] = numberStr.toCharArray(); + //String[] digit = numberStr.split("(?<=.)"); + + int result = count(number, 0); + + if (cnt > 1) { + result = result * cnt; + finalResult = count(result, 0); + } else { + finalResult = result; + } + + System.out.print("number : " + finalResult); + } + + private int count(int num, int total) + { + int remain = num % 10; + int divide = num / 10; + if (remain > 0 && divide > 0) { + total = total + remain; + total = count(divide, total); + } else if (remain > 0 && divide == 0) { + total = total + remain; + } else if (remain == 0 && divide > 0) { + total = count(divide, total); + } + + if (total > 10) { + total = count(total, 0); + } + + return total; + } +} diff --git a/Java/src/com/godepth/Main.java b/Java/src/com/godepth/Main.java index 26d326a..9a53cfd 100644 --- a/Java/src/com/godepth/Main.java +++ b/Java/src/com/godepth/Main.java @@ -1,11 +1,20 @@ package com.godepth; -import com.godepth.LoopLinkedList.DetectLoop; +import com.godepth.BFS.BFSNode; +import com.godepth.BFS.QueueBFSTree; +import com.godepth.DFS.DFSNode; +import com.godepth.DFS.DFSTraversal; +import com.godepth.DFS.StackDSTTree; import com.godepth.FindNodeFromEndLinklist.FindNodeEfficient; import com.godepth.FindNodeFromEndLinklist.FindNodeHashTable; +import com.godepth.LoopLinkedList.DetectLoop; +import com.godepth.BFS.BFSTree; import com.godepth.doublelinkedlist.DoubleLinkedList; import com.godepth.island.Island; import com.godepth.pathfinder.Pathfinder; +import com.godepth.regex.Regex; +import com.godepth.regex.SimpleRegexEngine; +import com.godepth.regex.SimpleRegexEngineByStep; import com.godepth.singlelinkedlist.SingleLinkedList; import java.util.ArrayList; @@ -22,7 +31,12 @@ public static void main(String[] args) //runDoubleLinkedList(); //runFindNodeFromEndEfficiently(); //runFindNodeFromEndHashTable(); - runDetectLoopInLinkList(); + //runDetectLoopInLinkList(); + //runRecursiveBFS(); + //runQueueBFS(); + ////runQueueDFS(); + //runDFSInorder(); + runRegexOne(); } private static void runIslandCounter() @@ -121,4 +135,98 @@ private static void runDetectLoopInLinkList() detectLoop.find(); } + + private static void runRecursiveBFS() + { + BFSTree tree = new BFSTree<>(); + tree.root= new BFSNode<>(1); + tree.root.setLeft(new BFSNode<>(2)); + tree.root.setRight(new BFSNode<>(3)); + tree.root.getLeft().setLeft(new BFSNode<>(4)); + tree.root.getLeft().setRight(new BFSNode<>(5)); + tree.root.getRight().setLeft(new BFSNode<>(6)); + tree.root.getRight().setRight(new BFSNode<>(7)); + + System.out.println("Level order traversal of binary tree is "); + tree.printLevelOrder(); + } + + private static void runQueueBFS() + { + QueueBFSTree tree = new QueueBFSTree<>(); + tree.root = new BFSNode<>('A'); + tree.root.setLeft(new BFSNode<>('B')); + tree.root.setRight(new BFSNode<>('C')); + tree.root.getLeft().setLeft(new BFSNode<>('D')); + tree.root.getLeft().setRight(new BFSNode<>('E')); + tree.root.getLeft().getRight().setLeft(new BFSNode<>('F')); + + System.out.println("Level order traversal of binary tree is - "); + tree.printLevelOrder(); + } + + private static void runQueueDFS() + { + StackDSTTree tree = new StackDSTTree<>(); + tree.root = new DFSNode<>('A');; + tree.root.setLeft(new DFSNode<>('B')); + tree.root.setRight(new DFSNode<>('C')); + tree.root.getLeft().setLeft(new DFSNode<>('D')); + tree.root.getLeft().setRight(new DFSNode<>('E')); + tree.root.getLeft().getRight().setLeft(new DFSNode<>('F')); + + System.out.println("Depth order traversal of binary tree is - "); + tree.printDepthOrder(); + + } + + private static void runDFSInorder() + { + DFSTraversal tree = new DFSTraversal<>(); + tree.root = new DFSNode<>(25); + + tree.root.setLeft(new DFSNode<>(15)); + + tree.root.getLeft().setLeft(new DFSNode<>(10)); + tree.root.getLeft().setRight(new DFSNode<>(22)); + + tree.root.getLeft().getLeft().setLeft(new DFSNode<>(4)); + tree.root.getLeft().getLeft().setRight(new DFSNode<>(12)); + + tree.root.getLeft().getRight().setLeft(new DFSNode<>(18)); + tree.root.getLeft().getRight().setRight(new DFSNode<>(24)); + + tree.root.setRight(new DFSNode<>(50)); + + tree.root.getRight().setLeft(new DFSNode<>(35)); + tree.root.getRight().setRight(new DFSNode<>(70)); + + tree.root.getRight().getLeft().setLeft(new DFSNode<>(31)); + tree.root.getRight().getLeft().setRight(new DFSNode<>(44)); + + tree.root.getRight().getRight().setLeft(new DFSNode<>(66)); + tree.root.getRight().getRight().setRight(new DFSNode<>(90)); + + System.out.println("\nPreorder traversal of BST is "); + tree.printPreorder(tree.root); + + System.out.println("\nInorder traversal of BST is "); + tree.printInorder(tree.root); + + System.out.println("\nPostorder traversal of BST is "); + tree.printPostorder(tree.root); + } + + private static void runRegexOne() + { + String expression ="a*b*"; + String text = "aabbbb"; + + //SimpleRegexEngine regex = new SimpleRegexEngine(); + //Regex regex = new Regex(); + //System.out.println("Regex result is " + (regex.match(expression.toCharArray(), text.toCharArray()) ? "true" : "false")); + + SimpleRegexEngineByStep regex = new SimpleRegexEngineByStep(); + System.out.println("Regex result is " + (regex.match(expression, text) ? "true" : "false")); + } } diff --git a/Java/src/com/godepth/regex/Regex.java b/Java/src/com/godepth/regex/Regex.java new file mode 100644 index 0000000..2014aa6 --- /dev/null +++ b/Java/src/com/godepth/regex/Regex.java @@ -0,0 +1,55 @@ +package com.godepth.regex; + +public class Regex { + + public static boolean match(char[] pattern, char[] input) { + if (pattern.length == 0 || input.length == 0) + return false; + + if (pattern[0] == '*') { + return matchFromBack(pattern, input); + } else if (pattern[pattern.length - 1] == '*') { + return matchFromBeginning(pattern, input); + } else { + return (matchFromBeginning(pattern, input) && matchFromBack(pattern, input)); + } + } + + private static boolean matchFromBeginning(char[] pattern, char[] input) { + boolean result = true; + int i, j; + + for (i = 0, j = 0; i < input.length && j < pattern.length; i++, j++) { + if (pattern[j] == '*') { + return true; + } else if (pattern[j] != input[i]) { + return false; + } + } + + // input string did not finish + if (i != input.length) + result = false; + + return result; + } + + private static boolean matchFromBack(char[] pattern, char[] input) { + boolean result = true; + int i, j; + + for (i = input.length - 1, j = pattern.length - 1; i >= 0 && j >= 0; i--, j--) { + if (pattern[j] == '*') { + return true; + } else if (pattern[j] != input[i]) { + return false; + } + } + + // input string did not finish + if (i >= 0) + result = false; + + return result; + } +} diff --git a/Java/src/com/godepth/regex/SimpleRegexEngine.java b/Java/src/com/godepth/regex/SimpleRegexEngine.java new file mode 100644 index 0000000..c513976 --- /dev/null +++ b/Java/src/com/godepth/regex/SimpleRegexEngine.java @@ -0,0 +1,22 @@ +package com.godepth.regex; + +public class SimpleRegexEngine { + + public boolean match(char expression[], char str[], int i, int j) { + + if (i == expression.length && j == str.length) + return true; + + if ((i == expression.length && j != str.length) + || (i != expression.length && j == str.length)) + return false; + + if (expression[0] == '?' || expression[i] == str[j]) + return match(expression, str, i+1, j+1); + + if (expression[i] == '*') + return match(expression, str, i+1, j) || match(expression, str, i, j+1) || match(expression, str, i+1, j+1); + + return false; + } +} diff --git a/Java/src/com/godepth/regex/SimpleRegexEngineByStep.java b/Java/src/com/godepth/regex/SimpleRegexEngineByStep.java new file mode 100644 index 0000000..9da99d8 --- /dev/null +++ b/Java/src/com/godepth/regex/SimpleRegexEngineByStep.java @@ -0,0 +1,52 @@ +package com.godepth.regex; + +public class SimpleRegexEngineByStep { + + public boolean match(String expression, String str) { + if (expression.equals("")) + return true; + else if (expression.equals("$") && str.equals("")) + return true; + else if (expression.charAt(1) == '?') + return matchQuestionSign(expression, str); + else if (expression.charAt(1) == '*') { + return matchStarSign(expression, str); + } + else + return matchOne(expression.charAt(0), str.charAt(0)) && match(expression.substring(1), str.substring(1)); + } + + private boolean matchOne(Character expression, Character str){ + if (expression.toString().equals("")) + return true; + else if (str.toString().equals("")) + return false; + else + return expression.equals('.') || expression.equals(str); + } + + private boolean matchQuestionSign(String expression, String str) { + return (matchOne(expression.charAt(0), str.charAt(0)) && match(expression.substring(2), str.substring(1))) || match(expression.substring(2), str); + } + + private boolean matchStarSign(String expression, String str) { + return (matchOne(expression.charAt(0), str.charAt(0)) && match(expression, str.substring(1))) + || match(expression.substring(2), str); + + } + + private boolean matchSameLength(String expression, String str){ + return expression.equals("") + || (expression.charAt(0) == str.charAt(0) && match(expression.substring(1), str.substring(1))); + } + + private boolean matchDollarSign(String expression, String str) { + return expression.equals("") + || (expression.equals("$") && str.equals("")) + || (matchOne(expression.charAt(0), str.charAt(0)) && match(expression.substring(1), str.substring(1))); + } + + private boolean searchCaret(String expression, String str) { + return (expression.charAt(0) == '^') ? match(expression.substring(1), str) : match(".*" + expression, str); + } +} diff --git a/Java/src/src.iml b/Java/src/src.iml new file mode 100644 index 0000000..d1374d1 --- /dev/null +++ b/Java/src/src.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file