romina
BAN USERpublic class PlaceHolderDictionnary {
Set<String> dictionnary;
public PlaceHolderDictionnary(List<String> words) {
this.dictionnary = new HashSet<String>();
for (String word : words) {
add(word);
}
}
private void add(String word) {
this.dictionnary.add(word);
int stars = 1;
while (stars <= word.length()) {
for (int i = 0; i + stars <= word.length(); i++) {
char[] strToChars = word.toCharArray();
Arrays.fill(strToChars, i, i + stars, '*');
this.dictionnary.add(new String(strToChars));
}
stars++;
}
}
public String answer(String word) {
return dictionnary.contains(word) ? "YES" : "NO";
}
public static void main(String[] args) {
PlaceHolderDictionnary placeHolderDictionnary = new PlaceHolderDictionnary(Arrays.asList("hazem", "ahmed", "moustafa", "fizo"));
System.out.println(placeHolderDictionnary.answer("ahmed"));
System.out.println(placeHolderDictionnary.answer("m**stafa"));
System.out.println(placeHolderDictionnary.answer("fizoo"));
System.out.println(placeHolderDictionnary.answer("fizd"));
System.out.println(placeHolderDictionnary.answer("*****"));
System.out.println(placeHolderDictionnary.answer("****"));
System.out.println(placeHolderDictionnary.answer("**"));
}
}
public class PermutateStrings {
static List<String> perms(List<String> words, int current) {
if (current == words.size())
return new ArrayList<String>();
String word = words.get(current);
List<String> results = new ArrayList<String>();
List<String> sub = perms(words, current + 1);
if (sub.isEmpty()) {
for (int i = 0; i < word.length(); i++) {
results.add("" + word.charAt(i));
}
} else {
for (int i = 0; i < word.length(); i++) {
for (String subWord : sub) {
results.add(word.charAt(i) + subWord);
}
}
}
return results;
}
public static void main(String[] args) {
System.out.println(perms(Arrays.asList("red", "fox", "super"), 0));
}
}
static String findSubstring(String str, int n) {
String result = "";
for (int i = 0; i < str.length(); i++) {
if (str.length() - i <= result.length())
continue;
for (int j = str.length(); j > i; j--) {
String subChain = str.substring(i, j);
if (hasUniqueCharacters(subChain, n) && result.length() < subChain.length())
result = subChain;
}
}
return result;
}
static boolean hasUniqueCharacters(String str, int n) {
Set<Character> uniques = new HashSet<Character>();
for (int i = 0; i < str.length(); i++) {
uniques.add(str.charAt(i));
if (uniques.size() > n) {
return false;
}
}
return uniques.size() == n ? true : false;
}
public static int findPeek(int[] arr) {
if (arr[0] < arr[arr.length - 1])
return -1;
int lo = 0;
int hi = arr.length - 1;
return findPeek(arr, lo, hi);
}
private static int findPeek(int[] arr, int lo, int hi) {
if (lo > hi)
return -1;
int mid = (lo + hi) / 2;
if (mid != hi && arr[mid] > arr[mid + 1])
return mid + 1;
if (arr[lo] > arr[mid]) {
return findPeek(arr, lo, mid);
} else {
return findPeek(arr, mid + 1, hi);
}
}
public static void rearrange(int[] arr) {
int peek = findPeek(arr);
if (peek == -1) {
return;
}
int[] aux = new int[arr.length];
for (int i = 0; i < aux.length; i++) {
aux[i] = arr[i];
}
int pos = 0;
for (int i = peek; i < aux.length; i++) {
arr[pos++] = aux[i];
}
for (int i = 0; i < peek; i++) {
arr[pos++] = aux[i];
}
}
public static String add(String str1, String str2) {
int i = str1.length() - 1;
int j = str2.length() - 1;
boolean rem = false;
StringBuilder builder = new StringBuilder();
while (i >= 0 && j >= 0) {
char a = str1.charAt(i--);
char b = str2.charAt(j--);
if (a == '1' && b == '1') {
if (rem) {
builder.append('1');
} else {
builder.append('0');
}
rem = true;
} else if (a == '0' && b == '0') {
if (rem) {
builder.append('1');
} else {
builder.append('0');
}
rem = false;
} else {
if (rem) {
builder.append('0');
} else {
builder.append('1');
}
}
}
int c = i > j ? i : j;
String strRem = i > j ? str1 : str2;
while (c >= 0) {
char a = strRem.charAt(c--);
if (a == '0' && rem) {
builder.append('1');
rem = false;
} else if (a == '1' && rem) {
builder.append('0');
} else {
builder.append(a);
}
}
if (rem)
builder.append('1');
return builder.reverse().toString();
}
public class EvenOddLinkedList {
Node head;
Node last;
public EvenOddLinkedList add(int data) {
Node node = new Node(data);
if (head == null) {
head = node;
} else {
last.next = node;
}
last = node;
return this;
}
static class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
}
@Override
public String toString() {
return data + "";
}
}
public void print() {
Node current = head;
while (current != null) {
System.out.println(current.data + " ");
current = current.next;
}
}
public void swap() {
if (head == null || head.next == null)
return;
Node c = head;
Node runner = head.next.next;
head = head.next;
while (runner != null) {
c.next.next = c;
c.next = runner.next == null ? runner : runner.next;
c = runner;
if (runner.next != null)
runner = runner.next.next;
else
runner = null;
}
if (c.next != null) {
c.next.next = c;
c.next = null;
}
last = c;
}
public static void main(String[] args) {
EvenOddLinkedList evenOddLinkedList = new EvenOddLinkedList().add(1).add(2).add(3).add(4).add(5).add(6);
evenOddLinkedList.print();
System.out.println("======");
evenOddLinkedList.swap();
evenOddLinkedList.print();
}
}
public static boolean isPaldrome(String str) {
if (str == null || str.length() == 0)
return false;
int i = 0;
int j = str.length() - 1;
while (i < j) {
char left = str.charAt(i);
char right = str.charAt(j);
while (!Character.isAlphabetic(left) && i < str.length()) {
left = str.charAt(++i);
}
while (!Character.isAlphabetic(right) && j >= 0) {
right = str.charAt(--j);
}
if (Character.toLowerCase(right) != Character.toLowerCase(left))
return false;
i++;
j--;
}
return true;
}
- romina June 06, 2015