diff --git a/DecodeWays.java b/DecodeWays.java new file mode 100644 index 00000000..c8b04a1e --- /dev/null +++ b/DecodeWays.java @@ -0,0 +1,56 @@ +package main; + +//A message containing letters from A-Z is being encoded to numbers using the following mapping: + +//'A' -> 1 +//'B' -> 2 +//... +//'Z' -> 26 +//tự viết +//Example 1 +//Input: 34 +//Output: 1 +//Example 2 +//Input: 12 +//Output: 2 +//Example 3 +//Input: 16 +//Output: 2 +//Example 4 +//Input:299 +//Output: 1 +//Example 5 +//Input: 53 +//Output: 1 + +//Given an encoded message containing digits, determine the total number of ways to decode it. + +//For example, +//Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12). + +//The number of ways decoding "12" is 2. + +public class DecodeWays { + public static int numDecodings(String s) { + int n = s.length(); + + if(n == 0) { + return 0; + } + + int[] dp = new int[n + 1]; + dp[n] = 1; + dp[n - 1] = s.charAt(n - 1) != '0' ? 1 : 0; + + for(int i = n - 2; i >= 0; i--) { + if(s.charAt(i) == '0') { + continue; + } else { + dp[i] = (Integer.parseInt(s.substring(i, i + 2)) <= 26) ? dp[i + 1] + dp[i + 2] : dp[i + 1]; + } + } + + return dp[0]; + } +} + diff --git a/MainTest.java b/MainTest.java new file mode 100644 index 00000000..d1c7c4d1 --- /dev/null +++ b/MainTest.java @@ -0,0 +1,29 @@ +package test; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import main.Power; + +public class MainTest { + + @Test + void testExample1() { + + assertEquals(true, Power.isPowerOfTwo(1)); + + } +@Test + void testExample4() { + + assertEquals(true, Power.isPowerOfTwo(0)); + + } +@Test + void testExample5() { + + assertEquals(true, Power.isPowerOfTwo(100)); + + } + +} diff --git a/Power.java b/Power.java new file mode 100644 index 00000000..8b953177 --- /dev/null +++ b/Power.java @@ -0,0 +1,11 @@ +package main; +public class Power { + public static boolean isPowerOfTwo(int n) { + long i = 1; + while(i < n) { + i <<= 1; + } + + return i == n; + } +} diff --git a/ReverseWord.java b/ReverseWord.java new file mode 100644 index 00000000..ed4ef408 --- /dev/null +++ b/ReverseWord.java @@ -0,0 +1,31 @@ +package main; +//Given an input string, reverse the string word by word. +//For example, +//Given s = "the sky is blue", +//return "blue is sky the". +//tự viết +//Example 1 +//Input: String a = "Hello I am Nhi" +//Output: String a1 = "Nhi am I Hello" +//Example 2 +//Input: String b = "I Love You" +//Output: String b1 = "You Love I" +//Example 3 +//Input: String c = "Sorry" +//Output: String c1 = "Sorry" +//Example 4 +//Input: String d = "Thank You" +//Output: String d1 = "You Thank" + +public class ReverseWord { + public static String reverseWords(String s) { + String[] words = s.trim().split("\\s+"); + String result = ""; + for(int i = words.length - 1; i > 0; i--) { + result += words[i] + " "; + } + + return result + words[0]; + } +} + diff --git a/TestDecodeFacebook.java b/TestDecodeFacebook.java new file mode 100644 index 00000000..10126e70 --- /dev/null +++ b/TestDecodeFacebook.java @@ -0,0 +1,27 @@ +package test; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +import main.DecodeWays; + +public class TestDecodeFacebook { + @Test + public void testEx1() { + + assertEquals(1, DecodeWays.numDecodings("34")); + + } + @Test + public void testEx2() { + + assertEquals(2, DecodeWays.numDecodings("12")); + + } + @Test + public void testEx5() { + + assertEquals(1, DecodeWays.numDecodings("53")); + + } +} diff --git a/TestReverseWord.java b/TestReverseWord.java new file mode 100644 index 00000000..4bfb9d1e --- /dev/null +++ b/TestReverseWord.java @@ -0,0 +1,30 @@ +package test; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import main.ReverseWord; + + +public class TestReverseWord { + @Test + public void Example1TestReverse() { + + System.out.println(ReverseWord.reverseWords("Hello I am Nhi")); + assertEquals("Nhi am I Hello", ReverseWord.reverseWords("Hello I am Nhi")); + + + } +@Test + public void Example3TestReverse() { + System.out.println(ReverseWord.reverseWords("Sorry")); + assertEquals("Sorry", ReverseWord.reverseWords("Sorry")); + + } +@Test + public void Example4TestReverse() { + System.out.println(ReverseWord.reverseWords("Thank You")); + assertEquals("You Thank", ReverseWord.reverseWords("Thank You")); + + } +}