Krishna
BAN USERstatic boolean findIsomorphic( String str1, String str2 ) {
char[] ch1 = str1.toCharArray();
char[] ch2 = str2.toCharArray();
Map<Character, Set<Character>> map = new HashMap<Character, Set<Character>>();
Set<Character> set = null;
for( int i = 0 ; i< ch1.length; i++ ) {
Character ch = ch1[i];
if(map.containsKey(ch)) {
Set<Character> s = map.get(ch);
Character setCh = ch2[i];
if(!s.contains(setCh)){
s.add(setCh);
}
}
else {
set = new HashSet<Character>();
set.add(ch2[i]);
map.put(ch, set);
}
}
Set<Character> keySet = map.keySet();
Iterator<Character> it = keySet.iterator();
Boolean bool = true;
while( it.hasNext() ) {
Character ch = it.next();
Set<Character> finalSet = map.get(ch);
if( finalSet.size() > 1 ) {
bool = false;
}
}
return bool;
}
Code without using recursion .
static Node reverseAlternateElements(Node node) {
Node retNode1= null;
Node temp = node;
Node first = null;
Node second = null;
Node current = temp.next.next;
temp.next.next = null;
first = temp;
second = temp.next;
first.next = null;
second.next = null;
second.next = first;
temp = null;
retNode1 = second;
Node returnNodeTemp = null;
// returnNodeTemp = returnNode;
returnNodeTemp = retNode1.next;
while (current != null ) {
temp = null;
temp = current;
if( current.next == null ) {
returnNodeTemp.next = current;
break;
}
current = current.next.next;
first = null;
second = null;
first = temp;
second = temp.next;
first.next = null;
second.next = null;
second.next = first;
returnNodeTemp.next = second;
returnNodeTemp = returnNodeTemp.next.next;
}
return retNode1;
}
public class MaximumParanthesis {
public static void main(String[] args) {
String str = "abc(123(xyz))m((((n))))o";
findDepthElement(str);
}
static void findDepthElement(String str ) {
Stack st = new Stack();
int i = 0;
int count = 0;
while( i < str.length() ) {
System.out.println(str.charAt(i));
if(str.charAt(i) == '(') {
st.push(str.charAt(i));
if( count < st.top ) {
count = st.top;
}
}
if(str.charAt(i) == ')' ) {
if(st.top > 0 ) {
st.pop();
}
else {
System.out.println("Wrong pranthesis ");
}
}
i++;
}
System.out.println("Total Value : " +count);
}
}
static int findSmallestElement( TreeNode node, int finalValue ) {
if(node != null) {
finalValue = findSmallestElement(node.left, finalValue);
finalValue = findSmallestElement(node.right, finalValue);
if( node.data < finalValue ) {
finalValue = node.data;
}
}
return finalValue;
}
public class MaximumParanthesis {
public static void main(String[] args) {
String str = "abc(123(xyz))m((((n))))o";
findDepthElement(str);
}
static void findDepthElement(String str ) {
Stack st = new Stack();
int i = 0;
int count = 0;
while( i < str.length() ) {
System.out.println(str.charAt(i));
if(str.charAt(i) == '(') {
st.push(str.charAt(i));
if( count < st.top ) {
count = st.top;
}
}
if(str.charAt(i) == ')' ) {
if(st.top > 0 ) {
st.pop();
}
else {
System.out.println("Wrong pranthesis ");
}
}
i++;
}
System.out.println("Total Value : " +count);
}
}
package com.algorithm;
public class MaximumParanthesis {
public static void main(String[] args) {
String str = "abc(123(xyz))m((((n))))o";
findDepthElement(str);
}
static void findDepthElement(String str ) {
Stack st = new Stack();
int i = 0;
int count = 0;
while( i < str.length() ) {
System.out.println(str.charAt(i));
if(str.charAt(i) == '(') {
st.push(str.charAt(i));
if( count < st.top ) {
count = st.top;
}
}
if(str.charAt(i) == ')' ) {
if(st.top > 0 ) {
st.pop();
}
else {
System.out.println("Wrong pranthesis ");
}
}
i++;
}
System.out.println("Total Value : " +count);
}
}
- Krishna May 07, 2015