码迷,mamicode.com
首页 > 其他好文 > 详细

Scramble String

时间:2014-10-16 14:31:02      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   java   for   

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.

Below is one possible representation of s1 = "great":

    great
   /      gr    eat
 / \    /  g   r  e   at
           /           a   t

To scramble the string, we may choose any non-leaf node and swap its two children.

For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".

    rgeat
   /      rg    eat
 / \    /  r   g  e   at
           /           a   t

We say that "rgeat" is a scrambled string of "great".

Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".

    rgtae
   /      rg    tae
 / \    /  r   g  ta  e
       /       t   a

We say that "rgtae" is a scrambled string of "great".

Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.

答案

public class Solution {
    char[] array1;

    char[] array2;

    Map<Character, Integer> map = new HashMap<Character, Integer>(255);

    public boolean isScramble(int startIndex1, int endIndex1,int startIndex2,int endIndex2)
    {
        if (startIndex1 == endIndex1)
        {
            return array1[startIndex1] == array2[startIndex2];
        }
        int index;
        for (index = 0; index <=endIndex1- startIndex1; index++ )
        {
            if (array1[startIndex1+index] != array2[startIndex2+index])
            {
                break;
            }
        }
        if (index > endIndex1- startIndex1)
        {
            return true;
        }
        map.clear();
        for (index = startIndex1; index <= endIndex1; index++ )
        {
            Integer value = map.get(array1[index]);
            map.put(array1[index], value == null ? 1 : value + 1);
        }
        for (index = startIndex2; index <= endIndex2; index++ )
        {
            Integer value = map.get(array2[index]);
            if (value == null || value == 0)
            {
                return false;
            }
            map.put(array2[index], value - 1);
        }
        for (index = 0; index <endIndex1- startIndex1; index++ )
        {
            if(isScramble(startIndex1, startIndex1+index,startIndex2,startIndex2+index)
                &&isScramble(startIndex1+index+1, endIndex1,startIndex2+index+1,endIndex2))
            {
                return true;
            }
            if(isScramble(startIndex1, startIndex1+index,endIndex2-index,endIndex2)
                &&isScramble(startIndex1+index+1, endIndex1,startIndex2,endIndex2-index-1))
              {
                return true;
            }
        }
        return false;
    }

    public boolean isScramble(String s1, String s2)
    {
        if (s1 == null || s2 == null || s1.length() != s2.length())
        {
            return false;
        }
        if (s1.length() == 0)
        {
            return true;
        }
        array1 = s1.toCharArray();
        array2 = s2.toCharArray();
        return isScramble(0, array1.length - 1,0,array2.length-1);
    }
}


Scramble String

标签:des   style   blog   color   io   os   ar   java   for   

原文地址:http://blog.csdn.net/jiewuyou/article/details/40145299

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!