First Orion Interview Question
SDE1sCountry: United States
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WordIntersections
{
private Map<Character, List<Integer>> toMap(String word)
{
Map<Character, List<Integer>> map = new HashMap<>();
for (int index = 0; index < word.length(); index++)
{
map.computeIfAbsent(word.charAt(index), k -> new ArrayList<Integer>()).add(index);
}
return map;
}
private String position(int n)
{
int num = n + 1;
String str = String.valueOf(num);
switch (str.charAt(str.length() - 1))
{
case '1':
return num + "st";
case '2':
return num + "nd";
case '3':
return num + "rd";
default:
return num + "th";
}
}
public List<String> getIntersections(String word1, String word2)
{
List<String> intersections = new ArrayList<>();
Map<Character, List<Integer>> map = toMap(word2);
for (int index = 0; index < word1.length(); index++)
{
List<Integer> integers = map.get(word1.charAt(index));
if (integers != null)
{
for (Integer i : integers)
{
intersections
.add("The " + position(index) + " letter of " + word1 + " intersects with " + position(i) + " letter of " + word2 + ".");
}
}
}
return intersections;
}
}
#include <iostream>
#include <string>
using namespace std;
static int count=0;
void interceptCounter(string &a, string &b){
for (int i = 0; i<a.length(); i++){
for(int z=0; z<b.length(); z++){
if( a[i]==b[z]){
++count;
cout<<"The "<<i+1<<"th value of a intercepts with the "<<z+1<<"th value of b\n";
}
}
}
cout<<"There are "<<count<<" interceptions"<<endl;
}
int main() {
string err= "thessalonians";
string merr = "collosians";
interceptCounter(err, merr);
// your code goes here
return 0;
}
#include <iostream>
- Mobolaji January 07, 2018#include <string>
using namespace std;
static int count=0;
void interceptCounter(string &a, string &b){
for (int i = 0; i<a.length(); i++){
for(int z=0; z<b.length(); z++){
if( a[i]==b[z]){
++count;
cout<<"The "<<i+1<<"th value of a intercepts with the "<<z+1<<"th value of b\n";
// if you want to do a switch case for the th,nd, and st of the worlds suit thyself.
}
}
}
cout<<"There are "<<count<<" interceptions"<<endl;
}
int main() {
string err= "thessalonians";
string merr = "collosians";
interceptCounter(err, merr);
return 0;
}