Twitter Interview Question for Senior Software Development Engineers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 1 vote

// ZoomBA
m = mset ( words ) -> {  
  a = $.o.toCharArray ; sorta( a )
  str(a,'') 
}
anagrams = select(m) :: {  #|$.o.value| > 1 } -> { $.o.value }

- NoOne October 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Anagram {

  public static void main(String[] args) {
    // TODO Auto-generated method stub

    String[] input = new String[] {"tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc"};
    // Output : {"tea", "ate", "eat","java", "vaja", "cut", "utc"}
    List<String> inputList = new ArrayList<String>();
    for (String str : input)
      inputList.add(str);

    Set<String> list = new HashSet<String>();

    int i = 0;
    int j = 0;
    for (; i < inputList.size() - 1; i++) {
      System.out.println(list);
      for (j = i + 1; j < inputList.size(); j++) {
        if (checkAnagram(inputList.get(i), inputList.get(j))) {
          list.add(inputList.get(i));
          list.add(inputList.get(j));
          inputList.remove(j--);
        }
      }
    }

    System.out.println(list);
  }
  
  private static boolean checkAnagram(String str1, String str2) {

    if (str1.length() != str2.length()) {
      return false;
    }

    char[] s1Array = str1.toLowerCase().toCharArray();

    char[] s2Array = str2.toLowerCase().toCharArray();

    Arrays.sort(s1Array);

    Arrays.sort(s2Array);

    return Arrays.equals(s1Array, s2Array);

  }

}

- shanmuktarlana April 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>
#include <vector>
#include <map>

using namespace std;

int hash_code(string s){
   int hash = 0;
   for(int i=0;i<s.size();i++){
      hash+=(int)s[i];
   }

   return hash;
}

int main(){

  int N;
  vector<string> v;
  cin>>N;

  for(int i=0;i<N;i++){
    string s;
    cin>>s;
    v.push_back(s);
  }

  map<string, int> a;

  for(int i=0;i<v.size()-1;i++){
	for(int j=i+1;j<v.size();j++){
	    int h1 = hash_code(v[i]);
            int h2 = hash_code(v[j]);
	    if(h1 == h2){
		if(a[v[i]] != 1){
		   cout<<v[i]<<" ";
		}
		if(a[v[j]] != 1){
		   cout<<v[j]<<" ";
		}
		a[v[i]] = 1;
                a[v[j]] = 1;

	    }
	}
  }

  return 0;
}

- rheno April 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define ROW 8
#define COL 10
long long int getWordSum(char *str){
long long int sum = 0;
int k = 0;
while(*(str+k) != '\0'){
sum += *(str+k);
k++;
}
return sum;
}
int isAnagram(int index,long long int sum,long long int sumArr[],int length){
int i = 0;
for(i = 0; i < length; i++){
if(index == i)
;
else if(sum == sumArr[i])
return 1;
}
return 0;
}

int main(){
char data[ROW][COL] = {"tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc"};
long long int wordSum[ROW];
char *anagramArr[ROW];
int i;
int k = 0;
for(i = 0; i < ROW; i++){
anagramArr[i] = (char *) malloc(sizeof(COL));
}
for(i = 0; i < ROW; i++){
wordSum[i] = getWordSum(data[i]);
}
//now check for anagram
for(i = 0; i < ROW; i++){
if(isAnagram(i,wordSum[i],wordSum,ROW)){
anagramArr[k] = data[i];
k++;
}
}
for(i = 0; i < k; i++)
printf("%s\n",anagramArr[i]);
return 0;
}

- Shahid Rabbani April 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define ROW 8
#define COL 10
long long int getWordSum(char *str){
long long int sum = 0;
int k = 0;
while(*(str+k) != '\0'){
sum += *(str+k);
k++;
}
return sum;
}
int isAnagram(int index,long long int sum,long long int sumArr[],int length){
int i = 0;
for(i = 0; i < length; i++){
if(index == i)
;
else if(sum == sumArr[i])
return 1;
}
return 0;
}

int main(){
char data[ROW][COL] = {"tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc"};
long long int wordSum[ROW];
char *anagramArr[ROW];
int i;
int k = 0;
for(i = 0; i < ROW; i++){
anagramArr[i] = (char *) malloc(sizeof(COL));
}
for(i = 0; i < ROW; i++){
wordSum[i] = getWordSum(data[i]);
}
//now check for anagram
for(i = 0; i < ROW; i++){
if(isAnagram(i,wordSum[i],wordSum,ROW)){
anagramArr[k] = data[i];
k++;
}
}
for(i = 0; i < k; i++)
printf("%s\n",anagramArr[i]);
return 0;
}

- Shahid Rabbani April 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <list>
#include<map>
#include<string>
#include<algorithm>

using namespace std;
void FindAnagram(string,string,map<string,int>&);
bool map_compare(const map<char,int>m1,const map<char,int>m2){
return (m1.size() == m2.size() && equal(m1.begin(),m1.end(),m2.begin()));
}

int main()
{
list<string> lists;
lists.push_back("tea");
lists.push_back("ate");
lists.push_back("eat");
lists.push_back("apple");
lists.push_back("java");
lists.push_back("vaja");
lists.push_back("cut");
lists.push_back("utc");
string input [] = {"tea","ate","eat","apple","java","vaja","cut","utc"};
map<string,int> mapString;
int len = (sizeof(input)/sizeof(string));
cout<<len;
for(int i=0;i<len-1;i++){
for(int j=i+1;j<len;j++){
FindAnagram(input[i],input[j],mapString);
}
}

//display output
map<string,int>::iterator mtr = mapString.begin();
while(mtr != mapString.end()){
cout<<mtr->first<<endl;
mtr++;
}
return 0;
}
void FindAnagram(string str1,string str2,map<string,int>& output){
//check if the size of the strings are same
if(str1.length() != str2.length())
return;

int charMatch[256]={0};
for(int i=0;i<str1.length();i++){
int asci = (int)str1[i];
charMatch[asci]++;
asci = (int) str2[i];
charMatch[asci]--;
}
//make sure the charMatch has all entry zero
bool flag = true;
for(int i=0;i<256;i++){
if(charMatch[i] !=0){
flag = false;
}
}
if(flag == true){
output[str1]++;
output[str2]++;
cout<<"Match found";
}

//shorter version to check the strings:
string s1 = str1;
string s2 = str2;
sort(s1.begin(),s1.end());
sort(s2.begin(),s2.end());
if(s1.compare(s2) ==0){
cout<<"matching"<<s1<<" and "<<s2<<endl;
}

}

- deepthi.shetty90 April 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.omt.learn.algo;

import java.util.Set;
import java.util.TreeSet;

public class FindAnagrams {

	public static void main(String[] args) {
		String[] input = new String[] { "tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc" };

		Set<String> output = new TreeSet<>();

		for (String s : input) {
			if (output.contains(s)) {
				System.out.println(s);
				output.remove(s);
			} else {
				Set<String> set = getAllPossibleAnagram("", s);
				set.remove(s);
				out: for (String st : set) {

					for (String inp : input) {
						if (set.contains(inp)) {
							System.out.println(s);
							output.addAll(set);
							break out;
						}
					}

				}
			}
		}

	}

	public static Set<String> getAllPossibleAnagram(String prefix, String s) {

		Set<String> set = new TreeSet<>();

		// System.out.println(prefix);
		if (s.length() == 0) {
			set.add(prefix);
			return set;
		}

		for (int i = 0; i < s.length(); i++) {

			set.addAll(getAllPossibleAnagram(prefix + s.charAt(i), s.substring(0, i) + s.substring(i + 1)));
		}

		return set;
	}

}

- dhiralpandya April 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it's a not well performed solution, but here we go:

public class Anagrams{
	
	public static void main(String[] args){
		new Anagrams().solution(new String[]{"tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc"});
	}
	
	
	private void solution(String[] args){
		Map<String, List<String>> mapBase = new HashMap<>();

		for(String word : args){

			boolean found = false;

			for(String key : mapBase.keySet()){

				if(key.length() == word.length()){

					int i=0;
					for(; i<word.length(); i++){

						if(key.indexOf(word.charAt(i)) == -1){

							break;
						}
					}

					if(i == word.length()){
						found = true;
						mapBase.get(key).add(word);		
						break;
					}
				}
			}

			if(!found){
				mapBase.put(word, new ArrayList<>());
				mapBase.get(word).add(word);
			}
		}

		List<String> finalAnagrams = new ArrayList<>();
		for(String key : mapBase.keySet()){
			if(mapBase.get(key).size() > 1){
				for(String word : mapBase.get(key)){
					finalAnagrams.add(word);
				}
			}
		}

		System.out.println(finalAnagrams);
	}
}

- Anonymous April 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's not a well performed solution, but here we go:

public class Anagrams{
	
	public static void main(String[] args){
		new Anagrams().solution(new String[]{"tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc"});
	}
	
	
	private void solution(String[] args){
		Map<String, List<String>> mapBase = new HashMap<>();

		for(String word : args){

			boolean found = false;

			for(String key : mapBase.keySet()){

				if(key.length() == word.length()){

					int i=0;
					for(; i<word.length(); i++){

						if(key.indexOf(word.charAt(i)) == -1){

							break;
						}
					}

					if(i == word.length()){
						found = true;
						mapBase.get(key).add(word);		
						break;
					}
				}
			}

			if(!found){
				mapBase.put(word, new ArrayList<>());
				mapBase.get(word).add(word);
			}
		}

		List<String> finalAnagrams = new ArrayList<>();
		for(String key : mapBase.keySet()){
			if(mapBase.get(key).size() > 1){
				for(String word : mapBase.get(key)){
					finalAnagrams.add(word);
				}
			}
		}

		System.out.println(finalAnagrams);
	}
}

- uebi.belli April 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

another solution :

public static HashSet<String> getAnagram(String [] given){

HashSet<String> result = new HashSet<String>();
if(given == null){
return result;
}
HashMap<String, String> hm = new HashMap<String, String>();
for (String str : given){
char[] tempstr = str.toCharArray();
Arrays.sort(tempstr);
String sortedkey = new String(tempstr);
System.out.println("sorted :" + sortedkey);
if (hm.containsKey(sortedkey)== false){
hm.put(sortedkey, str);
}
else {
String tempvalue = hm.get(sortedkey);
tempvalue = tempvalue + "," + str;
hm.put(sortedkey, tempvalue);
}
}

for (String str : hm.values()){
String[] temp = str.split(",");
if (temp.length > 1){
for (int i = 0; i < temp.length; i ++){
result.add(temp[i]);
}
}
}
System.out.println("result : ");
for (String ena : result)
System.out.print(ena +",");
return result;
}

- Rohan Tare April 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

another solution

public static HashSet<String> getAnagram(String [] given){
		
		HashSet<String> result = new HashSet<String>();
		if(given == null){
			return result;
		}
		HashMap<String, String> hm = new HashMap<String, String>();
		for (String str : given){
			char[] tempstr = str.toCharArray();
			Arrays.sort(tempstr);
			String sortedkey = new String(tempstr);
			System.out.println("sorted :" + sortedkey);
			if (hm.containsKey(sortedkey)== false){
				hm.put(sortedkey, str);
			}
			else {
				String tempvalue = hm.get(sortedkey);
				tempvalue = tempvalue + "," + str;
				hm.put(sortedkey, tempvalue);
  			} 
		}
		
		for (String str : hm.values()){
			String[] temp = str.split(",");
			if (temp.length > 1){
				for (int i = 0; i < temp.length; i ++){
				result.add(temp[i]);
				}
			}
		}
		System.out.println("result : ");
		for (String ena : result)
		System.out.print(ena +",");
		return result;
	}

- Rohan Tare April 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static HashSet<String> getAnagram(String [] given){
		
		HashSet<String> result = new HashSet<String>();
		if(given == null){
			return result;
		}
		HashMap<String, String> hm = new HashMap<String, String>();
		for (String str : given){
			char[] tempstr = str.toCharArray();
			Arrays.sort(tempstr);
			String sortedkey = new String(tempstr);
			System.out.println("sorted :" + sortedkey);
			if (hm.containsKey(sortedkey)== false){
				hm.put(sortedkey, str);
			}
			else {
				String tempvalue = hm.get(sortedkey);
				tempvalue = tempvalue + "," + str;
				hm.put(sortedkey, tempvalue);
  			} 
		}
		
		for (String str : hm.values()){
			String[] temp = str.split(",");
			if (temp.length > 1){
				for (int i = 0; i < temp.length; i ++){
				result.add(temp[i]);
				}
			}
		}
		System.out.println("result : ");
		for (String ena : result)
		System.out.print(ena +",");
		return result;
	}

- Rohan1985 April 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static HashSet<String> getAnagram(String [] given){
		
		HashSet<String> result = new HashSet<String>();
		if(given == null){
			return result;
		}
		HashMap<String, String> hm = new HashMap<String, String>();
		for (String str : given){
			char[] tempstr = str.toCharArray();
			Arrays.sort(tempstr);
			String sortedkey = new String(tempstr);
			System.out.println("sorted :" + sortedkey);
			if (hm.containsKey(sortedkey)== false){
				hm.put(sortedkey, str);
			}
			else {
				String tempvalue = hm.get(sortedkey);
				tempvalue = tempvalue + "," + str;
				hm.put(sortedkey, tempvalue);
  			} 
		}
		
		for (String str : hm.values()){
			String[] temp = str.split(",");
			if (temp.length > 1){
				for (int i = 0; i < temp.length; i ++){
				result.add(temp[i]);
				}
			}
		}
		System.out.println("result : ");
		for (String ena : result)
		System.out.print(ena +",");
		return result;

- Rohan1985 April 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String input[] = { "tea", "ate", "eat", "apple", "java", "vaja", "cut","utc","men","hen" };
System.out.println("length--> " + input.length);
System.out.println("Input: " + Arrays.toString(input));
Map<String, Integer> output = new HashMap<String, Integer>();
List<String> removal = new ArrayList<String>(input.length);

for (int i = 0; i < input.length; i++) {

char[] ch = input[i].toCharArray();
int sum = 0;
int j = 0;
while (j < ch.length) {
sum += ch[j];
j++;
}

output.put(input[i], sum);

//System.out.println(" " + input[i] + " sum:" + sum);

}

List<Integer> array = new ArrayList<Integer>(output.values());

for (int i = 0; i < array.size(); i++) {
int count = 0;
for (int j = 0; j < array.size(); j++) {

if ((int) array.get(i) == (int) array.get(j)) {
count++;
}
}
if (count == 1) {

Set<String> list = output.keySet();

for (String str : list) {

if ((int) output.get(str) == (int) array.get(i)) {
removal.add(str);
}
}

}

}

for (String str : removal) {
output.remove(str);
}

System.out.println("Output: " + output.keySet().toString());
}

- Pawan Aravapalli April 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String input[] = { "tea", "ate", "eat", "apple", "java", "vaja", "cut","utc","men","hen" };
System.out.println("length--> " + input.length);
System.out.println("Input: " + Arrays.toString(input));
Map<String, Integer> output = new HashMap<String, Integer>();
List<String> removal = new ArrayList<String>(input.length);

for (int i = 0; i < input.length; i++) {

char[] ch = input[i].toCharArray();
int sum = 0;
int j = 0;
while (j < ch.length) {
sum += ch[j];
j++;
}

output.put(input[i], sum);

//System.out.println(" " + input[i] + " sum:" + sum);

}

List<Integer> array = new ArrayList<Integer>(output.values());

for (int i = 0; i < array.size(); i++) {
int count = 0;
for (int j = 0; j < array.size(); j++) {

if ((int) array.get(i) == (int) array.get(j)) {
count++;
}
}
if (count == 1) {

Set<String> list = output.keySet();

for (String str : list) {

if ((int) output.get(str) == (int) array.get(i)) {
removal.add(str);
}
}

}

}

for (String str : removal) {
output.remove(str);
}

System.out.println("Output: " + output.keySet().toString());
}

