File tree Expand file tree Collapse file tree
java/algorithm/Interview/practice05 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package algorithm .Interview .practice05 ;
2+
3+ /**
4+ * @author mood321
5+ * @date 2020/8/7 0:34
6+ 7+ */
8+ public class P_5_1_IsDeformation {
9+
10+ /**
11+ * 如果字符的类型很多,可以采用哈希表代替长度为256的整型数组,
12+ * 字符串的种类为 M,字符串的长度为 N,那么 时间复杂度是 O(N), 空间复杂度是O(M)
13+ * @param str1
14+ * @param str2
15+ * @return
16+ */
17+ public boolean isDeformation (String str1 , String str2 ){
18+
19+ if (str1 == null || str2 == null || str1 .length () != str2 .length ()){
20+ return false ;
21+ }
22+
23+ char [] chars1 = str1 .toCharArray ();
24+ char [] chars2 = str2 .toCharArray ();
25+ int [] map = new int [256 ];
26+
27+ for (int i = 0 ; i < chars1 .length ; i ++) {
28+ map [chars1 [i ]]++;
29+ }
30+
31+ for (int i = 0 ; i < chars2 .length ; i ++) {
32+ if (map [chars2 [i ]]-- == 0 ){
33+ return false ;
34+ }
35+ }
36+ return true ;
37+ }
38+ }
Original file line number Diff line number Diff line change @@ -713,6 +713,24 @@ N皇后问题是指N*N的棋盘要摆N个皇后,要求任何两个皇后不同
713713
714714
715715
716+ ### 字符串
717+
718+ #### 1 判断两个字符串是否互为变形词
719+ ````
720+ 给定两个字符串 str1 和str2 ,如果两个字符串中出现的字符种类一样,次数也一样,则互为变形词,实现一个函数判断两个字符串是否互为变形词。例如 str1=“123”,str2=“132”,true; str1=“123”,str2=“1332”,false;
721+
722+
723+
724+ 【解题思路】
725+
726+ 1. 首先比较两个字符串的长度,长度不同肯定是false。
727+
728+ 2. 如果长度相同,新建一个数组,用以存储每个字符出现次数。
729+
730+ 3. 初始值都是为0,在str1 中出现一次就加1,在str2 中出现一次就减1,最后遍历完str2没有出现负值,就返回true。
731+ ````
732+ <p ><a href =" /src/main/java/algorithm/Interview/practice04/P_4_17_NQueens.java " > code</a >
733+
716734
717735
718736### 其他
You can’t perform that action at this time.
0 commit comments