Untitled
raw download clone
TEXT
views 18
,
size 1918 b
package com.rally.health;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Test1 {

	static boolean isAnagram(String word1, String word2) {
		int n1 = word1.length();
		int n2 = word2.length();
		if (n1 != n2) return false;

		char[] c1 = word1.toCharArray();
		char[] c2 = word2.toCharArray();
		
		Arrays.sort(c1);
		Arrays.sort(c2);
		
		for (int i=0; i<n1; i++) 
			if (c1[i] != c2[i]) return false;
		
		return true;
	}
	
	
	public static void main(String[] args) {
		String sentence = "read words from console";
		String word = "fmor";
		
		String[] words = sentence.split(" ");
		
		
		ExecutorService executorService = Executors.newFixedThreadPool(words.length);

		List<Future<Boolean>> tasks = new ArrayList<>();
		for(String s : words)  {
			tasks.add(executorService.submit(new Anagremer(s, word)));
		}
		
		for(Future<Boolean> task : tasks)  {
			try {
				System.out.println(task.get());
			} catch (InterruptedException | ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}
	
	
	class Anagremer implements Callable<Boolean> {
		
		String word1, word2;
		
		public Anagremer(String word1, String word2) {
			this.word1 = word1;
			this.word2 = word2;
		}

		@Override
		public Boolean call() throws Exception {
			int n1 = word1.length();
			int n2 = word2.length();
			if (n1 != n2) return false;

			char[] c1 = word1.toCharArray();
			char[] c2 = word2.toCharArray();
			
			Arrays.sort(c1);
			Arrays.sort(c2);
			
			for (int i=0; i<n1; i++) 
				if (c1[i] != c2[i]) return false;
			
			return true;
		}
		
	}
close fullscreen
Login or Register to edit or fork this paste. It's free.