- Pawan Aravapalli April 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String input[] = { "tea", "ate", "eat", "apple", "java", "vaja", "cut","utc","men","hen" };
		System.out.println("length--> " + input.length);
		System.out.println("Input: " + Arrays.toString(input));
		Map<String, Integer> output = new HashMap<String, Integer>();
		List<String> removal = new ArrayList<String>(input.length);

		for (int i = 0; i < input.length; i++) {

			char[] ch = input[i].toCharArray();
			int sum = 0;
			int j = 0;
			while (j < ch.length) {
				sum += ch[j];
				j++;
			}

			output.put(input[i], sum);

			//System.out.println(" " + input[i] + " sum:" + sum);

		}

		List<Integer> array = new ArrayList<Integer>(output.values());

		for (int i = 0; i < array.size(); i++) {
			int count = 0;
			for (int j = 0; j < array.size(); j++) {

				if ((int) array.get(i) == (int) array.get(j)) {
					count++;
				}
			}
			if (count == 1) {

				Set<String> list = output.keySet();

				for (String str : list) {

					if ((int) output.get(str) == (int) array.get(i)) {
						removal.add(str);
					}
				}

			}

		}

		for (String str : removal) {
			output.remove(str);
		}

		System.out.println("Output: " + output.keySet().toString());
	}

