LeetCode - Isomorphic Strings

    题意很好理解,判断两个字符串的结构是否相同。测试数据里有?{}【】等符号啊,果断扩大到300.

   

public class Solution {
    public static boolean isIsomorphic(String s, String t) {
		if(isIsomorphicOne(s,t) && isIsomorphicOne(t,s)) {
			return true;
		}
		else {
			return false;
		}
	}
	public static boolean isIsomorphicOne(String s, String t) {
		if(s==null || t==null) {
			return false;
		}
        Letter[] letter = new Letter[300];
        for(int i=0; i<300; i++) {
        	letter[i] = new Letter((char)(i), -1, 0);
        }
        for(int i=0; i<s.length(); i++) {
        	Letter sl = letter[s.charAt(i)];
        	if(sl.pos==-1) {
        		sl.count ++;
        		sl.pos = i;
        	}
        	else {
        		int cur = sl.pos;
        		if(t.charAt(i)!=t.charAt(cur)) {
        			return false;
        		}
        		else {
        			sl.count ++;
            		sl.pos = i;
        		}
        		
        	}
        }
        return true;
        
    }
}
class Letter {
	char ch;
	int pos;
	int count;
	public Letter(char ch, int pos, int count) {
		this.ch = ch;
		this.pos = pos;
		this.count = count;
	}
}

 

posted @ 2015-05-21 15:24  Pickle  阅读(161)  评论(0编辑  收藏  举报