From 1aaed585a2b352e8b89f9e294da0d4451847b256 Mon Sep 17 00:00:00 2001 From: yashasvimisra2798 <54177363+yashasvimisra2798@users.noreply.github.com> Date: Tue, 22 Dec 2020 09:39:37 +0530 Subject: [PATCH 01/21] Create solution.cpp --- Trie/Contacts/solution.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Trie/Contacts/solution.cpp diff --git a/Trie/Contacts/solution.cpp b/Trie/Contacts/solution.cpp new file mode 100644 index 00000000..67c234b9 --- /dev/null +++ b/Trie/Contacts/solution.cpp @@ -0,0 +1,37 @@ +#include +#include + +using namespace std ; + +int main () +{ + // input the number of options to perform + int n ; + cin >> n ; + set < string > s ; + set < string > :: iterator it ; + while ( n -- ) + { + // input the operations + string a , b ; + cin >> a >> b ; + if ( a == "add" ) + { + s.insert ( b ) ; + } + else + { + it = s.lower_bound ( b ) ; + int counter = 0 ; + int len = b.length () ; + while ( it != s.end () && b == (*it).substr ( 0 , len ) ) + { + counter ++ ; + it ++ ; + } + // print the number of contact names starting with input string + cout << counter << endl ; + } + } + return 0 ; +} From bb74bcfd8793895c58c168c05595127c536406ef Mon Sep 17 00:00:00 2001 From: yashasvimisra2798 <54177363+yashasvimisra2798@users.noreply.github.com> Date: Tue, 22 Dec 2020 09:39:41 +0530 Subject: [PATCH 02/21] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ecc046f2..0f99ce87 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,8 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p ||[Tree: Square-Ten Tree](https://www.hackerrank.com/challenges/square-ten-tree/problem)|Hard|[View](Trees/Square-Ten%20Tree/solution.cpp)||[View](Trees/Square-Ten%20Tree/solution.py)| ||[Balanced Forest](https://www.hackerrank.com/challenges/balanced-forest/problem)|Hard|[View](Trees/Balanced%20Forest/solution.cpp)|[View](Trees/Balanced%20Forest/solution.java)|[View](Trees/Balanced%20Forest/solution.py)| ||[Kitty's Calculations on a Tree](https://www.hackerrank.com/challenges/kittys-calculations-on-a-tree/problem)|Advanced|[View](Trees/Kitty's%20Calculations%20on%20a%20Tree/solution.cpp)|[View](Trees/Kitty's%20Calculations%20on%20a%20Tree/solution.java)|[View](Trees/Kitty's%20Calculations%20on%20a%20Tree/solution.py)| +| Trie | +||[Contacts](https://www.hackerrank.com/challenges/contacts/problem)|Medium|[View](/Trie/Contacts/solution.cpp)||| | Algorithms | ||[Mark and Toys]( https://www.hackerrank.com/challenges/mark-and-toys/problem)|Easy|[View](/Algorithms/Greedy/Mark%20and%20Toys/solution.cpp)|[View](/Algorithms/Greedy/Mark%20and%20Toys/solution.java)|[View](/Algorithms/Greedy/Mark%20and%20Toys/solution.py)| ||[Grading Students](https://www.hackerrank.com/challenges/grading/problem)|Easy|[View](/Algorithms/Greedy/Grading%20Students/solution.cpp)|[View](/Algorithms/Grading%20Students/solution.java)|[View](/Algorithms/Grading%20Students/solution.py)| From e505f80d01ff4cc2b968fdbdb3b257edff055e6f Mon Sep 17 00:00:00 2001 From: yashasvimisra2798 <54177363+yashasvimisra2798@users.noreply.github.com> Date: Tue, 22 Dec 2020 09:54:43 +0530 Subject: [PATCH 03/21] Create solution.cpp --- Trie/No Prefix Set/solution.cpp | 73 +++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Trie/No Prefix Set/solution.cpp diff --git a/Trie/No Prefix Set/solution.cpp b/Trie/No Prefix Set/solution.cpp new file mode 100644 index 00000000..d2df99c0 --- /dev/null +++ b/Trie/No Prefix Set/solution.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +// Trie node +struct Node { + char data; + vector nexts = vector {}; + Node(char c){ + data = c; + } +}; + +// The trie class +class Trie { + public: + bool repeater = false; + Node* head = new Node('z'); + void insert(const string& s){ + bool working = false; + Node* tmp = head; + for(auto it=s.begin(); it < s.end(); ++it) { + vector::iterator vit; + for(vit = tmp->nexts.begin(); vit < tmp->nexts.end(); ++vit) { + if((*vit)->data == *it){ + tmp = *vit; + break; + } + + } + if(tmp->nexts.size() == 0 && tmp != head && !working) { + repeater = true; + return; + } + if(vit == tmp->nexts.end()){ + working = true; + auto vv = new Node(*it); + tmp->nexts.push_back(vv); + tmp = vv; + } + if(it==s.end()-1 && tmp->nexts.size() > 0){ + repeater = true; + return; + } + } + } +}; + +// driver function to test the above functions +int main() { + // input number of strings in the set + int x; + cin >> x; + + auto trie = Trie(); + string s; + //print GOOD SET if the set is valid else, print BAD SET followed by the first string for which the condition fails + for(int i = 0; i < x ; ++i) { + // input the string + cin >> s; + trie.insert(s); + if(trie.repeater){ + cout << "BAD SET" << "\n" << s << endl; + return 0; + } + } + cout << "GOOD SET" << endl; + return 0; +} \ No newline at end of file From 73dfc34fdff4eee7860f1504838ee16d832817f4 Mon Sep 17 00:00:00 2001 From: yashasvimisra2798 <54177363+yashasvimisra2798@users.noreply.github.com> Date: Tue, 22 Dec 2020 09:54:47 +0530 Subject: [PATCH 04/21] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ecc046f2..fbacbeb1 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,8 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p ||[Tree: Square-Ten Tree](https://www.hackerrank.com/challenges/square-ten-tree/problem)|Hard|[View](Trees/Square-Ten%20Tree/solution.cpp)||[View](Trees/Square-Ten%20Tree/solution.py)| ||[Balanced Forest](https://www.hackerrank.com/challenges/balanced-forest/problem)|Hard|[View](Trees/Balanced%20Forest/solution.cpp)|[View](Trees/Balanced%20Forest/solution.java)|[View](Trees/Balanced%20Forest/solution.py)| ||[Kitty's Calculations on a Tree](https://www.hackerrank.com/challenges/kittys-calculations-on-a-tree/problem)|Advanced|[View](Trees/Kitty's%20Calculations%20on%20a%20Tree/solution.cpp)|[View](Trees/Kitty's%20Calculations%20on%20a%20Tree/solution.java)|[View](Trees/Kitty's%20Calculations%20on%20a%20Tree/solution.py)| +| Trie | +||[No Prefix Set](https://www.hackerrank.com/challenges/no-prefix-set/problem)|Hard|[View](/Trie/No%20Prefix%20Set/solution.cpp)||| | Algorithms | ||[Mark and Toys]( https://www.hackerrank.com/challenges/mark-and-toys/problem)|Easy|[View](/Algorithms/Greedy/Mark%20and%20Toys/solution.cpp)|[View](/Algorithms/Greedy/Mark%20and%20Toys/solution.java)|[View](/Algorithms/Greedy/Mark%20and%20Toys/solution.py)| ||[Grading Students](https://www.hackerrank.com/challenges/grading/problem)|Easy|[View](/Algorithms/Greedy/Grading%20Students/solution.cpp)|[View](/Algorithms/Grading%20Students/solution.java)|[View](/Algorithms/Grading%20Students/solution.py)| From e3b232c53f548a9ab2c70d70acb989ca139bb7bd Mon Sep 17 00:00:00 2001 From: yashasvimisra2798 <54177363+yashasvimisra2798@users.noreply.github.com> Date: Tue, 22 Dec 2020 10:24:29 +0530 Subject: [PATCH 05/21] Create solution.py --- Algorithms/Strings/String Construction/solution.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Algorithms/Strings/String Construction/solution.py diff --git a/Algorithms/Strings/String Construction/solution.py b/Algorithms/Strings/String Construction/solution.py new file mode 100644 index 00000000..d838d9bc --- /dev/null +++ b/Algorithms/Strings/String Construction/solution.py @@ -0,0 +1,11 @@ +#!/bin/python3 + +import sys + +# input number of strings +n = int(input().strip()) +for a0 in range(n): + # input the strings + s = input().strip() + # print minimum cost of constructing new string + print(len(set(s))) \ No newline at end of file From c3777ceb272538c34521eeb278ba6e2500d71437 Mon Sep 17 00:00:00 2001 From: yashasvimisra2798 <54177363+yashasvimisra2798@users.noreply.github.com> Date: Tue, 22 Dec 2020 10:24:34 +0530 Subject: [PATCH 06/21] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ecc046f2..cf819ca0 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,7 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p ||[Knapsack](https://www.hackerrank.com/challenges/unbounded-knapsack/problem)|Medium|[View](/Algorithms/Dynamic%20Programming/Knapsack/solution.cpp)|[View](/Algorithms/Dynamic%20Programming/Knapsack/solution.java)|[View](/Algorithms/Dynamic%20Programming/Knapsack/solution.py)| ||[The Bomberman Game](https://www.hackerrank.com/challenges/bomber-man/problem)|Medium|[View](/Algorithms/Implementation/The%20Bomberman%20Game/solution.cpp)|[View](/Algorithms/Implementation/The%20Bomberman%20Game/solution.java)|[View](/Algorithms/Implementation/The%20Bomberman%20Game/solution.py)| ||[Organizing Containers of Balls](https://www.hackerrank.com/challenges/organizing-containers-of-balls/problem)|Easy|[View](Algorithms/Organizing%20Containers%20of%20Balls/solution.cpp)|[View](Algorithms/Organizing%20Containers%20of%20Balls/solution.java)|[View](/Algorithms/Organizing%20Containers%20of%20Balls/solution.py)| +||[String Construction](https://www.hackerrank.com/challenges/string-construction/problem)|Easy|||[View](/Algorithms/Strings/String%20Construction/solution.py)| | Contest Problems | ||[Squares of a Sorted Array](https://www.hackerrank.com/contests/coding-gym-mo0419/challenges/squares-of-a-sorted-array)|Medium|[View](/Contest%20Problems/Squares%20of%20a%20Sorted%20Array/solution.cpp)|[View](/Contest%20Problems/Squares%20of%20a%20Sorted%20Array/solution.java)|[View](/Contest%20Problems/Squares%20of%20a%20Sorted%20Array/solution.py)| | Disjoint Sets | From 2edfc737967a9b0dfcfb7f669a5c0d669121f2ff Mon Sep 17 00:00:00 2001 From: yashasvimisra2798 <54177363+yashasvimisra2798@users.noreply.github.com> Date: Tue, 22 Dec 2020 10:31:18 +0530 Subject: [PATCH 07/21] Create solution.py --- .../Highest Value Palindrome/solution.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Algorithms/Strings/Highest Value Palindrome/solution.py diff --git a/Algorithms/Strings/Highest Value Palindrome/solution.py b/Algorithms/Strings/Highest Value Palindrome/solution.py new file mode 100644 index 00000000..d6eb7a05 --- /dev/null +++ b/Algorithms/Strings/Highest Value Palindrome/solution.py @@ -0,0 +1,35 @@ +# input the number of digits and the maximum number of changes allowed +n,k=map(int,input().split(' ')) +# input the n-digit string of numbers +num=input() +ss=list(num) +ch=[False for i in range(n)] +offset=0 +cc=0 +# print a string representation of the highest value achievable or -1 +for i in range(n>>1): + if ord(ss[i])>ord(ss[n-1-i]): + ss[n-1-i]=ss[i] + ch[n-1-i]=True + cc+=1 + elif ord(ss[i])k: + print(-1) +else: + k-=cc + i=0 + while k and i<(n>>1): + if ss[i]!='9': + if ch[i] or ch[n-i-1]: + ss[i]=ss[n-1-i]='9' + k-=1 + elif k>=2: + ss[i] = ss[n - 1 - i] = '9' + k -= 2 + i+=1 + if n&1 and k: + ss[(n>>1)]='9' + print(''.join(ss)) From 1f3d1c9366c8622cd4dcc5f979bb15ef625ceda2 Mon Sep 17 00:00:00 2001 From: yashasvimisra2798 <54177363+yashasvimisra2798@users.noreply.github.com> Date: Tue, 22 Dec 2020 10:31:22 +0530 Subject: [PATCH 08/21] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index cf819ca0..82d940ed 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,7 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p ||[The Bomberman Game](https://www.hackerrank.com/challenges/bomber-man/problem)|Medium|[View](/Algorithms/Implementation/The%20Bomberman%20Game/solution.cpp)|[View](/Algorithms/Implementation/The%20Bomberman%20Game/solution.java)|[View](/Algorithms/Implementation/The%20Bomberman%20Game/solution.py)| ||[Organizing Containers of Balls](https://www.hackerrank.com/challenges/organizing-containers-of-balls/problem)|Easy|[View](Algorithms/Organizing%20Containers%20of%20Balls/solution.cpp)|[View](Algorithms/Organizing%20Containers%20of%20Balls/solution.java)|[View](/Algorithms/Organizing%20Containers%20of%20Balls/solution.py)| ||[String Construction](https://www.hackerrank.com/challenges/string-construction/problem)|Easy|||[View](/Algorithms/Strings/String%20Construction/solution.py)| +||[Highest Value Palindrome](https://www.hackerrank.com/challenges/richie-rich/problem)|Medium|||[View](/Algorithms/Strings/Highest%20Value%20Palindrome/solution.py)| | Contest Problems | ||[Squares of a Sorted Array](https://www.hackerrank.com/contests/coding-gym-mo0419/challenges/squares-of-a-sorted-array)|Medium|[View](/Contest%20Problems/Squares%20of%20a%20Sorted%20Array/solution.cpp)|[View](/Contest%20Problems/Squares%20of%20a%20Sorted%20Array/solution.java)|[View](/Contest%20Problems/Squares%20of%20a%20Sorted%20Array/solution.py)| | Disjoint Sets | From 3b522967f47be284ea4652cb36531aa7a3bfd0f3 Mon Sep 17 00:00:00 2001 From: yashasvimisra2798 <54177363+yashasvimisra2798@users.noreply.github.com> Date: Tue, 22 Dec 2020 10:43:02 +0530 Subject: [PATCH 09/21] Create solution.py --- .../Sorting/Lily's Homework/solution.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Algorithms/Sorting/Lily's Homework/solution.py diff --git a/Algorithms/Sorting/Lily's Homework/solution.py b/Algorithms/Sorting/Lily's Homework/solution.py new file mode 100644 index 00000000..502f4cf4 --- /dev/null +++ b/Algorithms/Sorting/Lily's Homework/solution.py @@ -0,0 +1,29 @@ + +def homework(n, arr): + + pos = dict() + for i, num in enumerate(arr): + pos[num] = i + + cnt = 0 + sorted_arr = sorted(arr) + + for i in range(len(arr)): + if arr[i] != sorted_arr[i]: + cnt += 1 + + new_idx = pos[sorted_arr[i]] + pos[arr[i]] = new_idx + arr[i], arr[new_idx] = arr[new_idx], arr[i] + + return cnt + +# input the number of elements of the array +n = int(input().strip()) +# input elements of array +arr = list(map(int, input().strip().split())) + +asc = homework(n, arr) +desc = homework(n, list(reversed(arr))) +# print the minimum number of swaps needed to make the array beautiful +print(min(asc, desc)) \ No newline at end of file From 6cb1544eea8e628bf1070ae11a391ba945306b2a Mon Sep 17 00:00:00 2001 From: yashasvimisra2798 <54177363+yashasvimisra2798@users.noreply.github.com> Date: Tue, 22 Dec 2020 10:43:05 +0530 Subject: [PATCH 10/21] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 82d940ed..761630d1 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,7 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p ||[Organizing Containers of Balls](https://www.hackerrank.com/challenges/organizing-containers-of-balls/problem)|Easy|[View](Algorithms/Organizing%20Containers%20of%20Balls/solution.cpp)|[View](Algorithms/Organizing%20Containers%20of%20Balls/solution.java)|[View](/Algorithms/Organizing%20Containers%20of%20Balls/solution.py)| ||[String Construction](https://www.hackerrank.com/challenges/string-construction/problem)|Easy|||[View](/Algorithms/Strings/String%20Construction/solution.py)| ||[Highest Value Palindrome](https://www.hackerrank.com/challenges/richie-rich/problem)|Medium|||[View](/Algorithms/Strings/Highest%20Value%20Palindrome/solution.py)| +||[Lily's Homework](https://www.hackerrank.com/challenges/lilys-homework/problem)|Medium|||[View](/Algorithms/Sorting/Lily's%20Homework/solution.py)| | Contest Problems | ||[Squares of a Sorted Array](https://www.hackerrank.com/contests/coding-gym-mo0419/challenges/squares-of-a-sorted-array)|Medium|[View](/Contest%20Problems/Squares%20of%20a%20Sorted%20Array/solution.cpp)|[View](/Contest%20Problems/Squares%20of%20a%20Sorted%20Array/solution.java)|[View](/Contest%20Problems/Squares%20of%20a%20Sorted%20Array/solution.py)| | Disjoint Sets | From 2e4105f7bdf3c51316004c1039f118255a4d6193 Mon Sep 17 00:00:00 2001 From: Nivedita967 <66518355+Nivedita967@users.noreply.github.com> Date: Tue, 22 Dec 2020 11:34:32 +0530 Subject: [PATCH 11/21] code added --- .../Beautiful Binary String/solution.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Algorithms/Strings/Beautiful Binary String/solution.java diff --git a/Algorithms/Strings/Beautiful Binary String/solution.java b/Algorithms/Strings/Beautiful Binary String/solution.java new file mode 100644 index 00000000..e3a33bc7 --- /dev/null +++ b/Algorithms/Strings/Beautiful Binary String/solution.java @@ -0,0 +1,25 @@ +import java.io.*; +import java.util.*; + +public class Solution { + + public static void main(String[] args) { + // Read input from STDIN and Print output to STDOUT + Scanner input = new Scanner(System.in); + int n = input.nextInt(); // length of binary string + input.nextLine(); + String s = input.nextLine(); // single binary string of length n + int switches = 0; + + for(int i = 0; i < s.length()-2; i++) // left to right character of binary string + { + if(s.charAt(i) == '0' && s.charAt(i+1) == '1' && s.charAt(i+2) == '0') + { + switches++; + i += 2; + } + } + // minimum number of steps needed to make the string beautiful + System.out.println(switches); + } +} \ No newline at end of file From f0718a4c534458241183ba41292c335cfc55b70b Mon Sep 17 00:00:00 2001 From: Nivedita967 <66518355+Nivedita967@users.noreply.github.com> Date: Tue, 22 Dec 2020 12:18:26 +0530 Subject: [PATCH 12/21] comment --- Algorithms/Strings/Beautiful Binary String/solution.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Algorithms/Strings/Beautiful Binary String/solution.java b/Algorithms/Strings/Beautiful Binary String/solution.java index e3a33bc7..08e8dfa7 100644 --- a/Algorithms/Strings/Beautiful Binary String/solution.java +++ b/Algorithms/Strings/Beautiful Binary String/solution.java @@ -1,3 +1,9 @@ +/* +We could use a greedy approach and starting from the +left everytime we see a 010 replace the last 0 with a 1 +and continue +*/ + import java.io.*; import java.util.*; From 77190e04b76f17d84015626d0bbc3213a14a336c Mon Sep 17 00:00:00 2001 From: Nivedita967 <66518355+Nivedita967@users.noreply.github.com> Date: Tue, 22 Dec 2020 12:20:57 +0530 Subject: [PATCH 13/21] readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ecc046f2..2735ffb1 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p ||[Missing Numbers](https://www.hackerrank.com/challenges/missing-numbers/problem)|Easy|[View](Algorithms/Search/Missing%20Numbers/solution.cpp)|[View](Algorithms/Search/Missing%20Numbers/solution.java)|[View](/Algorithms/Search/Missing%20Numbers/solution.py)| ||[Making Anagrams](https://www.hackerrank.com/challenges/making-anagrams/problem)|Easy|[View](Algorithms/Strings/Making%20Anagrams/solution.cpp)||| ||[The Love-Letter Mystery](https://www.hackerrank.com/challenges/the-love-letter-mystery/problem)|Easy|[View](Algorithms/Strings/The%20Love-Letter%20Mystery/solution.cpp)||| -||[Beautiful Binary String](https://www.hackerrank.com/challenges/beautiful-binary-string/problem)|Easy|[View](Algorithms/Strings/Beautiful%20Binary%20String/solution.cpp)||| +||[Beautiful Binary String](https://www.hackerrank.com/challenges/beautiful-binary-string/problem)|Easy|[View](Algorithms/Strings/Beautiful%20Binary%20String/solution.cpp)|[View](Algorithms/Strings/Beautiful%20Binary%20String/solution.java)|| ||[HackerRank in a String!](https://www.hackerrank.com/challenges/hackerrank-in-a-string/problem)|Easy|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.cpp)|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.java)|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.py)| ||[Funny String](https://www.hackerrank.com/challenges/funny-string/problem)|Easy|[View](Algorithms/Strings/Funny%20String/solution.cpp)|[View](Algorithms/Strings/Funny%20String/solution.java)|[View](Algorithms/Strings/Funny%20String/solution.py)| ||[Gemstones](https://www.hackerrank.com/challenges/gem-stones/problem)|Easy|[View](Algorithms/Strings/Gemstones/solution.cpp)|[View](Algorithms/Strings/Gemstones/solution.java)|[View](Algorithms/Strings/Gemstones/solution.py)| From 3c910a21b86dc52143f1bed50e7151729d4f408c Mon Sep 17 00:00:00 2001 From: Nivedita967 <66518355+Nivedita967@users.noreply.github.com> Date: Tue, 22 Dec 2020 12:30:56 +0530 Subject: [PATCH 14/21] code --- .../The Love-Letter Mystery/solution.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Algorithms/Strings/The Love-Letter Mystery/solution.java diff --git a/Algorithms/Strings/The Love-Letter Mystery/solution.java b/Algorithms/Strings/The Love-Letter Mystery/solution.java new file mode 100644 index 00000000..de310df3 --- /dev/null +++ b/Algorithms/Strings/The Love-Letter Mystery/solution.java @@ -0,0 +1,48 @@ +import java.io.*; +import java.math.*; +import java.security.*; +import java.text.*; +import java.util.*; +import java.util.concurrent.*; +import java.util.regex.*; + +public class Solution { + + static int theLoveLetterMystery(String s) { + int count = 0; + int i = 0; + int j = s.length() - 1; + + while (i <= j) { + if (s.charAt(i) != s.charAt(j)) { + count += Math.abs(((int) s.charAt(i)) - ((int) s.charAt(j))); + } + i++; j--; + } + + return count; + + } + + private static final Scanner scanner = new Scanner(System.in); + + public static void main(String[] args) throws IOException { + BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); + + int q = scanner.nextInt(); + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); + + for (int qItr = 0; qItr < q; qItr++) { + String s = scanner.nextLine(); + + int result = theLoveLetterMystery(s); + + bufferedWriter.write(String.valueOf(result)); + bufferedWriter.newLine(); + } + + bufferedWriter.close(); + + scanner.close(); + } +} From e8e7ddb74a1c9f4aebcd25cfea752f84d8ea78cc Mon Sep 17 00:00:00 2001 From: Nivedita967 <66518355+Nivedita967@users.noreply.github.com> Date: Tue, 22 Dec 2020 12:31:29 +0530 Subject: [PATCH 15/21] comment --- .../Strings/The Love-Letter Mystery/solution.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Algorithms/Strings/The Love-Letter Mystery/solution.java b/Algorithms/Strings/The Love-Letter Mystery/solution.java index de310df3..76971415 100644 --- a/Algorithms/Strings/The Love-Letter Mystery/solution.java +++ b/Algorithms/Strings/The Love-Letter Mystery/solution.java @@ -8,11 +8,13 @@ public class Solution { + // theLoveLetterMystery function static int theLoveLetterMystery(String s) { int count = 0; - int i = 0; - int j = s.length() - 1; + int i = 0; // pointer at starting of string s + int j = s.length() - 1; // pointer at end of string s + // check for palindromic string while (i <= j) { if (s.charAt(i) != s.charAt(j)) { count += Math.abs(((int) s.charAt(i)) - ((int) s.charAt(j))); @@ -20,20 +22,22 @@ static int theLoveLetterMystery(String s) { i++; j--; } + // minimum number of operations corresponding to each test case return count; } private static final Scanner scanner = new Scanner(System.in); + // main function public static void main(String[] args) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); - int q = scanner.nextInt(); + int q = scanner.nextInt(); // number of queries scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); for (int qItr = 0; qItr < q; qItr++) { - String s = scanner.nextLine(); + String s = scanner.nextLine(); // string int result = theLoveLetterMystery(s); From 144b842ee5d19ce820554490e63824181a43fed9 Mon Sep 17 00:00:00 2001 From: Nivedita967 <66518355+Nivedita967@users.noreply.github.com> Date: Tue, 22 Dec 2020 12:33:33 +0530 Subject: [PATCH 16/21] readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2735ffb1..1682c7bf 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p ||[Ice Cream Parlor](https://www.hackerrank.com/challenges/icecream-parlor/problem)|Easy|[View](Algorithms/Search/Ice%20Cream%20Parlor/solution.cpp)|[View](Algorithms/Search/Ice%20Cream%20Parlor/solution.java)|[View](/Algorithms/Search/Ice%20Cream%20Parlor/solution.py)| ||[Missing Numbers](https://www.hackerrank.com/challenges/missing-numbers/problem)|Easy|[View](Algorithms/Search/Missing%20Numbers/solution.cpp)|[View](Algorithms/Search/Missing%20Numbers/solution.java)|[View](/Algorithms/Search/Missing%20Numbers/solution.py)| ||[Making Anagrams](https://www.hackerrank.com/challenges/making-anagrams/problem)|Easy|[View](Algorithms/Strings/Making%20Anagrams/solution.cpp)||| -||[The Love-Letter Mystery](https://www.hackerrank.com/challenges/the-love-letter-mystery/problem)|Easy|[View](Algorithms/Strings/The%20Love-Letter%20Mystery/solution.cpp)||| +||[The Love-Letter Mystery](https://www.hackerrank.com/challenges/the-love-letter-mystery/problem)|Easy|[View](Algorithms/Strings/The%20Love-Letter%20Mystery/solution.cpp)||[View](Algorithms/Strings/The%20Love-Letter%20Mystery/solution.java)| ||[Beautiful Binary String](https://www.hackerrank.com/challenges/beautiful-binary-string/problem)|Easy|[View](Algorithms/Strings/Beautiful%20Binary%20String/solution.cpp)|[View](Algorithms/Strings/Beautiful%20Binary%20String/solution.java)|| ||[HackerRank in a String!](https://www.hackerrank.com/challenges/hackerrank-in-a-string/problem)|Easy|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.cpp)|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.java)|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.py)| ||[Funny String](https://www.hackerrank.com/challenges/funny-string/problem)|Easy|[View](Algorithms/Strings/Funny%20String/solution.cpp)|[View](Algorithms/Strings/Funny%20String/solution.java)|[View](Algorithms/Strings/Funny%20String/solution.py)| From 3733e75e8adebbf70ffb5f1dc216f7b92fc8189c Mon Sep 17 00:00:00 2001 From: Nivedita967 <66518355+Nivedita967@users.noreply.github.com> Date: Tue, 22 Dec 2020 12:34:43 +0530 Subject: [PATCH 17/21] code --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1682c7bf..679116b5 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p ||[Ice Cream Parlor](https://www.hackerrank.com/challenges/icecream-parlor/problem)|Easy|[View](Algorithms/Search/Ice%20Cream%20Parlor/solution.cpp)|[View](Algorithms/Search/Ice%20Cream%20Parlor/solution.java)|[View](/Algorithms/Search/Ice%20Cream%20Parlor/solution.py)| ||[Missing Numbers](https://www.hackerrank.com/challenges/missing-numbers/problem)|Easy|[View](Algorithms/Search/Missing%20Numbers/solution.cpp)|[View](Algorithms/Search/Missing%20Numbers/solution.java)|[View](/Algorithms/Search/Missing%20Numbers/solution.py)| ||[Making Anagrams](https://www.hackerrank.com/challenges/making-anagrams/problem)|Easy|[View](Algorithms/Strings/Making%20Anagrams/solution.cpp)||| -||[The Love-Letter Mystery](https://www.hackerrank.com/challenges/the-love-letter-mystery/problem)|Easy|[View](Algorithms/Strings/The%20Love-Letter%20Mystery/solution.cpp)||[View](Algorithms/Strings/The%20Love-Letter%20Mystery/solution.java)| +||[The Love-Letter Mystery](https://www.hackerrank.com/challenges/the-love-letter-mystery/problem)|Easy|[View](Algorithms/Strings/The%20Love-Letter%20Mystery/solution.cpp)|[View](Algorithms/Strings/The%20Love-Letter%20Mystery/solution.java)|| ||[Beautiful Binary String](https://www.hackerrank.com/challenges/beautiful-binary-string/problem)|Easy|[View](Algorithms/Strings/Beautiful%20Binary%20String/solution.cpp)|[View](Algorithms/Strings/Beautiful%20Binary%20String/solution.java)|| ||[HackerRank in a String!](https://www.hackerrank.com/challenges/hackerrank-in-a-string/problem)|Easy|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.cpp)|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.java)|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.py)| ||[Funny String](https://www.hackerrank.com/challenges/funny-string/problem)|Easy|[View](Algorithms/Strings/Funny%20String/solution.cpp)|[View](Algorithms/Strings/Funny%20String/solution.java)|[View](Algorithms/Strings/Funny%20String/solution.py)| From 8466e9fdab7e2c179198686883399804870f4e93 Mon Sep 17 00:00:00 2001 From: Nivedita967 <66518355+Nivedita967@users.noreply.github.com> Date: Tue, 22 Dec 2020 12:48:58 +0530 Subject: [PATCH 18/21] code added --- .../Strings/Making Anagrams/solution.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Algorithms/Strings/Making Anagrams/solution.java diff --git a/Algorithms/Strings/Making Anagrams/solution.java b/Algorithms/Strings/Making Anagrams/solution.java new file mode 100644 index 00000000..cec52544 --- /dev/null +++ b/Algorithms/Strings/Making Anagrams/solution.java @@ -0,0 +1,46 @@ +import java.io.*; +import java.math.*; +import java.security.*; +import java.text.*; +import java.util.*; +import java.util.concurrent.*; +import java.util.regex.*; + +public class Solution { + + static int makingAnagrams(String s1, String s2) { + int[] freqs1 = new int[26]; + int[] freqs2 = new int[26]; + int deletion = 0; + + for(int i = 0; i < s1.length(); i++) + freqs1[s1.charAt(i)-'a'] = freqs1[s1.charAt(i)-'a'] + 1; + for(int i = 0; i < s2.length(); i++) + freqs2[s2.charAt(i)-'a'] = freqs2[s2.charAt(i)-'a'] + 1; + + for(int i = 0; i < 26; i++) + deletion += Math.abs(freqs1[i] - freqs2[i]); + + return deletion; + + } + + private static final Scanner scanner = new Scanner(System.in); + + public static void main(String[] args) throws IOException { + BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); + + String s1 = scanner.nextLine(); + + String s2 = scanner.nextLine(); + + int result = makingAnagrams(s1, s2); + + bufferedWriter.write(String.valueOf(result)); + bufferedWriter.newLine(); + + bufferedWriter.close(); + + scanner.close(); + } +} From 7814a96a14b0612e08940dffe7b8ffb0e6eabed6 Mon Sep 17 00:00:00 2001 From: Nivedita967 <66518355+Nivedita967@users.noreply.github.com> Date: Tue, 22 Dec 2020 12:49:41 +0530 Subject: [PATCH 19/21] comments --- Algorithms/Strings/Making Anagrams/solution.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Algorithms/Strings/Making Anagrams/solution.java b/Algorithms/Strings/Making Anagrams/solution.java index cec52544..6dd3a246 100644 --- a/Algorithms/Strings/Making Anagrams/solution.java +++ b/Algorithms/Strings/Making Anagrams/solution.java @@ -8,33 +8,36 @@ public class Solution { + // makingAnagrams function static int makingAnagrams(String s1, String s2) { int[] freqs1 = new int[26]; int[] freqs2 = new int[26]; int deletion = 0; for(int i = 0; i < s1.length(); i++) - freqs1[s1.charAt(i)-'a'] = freqs1[s1.charAt(i)-'a'] + 1; + freqs1[s1.charAt(i)-'a'] = freqs1[s1.charAt(i)-'a'] + 1; //Increment frequency of char at i for(int i = 0; i < s2.length(); i++) - freqs2[s2.charAt(i)-'a'] = freqs2[s2.charAt(i)-'a'] + 1; + freqs2[s2.charAt(i)-'a'] = freqs2[s2.charAt(i)-'a'] + 1; //Increment frequency of char at i for(int i = 0; i < 26; i++) - deletion += Math.abs(freqs1[i] - freqs2[i]); + deletion += Math.abs(freqs1[i] - freqs2[i]); //Track the total deletions needed + // minimum number of characters which must be deleted to make the two strings anagrams of each other return deletion; } private static final Scanner scanner = new Scanner(System.in); + // main function public static void main(String[] args) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); - String s1 = scanner.nextLine(); + String s1 = scanner.nextLine(); // first string - String s2 = scanner.nextLine(); + String s2 = scanner.nextLine(); // second string - int result = makingAnagrams(s1, s2); + int result = makingAnagrams(s1, s2); // function call bufferedWriter.write(String.valueOf(result)); bufferedWriter.newLine(); From ef2f405d00e7e6c3847f3934b4ecbe5adacec83c Mon Sep 17 00:00:00 2001 From: Nivedita967 <66518355+Nivedita967@users.noreply.github.com> Date: Tue, 22 Dec 2020 12:51:33 +0530 Subject: [PATCH 20/21] readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 679116b5..1fe7663b 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p ||[Sherlock and Array](https://www.hackerrank.com/challenges/sherlock-and-array/problem)|Easy|[View](Algorithms/Search/Sherlock%20and%20Array/solution.cpp)|[View](Algorithms/Search/Sherlock%20and%20Array/solution.java)|[View](/Algorithms/Search/Sherlock%20and%20Array/solution.py)| ||[Ice Cream Parlor](https://www.hackerrank.com/challenges/icecream-parlor/problem)|Easy|[View](Algorithms/Search/Ice%20Cream%20Parlor/solution.cpp)|[View](Algorithms/Search/Ice%20Cream%20Parlor/solution.java)|[View](/Algorithms/Search/Ice%20Cream%20Parlor/solution.py)| ||[Missing Numbers](https://www.hackerrank.com/challenges/missing-numbers/problem)|Easy|[View](Algorithms/Search/Missing%20Numbers/solution.cpp)|[View](Algorithms/Search/Missing%20Numbers/solution.java)|[View](/Algorithms/Search/Missing%20Numbers/solution.py)| -||[Making Anagrams](https://www.hackerrank.com/challenges/making-anagrams/problem)|Easy|[View](Algorithms/Strings/Making%20Anagrams/solution.cpp)||| +||[Making Anagrams](https://www.hackerrank.com/challenges/making-anagrams/problem)|Easy|[View](Algorithms/Strings/Making%20Anagrams/solution.cpp)|[View](Algorithms/Strings/Making%20Anagram/solution.java )|| ||[The Love-Letter Mystery](https://www.hackerrank.com/challenges/the-love-letter-mystery/problem)|Easy|[View](Algorithms/Strings/The%20Love-Letter%20Mystery/solution.cpp)|[View](Algorithms/Strings/The%20Love-Letter%20Mystery/solution.java)|| ||[Beautiful Binary String](https://www.hackerrank.com/challenges/beautiful-binary-string/problem)|Easy|[View](Algorithms/Strings/Beautiful%20Binary%20String/solution.cpp)|[View](Algorithms/Strings/Beautiful%20Binary%20String/solution.java)|| ||[HackerRank in a String!](https://www.hackerrank.com/challenges/hackerrank-in-a-string/problem)|Easy|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.cpp)|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.java)|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.py)| From 75ae194e843c510102b98815685765af71b2f45e Mon Sep 17 00:00:00 2001 From: Nivedita967 <66518355+Nivedita967@users.noreply.github.com> Date: Tue, 22 Dec 2020 12:51:53 +0530 Subject: [PATCH 21/21] typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1fe7663b..2ccfe307 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ Please read [contribution guidelines](CONTRIBUTING.md) for contributing to the p ||[Sherlock and Array](https://www.hackerrank.com/challenges/sherlock-and-array/problem)|Easy|[View](Algorithms/Search/Sherlock%20and%20Array/solution.cpp)|[View](Algorithms/Search/Sherlock%20and%20Array/solution.java)|[View](/Algorithms/Search/Sherlock%20and%20Array/solution.py)| ||[Ice Cream Parlor](https://www.hackerrank.com/challenges/icecream-parlor/problem)|Easy|[View](Algorithms/Search/Ice%20Cream%20Parlor/solution.cpp)|[View](Algorithms/Search/Ice%20Cream%20Parlor/solution.java)|[View](/Algorithms/Search/Ice%20Cream%20Parlor/solution.py)| ||[Missing Numbers](https://www.hackerrank.com/challenges/missing-numbers/problem)|Easy|[View](Algorithms/Search/Missing%20Numbers/solution.cpp)|[View](Algorithms/Search/Missing%20Numbers/solution.java)|[View](/Algorithms/Search/Missing%20Numbers/solution.py)| -||[Making Anagrams](https://www.hackerrank.com/challenges/making-anagrams/problem)|Easy|[View](Algorithms/Strings/Making%20Anagrams/solution.cpp)|[View](Algorithms/Strings/Making%20Anagram/solution.java )|| +||[Making Anagrams](https://www.hackerrank.com/challenges/making-anagrams/problem)|Easy|[View](Algorithms/Strings/Making%20Anagrams/solution.cpp)|[View](Algorithms/Strings/Making%20Anagrams/solution.java)|| ||[The Love-Letter Mystery](https://www.hackerrank.com/challenges/the-love-letter-mystery/problem)|Easy|[View](Algorithms/Strings/The%20Love-Letter%20Mystery/solution.cpp)|[View](Algorithms/Strings/The%20Love-Letter%20Mystery/solution.java)|| ||[Beautiful Binary String](https://www.hackerrank.com/challenges/beautiful-binary-string/problem)|Easy|[View](Algorithms/Strings/Beautiful%20Binary%20String/solution.cpp)|[View](Algorithms/Strings/Beautiful%20Binary%20String/solution.java)|| ||[HackerRank in a String!](https://www.hackerrank.com/challenges/hackerrank-in-a-string/problem)|Easy|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.cpp)|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.java)|[View](Algorithms/Strings/HackerRank%20in%20a%20String!/solution.py)|