- Pawan Aravapalli April 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String input[] = { "tea", "ate", "eat", "apple", "java", "vaja", "cut","utc","men","hen" };
		System.out.println("length--> " + input.length);
		System.out.println("Input: " + Arrays.toString(input));
		Map<String, Integer> output = new HashMap<String, Integer>();
		List<String> removal = new ArrayList<String>(input.length);

		for (int i = 0; i < input.length; i++) {

			char[] ch = input[i].toCharArray();
			int sum = 0;
			int j = 0;
			while (j < ch.length) {
				sum += ch[j];
				j++;
			}

			output.put(input[i], sum);

			//System.out.println(" " + input[i] + " sum:" + sum);

		}

		List<Integer> array = new ArrayList<Integer>(output.values());

		for (int i = 0; i < array.size(); i++) {
			int count = 0;
			for (int j = 0; j < array.size(); j++) {

				if ((int) array.get(i) == (int) array.get(j)) {
					count++;
				}
			}
			if (count == 1) {

				Set<String> list = output.keySet();

				for (String str : list) {

					if ((int) output.get(str) == (int) array.get(i)) {
						removal.add(str);
					}
				}

			}

		}

		for (String str : removal) {
			output.remove(str);
		}

		System.out.println("Output: " + output.keySet().toString());
	}

