File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package leetcode ;
2+
3+ import java .util .Arrays ;
4+
5+ public class IsAnagram {
6+
7+ public static void main (String [] args ){
8+ String s1 = "anagram" ;
9+ String s2 = "nagaram" ;
10+ System .out .println (isAnagram1 (s1 , s2 ));
11+ System .out .println (isAnagram2 (s1 , s2 ));
12+ }
13+
14+ /**
15+ * ÅÅÐòºó±È½Ï
16+ * @param s
17+ * @param t
18+ * @return
19+ */
20+ public static boolean isAnagram1 (String s , String t ) {
21+ if (s .length () != t .length ()){
22+ return false ;
23+ }
24+ char [] cs = s .toCharArray ();
25+ char [] ct = t .toCharArray ();
26+
27+ Arrays .sort (cs );
28+ Arrays .sort (ct );
29+
30+ return Arrays .equals (cs , ct );
31+ }
32+
33+ public static boolean isAnagram2 (String s , String t ) {
34+ if (s .length () != t .length ()){
35+ return false ;
36+ }
37+
38+ int [] hashTable = new int [26 ];
39+ for (char c : s .toCharArray ()){
40+ hashTable [c - 'a' ]++;
41+ }
42+ for (char c : t .toCharArray ()){
43+ hashTable [c - 'a' ]--;
44+ }
45+
46+ for (int i = 0 ; i < hashTable .length ; i ++){
47+ if (0 != hashTable [i ]){
48+ return false ;
49+ }
50+ }
51+ return true ;
52+ }
53+ }
Original file line number Diff line number Diff line change 1+ package leetcode ;
2+
3+ public class IsSubsequence {
4+
5+ public static void main (String [] args ) {
6+ // TODO Auto-generated method stub
7+
8+ String s = "axc" ;
9+ String t = "ahbgdc" ;
10+ System .out .println (isSubsequence (s , t ));
11+ }
12+
13+ // ˫ָÕë
14+ public static boolean isSubsequence (String s , String t ) {
15+ int i = 0 , j = 0 ;
16+ for (i = 0 , j = 0 ; j < t .length () && i < s .length (); j ++){
17+ if (s .charAt (i ) == t .charAt (j )){
18+ i ++;
19+ }
20+ }
21+ if (i == s .length ()){
22+ return true ;
23+ }
24+ return false ;
25+ }
26+ }
You can’t perform that action at this time.
0 commit comments