top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Given a dictionary of strings and another string find out if the string is an exact match to some words in dictionary...

+4 votes
613 views

Given a dictionary of strings and another string find out if the string is an exact match to some words in the dictionary or varies at most in only one place of some word of the dictionary?

posted Apr 25, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

We can use a trie for the dictionary, then iterate through the trie and check the above conditions.

Sample:

import java.util.ArrayList;
import java.lang.String;

class AnagramsApp
{

  public static void main(String[] args)
  {
    Anagrams an = new Anagrams();

    an.getAnagrams();
    an.displayOutput();
  }
}

class Anagrams
{
  String[] input={"tea","eat","ate","apple","java","vaja","cut","utc"};
  ArrayList<String> output;
  boolean[] match;

  public Anagrams()
  {
    match=new boolean[input.length];
  }

  public void getAnagrams()
  {
    for(int i=0;i<input.length-1;i++)
    {
      for(int j=i+1;j<input.length;j++)
      {
        int a=0;

        while(true)
        {
          String c= String.valueOf(input[j].charAt(a));

          if(input[i].length()!=input[j].length())
            break;

          else if(input[i].contains(c))
          {
            a++;
            if(a==input[i].length())
            {
              match[i]=true;
              match[j]=true;
              break;
            }
          }

          else 
            break;
        }
      }
    }
  }

  public void displayOutput()
  {
    for(int i=0;i<match.length;i++)
    {
      if(match[i]==true)
        System.out.print(input[i]+" ");
    }
    System.out.println();
  }
}
answer May 25, 2016 by Manikandan J
Similar Questions
+1 vote

Given a string and dictionary of words, form a word by removing minimum number of characters.
Characters can be removed in-order only.

+2 votes

Given a string and two words which are present in the string, find the minimum distance between the words

Example:
"the brown quick frog quick the", "the" "quick"
Min Distance: 1

"the quick the brown quick brown the frog", "the" "brown"
Min Distance: 2

C/Java code would be helpful?

+2 votes

For
e.g 1. "hello how are you" - distance between "hello" and "you" is 3.
e.g 2. "hello how are hello you" - distance is 1
e.g 3. "you are hello" - distance is -1. Order of "hello" and "you" should be preserved.
e.g 4. "hello how are hello" - distance is -1 since "you" did not occur even once.

...