- Pawan April 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My Haskell solution:

import Data.List
words = ["tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc"]
compareFirst (x, _) (y, _) = x == y

-- 1. For each word, create a pair which consists of the sorted letters and the word itself.
-- 2. Sort the list of these pairs.
-- 3. Group the consecutive pairs which have the same sorted letters (i.e., the anagrams)
-- 4. Only consider the groups which have more than one such pair.
-- 5. Concatenate the groups with ++.
-- 6. Only take the second item of the pair, i.e., drop the sorted letters and just keep the word.

map snd $ foldl1 (++) $ filter ((>1).length) $ groupBy compareFirst $ sort $ map (\w -> (sort w, w)) words

- Frank April 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		String[] inputArray = {"tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc"};
		List<String> output = new ArrayList<String>();
		for (int i = 0; i< inputArray.length; i++)
		{
			for (int j = 0; j < inputArray.length; j++)
			{
				if (IsAnagram(inputArray[i], inputArray[j]) && i != j)
				{
					output.add(inputArray[i]);
					break;
				}
			}
		}
		System.out.println(output);
	}
	
	public static Boolean IsAnagram(String firstWord, String secondWord)
	{
		char[] firstWordChar = firstWord.toCharArray();
		char[] secondWordChar = secondWord.toCharArray();
		
		Arrays.sort(firstWordChar);
		Arrays.sort(secondWordChar);
		return Arrays.equals(firstWordChar, secondWordChar);
	}
}

