megido
BAN USER
Easy to read, simple and effective Java solution using Set:
public List<Integer> intersectArrays(int[] a, int[] b) {
Map<Integer, Integer> map = new HashMap<>(); // number of appearances
List<Integer> result = new ArrayList<>();
for (int i = 0; i < a.length; i++) {
int curNum = 1;
if (map.containsKey(a[i]))
curNum = map.get(a[i]) + 1;
map.put(a[i], curNum);
}
for (int i = 0; i < b.length; i++) {
if (!map.containsKey(b[i]))
continue;
int curNum = map.get(b[i]);
if (curNum > 0) {
curNum--;
result.add(b[i]);
}
map.put(b[i], curNum); // update decreased counter
}
return result;
}
Simple and effective Java solution using while loop:
public String charFrequency(String s) {
String res = "";
int ind = 0;
while (ind < s.length()) {
char cur = s.charAt(ind);
int num = 1;
while (ind < s.length() - 1 && cur == s.charAt(ind + 1)) {
ind++;
num++;
}
ind++;
res += String.format("%s%s", cur, num);
}
return res;
}
Effective Java solution using ArrayDeque
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) {
new Solution().solve();
}
public boolean isCorrectBracketExpression(String s) {
ArrayDeque<Character> q = new ArrayDeque<>();
String brackets = "({<[)}>]";
for (Character cur : s.toCharArray()) {
if (brackets.indexOf(cur) < 4)
q.addFirst(cur);
else {
if (q.size() > 0) {
if (brackets.charAt(brackets.indexOf(q.peek()) + 4) == cur)
q.poll();
else
return false;
} else // nothing in the queue, but we want to add something, incorrect expression
return false;
}
}
return q.size() == 0;
}
public void solve() {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
String s = in.nextLine();
out.print(isCorrectBracketExpression(s) ? "Correct" : "Incorrect");
out.close();
}
}
Effective Java solution using HashSet
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) {
new Solution().solve();
}
public void solve() {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int[] a = {3, 1, 4, 5, 19, 6};
int[] b = {14, 9, 22, 36, 8, 0, 64, 25};
List<Integer> result = findSquares(a, b);
for (int cur : result)
out.println(cur);
out.close();
}
// search a[i]^2 in b
public List<Integer> findSquares(int[] a, int[] b) {
List<Integer> result = new ArrayList<>();
Set<Integer> squares = new HashSet<>();
for (int i = 0; i < a.length; i++)
squares.add(a[i] * a[i]);
for (int i = 0; i < b.length; i++)
if (squares.contains(b[i]))
result.add(b[i]);
return result;
}
}
Complete Java solution with considering different edge cases
import java.util.*;
import java.io.*;
public class Solution {
/*
Smart string is a string no longer than 30 character with cut no words
*/
public static void main(String[] args) {
new Solution().solve();
}
public void solve() {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
String s = in.nextLine();
String res = s;
int maxLen = 10;
int firstSpace = s.indexOf(" "); // whole sentence is one word and it's longer than max allowed
if (firstSpace > maxLen)
res = "";
else if (s.length() > maxLen) {
String cutStr = s.substring(0, maxLen);
res = cutStr.substring(0, Math.max(cutStr.lastIndexOf(" "), cutStr.length()));
}
out.print(res);
out.close();
}
}
Repannadwilliams31, Applications Developer at National Instruments
I am Anthony, Human Resources specialist with a decade of successful experience in hiring and employee management.I am a ...
RepPennyRMullins, xyz at Aspire Systems
I am a creative Interior Designer with 2+ years of experience building customized solutions for residential and commercial clients. Intuitively ...
RepLisaTQuinn, Analyst at A9
I have a passion for reading and creative writing and attend many workshops and conventions surrounding love spell that work ...
Reptracyremly, Anesthesiologist at Ness
I am Tracy, working as an Anesthesiologist handles surgical patients and their pain relief during procedures.Rather than my job ...
RepTristaRJohn, Reverse Engineering and System Developer at BT
I am an Ophthalmic medical assistant in bluefield USA, I assist in retinal exams and procedures. Referred patients to outside ...
Repmarygaustria, Analyst at ADP
I am Mary from Los Angeles, USA. I am working as a Manager in Fragrant Flower Lawn Services company. I ...
Repjosiredhima, AT&T Customer service email at Aricent
I am an architect with three years experience planning and designing commercial buildings. I am working as principal architect on ...
Repggeraldinejrivera, Aghori Mahakal Tantrik at ABC TECH SUPPORT
I am a fashion model, talented in runway turns and photoshoot prep. I Have worked at two displaying organizations and ...
Repsheilaahollins, Blockchain Developer at ASAPInfosystemsPvtLtd
Hello, I am working as a mineralogist studying rocks, gems, and other minerals, including their chemical and crystalline structures. I ...
Repshanitajjana, +27655765355 SSD MONEY CLEANING CHEMICAL +27655765355 BLACK MONEY IN JOHANNESBURG, OMAN, SAUDI ARABIA, SUDAN, Somalia ,Zimbabwe Botswana at 247quickbookshelp
Hi I am Shanita from San Bernardino USA. I am working as a manager in flore company. I am outgoing ...
Repjonesreina93, Hawaiian airlines reservations at American Airlines
Hello, I am currently working at the airport as an air hostess, and my responsibility are Check tickets and assisted ...
Repverarfurman, Problem Setter at Cerberus Capital
I am Vera from Bullard USA, I am working as a Violin repairer in a Handyman company. My interest in ...
Reppaulinedsisk, Testing / Quality Assurance at Cloudmere, Inc.
I want to become a successful professional in a highly competitive technological world where performance is rewarded with exciting new ...
Repjacksssones433, Apple Phone Number available 24/7 for our Customers at A9
Proficient and creative WordPress developer with a strong history in website management and development. Experienced in SEO and PPC campaign ...
Repkathyrnunez, Area Sales Manager at Advent
I am manager in a Star Bright Investment Group company. I like to solve puzzles for Has excellent problem-solving and ...
Repthelmasaraht, Applications Developer at Accolite software
A child protection social worker is responsible for a variety of services designed to help families and children in a ...
Repkiaspenill, HTML Experienced at Computer Associates
I am Kia, a skilled cook with 2+ years of experience. Extensive knowledge of the food industry and its relation ...
RepIndianastrologyguru, Data Scientist at ABC TECH SUPPORT
Hey I'm Shelly Renee and I'm working as a system developer. Currently working part time on a project ...
Repelisahbuff, Apple Phone Number available 24/7 for our Customers at Altera
I have previous work experience in a customer focused environment. I like to Enjoy my Free Time Playing football.I ...
Repaliciajdew, Integration Software Engineer at Absolute Softech Ltd
I have a strong base in marketing and I believe in focused strategies that bring brands to their relevant audiences ...
Repanitajrouse, Kuwait airways reservations at American Airlines
I am Anita from Hastings USA. I am working as a manager in Sofa Express company. I Managed the schedules ...
RepI believe in magic, power, aliens, parallel universe, god. I always dream about the powers and out of the world ...
Replestermauldin, Assignment editor at Royal Gas
I am Lester Assignment editor . I am responsible for organizing the assignment desk to operate around the clock. I often ...
Reprubyperezj, Backend Developer at A9
My name is Ruby Perez and I live in Lyndonville and it would be my honor and privilege to be ...
Repjoeevansjoe6, Repairer at Monlinks
I have been functioning as a repairman at monlinks organization for a long time . Here I learn numerous things . My ...
Repellahrivas6, Android test engineer at 247quickbookshelp
I am working as an internist and I work in medical offices, clinics, and hospitals. An internist may work independently ...
Repmakaylaangwin, Backend Developer at Abs india pvt. ltd.
I'm part owner of Bloom Floral Land, an Australian floral design company. Life is really short, try to smile ...
RepDonnaArvin, Analyst at Apple
Hii am from the United States. I work as a computer control programmer at Dynatronics Accessories. I am a social ...
RepCarlERhodes, Cloud Support Associate at Baidu
I am an Engineering manager in Mosier USA. I am a freelance events coordinator and a lifetime entrepreneur. I want ...
Repmarkemorgan007, Applications Developer at Big Fish
I am Mark From Fresno city in USA.I working as a tour guide and also help for make a ...
Repvictorcraigw, Animator at Chicago Mercantile Exchange
Hi, I am Victor working as a Speech Writer in the USA. Spoke at an academic conference about mantra for ...
Java solution using DFS (Depth First Search). In graph node is an index in initial array. One node is connected to some other one if the last letter equals to first letter of the second word. First we build a graph, and then run DFS starting from each node. If at some point we visit all nodes while traversing the graph, it means we found the solution.
- megido December 08, 2015