nmc
BAN USER//For words which are non duplicate
eg :- "This is a string which is duplicate"
ans: this
a
string
which
duplicate
static void printUniqueWords(String string){
List<String> list = new ArrayList<String>();
int map[]=new int[string.length()];
int start =0;int k =0;
for (int i = 0; i <= string.length(); i++) {
if(i == string.length() || string.charAt(i)== ' '){
if((k = list.indexOf(string.substring(start, i)))!= -1)
map[k] = 1;
else list.add(string.substring(start, i));
start = i+1;
}
}
for (int i = 0; i < list.size(); i++) {
if(map[i] != 1)
System.out.println(list.get(i));
}
}
static void printUniqueWords(String string){
char array[] = string.toCharArray();
HashSet<String> hashSet = new HashSet<String>();
int start = 0;
for (int i = 0; i <= array.length; i++) {
if(i == array.length || array[i] == ' '){
hashSet.add(string.substring(start, i));
start = i+1;
}
}
for (Iterator<String> iterator = hashSet.iterator(); iterator.hasNext();) {
String string2 = iterator.next();
System.out.println(string2);
}
}
static int findIndex(char array[],char target){
int lo = 0;
int hi = array.length-1;
while(lo <= hi){
while(lo <= hi && array[hi]=='$')hi--;
if(lo > hi) return -1;
int mid = (lo+hi)/2;
while(array[mid] == '$')mid++;
if(array[mid] == target)return mid;
if(array[mid] < target ) lo = mid+1;
else hi = mid-1;
}
return -1;
}
static String deleteCharsof2String(String fs,String ss){
int map[] = new int[256];
for (int i = 0; i < ss.length(); i++) {
map[ss.charAt(i)] =1;
}
char array[] = fs.toCharArray();
int k=0;
for (int i = 0; i < fs.length(); i++) {
if(map[fs.charAt(i)]!=1)
array[k++] = fs.charAt(i);
}
if(k != fs.length())
array[k] = '\n';
return new String(array);
}
static String reverseWords(String string){
char array[] = reverseArray(string.toCharArray(),0,string.length());
System.out.println(new String(array));
int start = 0;
for (int i = 0; i <= array.length; i++) {
if(i == array.length || array[i] == ' ' ){
array = reverseArray(array,start, i-start);
start = i+1;
}
}
return new String(array);
}
static char[] reverseArray(char array[],int start,int length){
for (int i = 0; i < length/2; i++) {
char tmp = array[i+start];
array[i+start] = array[start+length-1-i];
array[start+length-1-i] = tmp;
}
return array;
}
static BinaryNode trimTheTree(BinaryNode root,int min,int max){
if(root == null) return null;
if(root.value >= min && root.value <= max){
root.left = trimTheTree(root.left, min, max);
root.right = trimTheTree(root.right, min, max);
}else{
while(root != null && (root.value < min || root.value > max)){
if(root.value < min)
root = root.right;
else
root = root.left;
}
}
return root;
}
with not cleaning the memory
it's java :)
If hashmap is implemented using array, After generating the hash it can be inserted or deleted in O(1). But while iterating to go through only valid locations, we need some links. I guess if we add a double pointers same as in double linked list, I think this can be done.
- nmc July 21, 2011public class SubsetSum {
/**
* @param args
*/
public static void main(String[] args) {
int num[] = new int[]{11,16,11,9,7,13,5,3,1,14};
count(num,10);
}
static void count(int num[],int sum){
for(int i=0;i<Math.pow(2, num.length);i++){
int set[]= new int[num.length];
int tsum=0;
for(int j=0;j<num.length;j++){
if((i&(1<<j))!= 0){
set[j] = 1;
tsum+=num[j];
}
}
if(tsum == sum){
for(int k=0;k<num.length;k++){
if(set[k] == 1)System.out.print(" "+num[k]);
}
System.out.print(" : "+sum+" \n");
}
}
}
}
using bit operation we can get all the subsets but its limited by the input size :)
Actually the subset sum problem is a NP complete problem.
good solution
- nmc January 13, 2012