- Justin Lim April 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		String[] inputArray = {"tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc"};
		List<String> output = new ArrayList<String>();
		for (int i = 0; i< inputArray.length; i++)
		{
			for (int j = 0; j < inputArray.length; j++)
			{
				if (IsAnagram(inputArray[i], inputArray[j]) && i != j)
				{
					output.add(inputArray[i]);
					break;
				}
			}
		}
		System.out.println(output);
	}
	
	public static Boolean IsAnagram(String firstWord, String secondWord)
	{
		char[] firstWordChar = firstWord.toCharArray();
		char[] secondWordChar = secondWord.toCharArray();
		
		Arrays.sort(firstWordChar);
		Arrays.sort(secondWordChar);
		return Arrays.equals(firstWordChar, secondWordChar);
	}
}

- Justin Lim April 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static List<List<string>> AnagramsWords(List<string> words)
{
List<List<string>> ret = new List<List<string>>();

while (words.Count > 0)
{
List<string> temp = new List<string>();
temp.Add(words[0]);
for (int j = 1; j < words.Count; j++)
{
if (AreAnagrams(words[0], words[j]))
{
temp.Add(words[j]);

}
}
for (int k = 0; k < temp.Count; k++)
{
words.Remove(temp[k]);
}
ret.Add(temp);
}
return ret;
}
public static bool AreAnagrams(string word1, string word2)
{
if (word1.Length != word2.Length)
{
return false;
}
Dictionary<char, int> count = new Dictionary<char, int>();
List<char> word1Lettres = word1.ToList<char>();
word1Lettres.Sort();
List<char> word2Lettres = word2.ToList<char>();
word2Lettres.Sort();

for (int i = 0; i < word1Lettres.Count; i++)
{
if (word1Lettres[i] != word2Lettres[i])
{
return false;
}
}
return true;

}

- mark.nag April 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#Find the anagrams from a list of strings 
#
#Input : ['tea', 'ate', 'eat', 'apple', 'java', 'vaja', 'cut', 'utc']
#Output : ['tea', 'ate', 'eat','java', 'vaja', 'cut', 'utc']
in_str=['tea', 'ate', 'eat', 'apple', 'java', 'vaja', 'cut', 'utc']
output=[]
t=True
i=0
j=i+1
l=len(in_str)
print "Input string is: ",in_str
while t and i<l-1:
	print "in_str[i] and in_str[j]",in_str[i],in_str[j]
	if in_str[i] != in_str[j] and (sum(ord(i) for i in in_str[i])) == (sum(ord(i) for i in in_str[j])):
		if len(output)==0 or i==j-1:
			output.append(in_str[i])
			output.append(in_str[j])
		else:
			output.append(in_str[j])
		print "i,j",i,j
		j=j+1
		i=i+1
	else:
		if j>=l-1:
			i=i+1
			j=i
		j=j+1
	print "i,j",i,j


print "output is: ",output

- Sasi May 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#Find the anagrams from a list of strings 
#
#Input : ['tea', 'ate', 'eat', 'apple', 'java', 'vaja', 'cut', 'utc']
#Output : ['tea', 'ate', 'eat','java', 'vaja', 'cut', 'utc']
in_str=['tea', 'ate', 'eat', 'apple', 'java', 'vaja', 'cut', 'utc']
output=[]
t=True
i=0
j=i+1
l=len(in_str)
print "Input string is: ",in_str
while t and i<l-1:
	print "in_str[i] and in_str[j]",in_str[i],in_str[j]
	if in_str[i] != in_str[j] and (sum(ord(i) for i in in_str[i])) == (sum(ord(i) for i in in_str[j])):
		if len(output)==0 or i==j-1:
			output.append(in_str[i])
			output.append(in_str[j])
		else:
			output.append(in_str[j])
		print "i,j",i,j
		j=j+1
		i=i+1
	else:
		if j>=l-1:
			i=i+1
			j=i
		j=j+1
	print "i,j",i,j


print "output is: ",output

- Sasi May 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{code}
#Find the anagrams from a list of strings
#
#Input : ['tea', 'ate', 'eat', 'apple', 'java', 'vaja', 'cut', 'utc']
#Output : ['tea', 'ate', 'eat','java', 'vaja', 'cut', 'utc']
in_str=['tea', 'ate', 'eat', 'apple', 'java', 'vaja', 'cut', 'utc']
output=[]
t=True
i=0
j=i+1
l=len(in_str)
print "Input string is: ",in_str
while t and i<l-1:
print "in_str[i] and in_str[j]",in_str[i],in_str[j]
if in_str[i] != in_str[j] and (sum(ord(i) for i in in_str[i])) == (sum(ord(i) for i in in_str[j])):
if len(output)==0 or i==j-1:
output.append(in_str[i])
output.append(in_str[j])
else:
output.append(in_str[j])
print "i,j",i,j
j=j+1
i=i+1
else:
if j>=l-1:
i=i+1
j=i
j=j+1
print "i,j",i,j


print "output is: ",output
{code}

- Anonymous May 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Arrays;
import java.util.HashSet;

/*
 * Find the anagrams from a list of strings 

 *Input : {"tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc"} 
 *Output : {"tea", "ate", "eat","java", "vaja", "cut", "utc"}
 
 */

public class Anagram {
	
	public static void main(String[] agrs){
		
		String arr[]= {"tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc"};
		String newarr[]=new String[arr.length];
		int i=0;
		for (String a : arr){
			
			char[] c = a.toCharArray();
			Arrays.sort(c);
			
			String s=new String(c);
			newarr[i]=s;
			i++;
		}
		
		HashSet<String> hs = new HashSet<String>();
		for (int k=0;k<arr.length;k++){
			for(int j=k+1;j<arr.length;j++){
				
				if(newarr[k].equals(newarr[j])){
					hs.add(arr[k]);
					hs.add(arr[j]);
				}
				
			}
		}
		
		System.out.print(hs.toString());
	}
}

- ojas May 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Arrays;
import java.util.HashSet;

/*
 * Find the anagrams from a list of strings 

 *Input : {"tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc"} 
 *Output : {"tea", "ate", "eat","java", "vaja", "cut", "utc"}
 
 */

public class Anagram {
	
	public static void main(String[] agrs){
		
		String arr[]= {"tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc"};
		String newarr[]=new String[arr.length];
		int i=0;
		for (String a : arr){
			
			char[] c = a.toCharArray();
			Arrays.sort(c);
			
			String s=new String(c);
			newarr[i]=s;
			i++;
		}
		
		HashSet<String> hs = new HashSet<String>();
		for (int k=0;k<arr.length;k++){
			for(int j=k+1;j<arr.length;j++){
				
				if(newarr[k].equals(newarr[j])){
					hs.add(arr[k]);
					hs.add(arr[j]);
				}
				
			}
		}
		
		System.out.print(hs.toString());
	}
}

- ojas May 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

x = ['tea', 'ate', 'eat', 'apple', 'java', 'vaja', 'cut', 'utc']
def get_anagrams(x):
    y = {}
    for s in x:
        p = ''.join(sorted(s))
        if p not in y:
            y[p] = s
        else:
            if y.get(p) not in x:
                x.remove(y.get(p))
    return x

print get_anagrams(x)

- Indraja.Punna June 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <bits/stdc++.h>

using namespace std;

void findAnagram(string arr [], int n){

    map <string, vector <string> > m;
    for (int i = 0; i < n; i++){
        string temp = arr[i];
        sort(arr[i].begin(), arr[i].end());
        m[arr[i]].push_back(temp);
    }

    for (map<string, vector<string> >::iterator it = m.begin();
        it != m.end(); it++){
            vector <string> v = it->second;
            if (v.size() == 1) continue;
            for (int i = 0; i < v.size(); i++){
                cout << v[i] << " ";
            }
            cout << endl;
    }

}

int main()
{
    string arr [] = {"tea", "ate", "eat", "apple", "java", 

"vaja" ,"cut", "utc"};
    findAnagram(arr, 8);
    return 0;
}

- aalaa.abdelMawla October 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

val input = Seq("tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc")

  val res = input
    .map(w => (w, w.toCharArray.sorted.mkString))
    .groupBy(_._2)
    .values
    .filter(_.size > 1)
    .flatMap(_.map(_._1))

  println(res.toList)

- Thang October 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

val input = Seq("tea", "ate", "eat", "apple", "java", "vaja", "cut", "utc")

  val res = input
    .map(w => (w, w.toCharArray.sorted.mkString))
    .groupBy(_._2)
    .values
    .filter(_.size > 1)
    .flatMap(_.map(_._1))

  println(res.toList)

- thangiee12 October 13, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More