Welcome to visit the blog CC150
Determines whether all characters of a string are all different. Method One: The method of character comparison. Time complexity: O (n2) O (n^2).
Import java.util.*;
public class Different {public
boolean checkdifferent (String inistring) {
//write code here
if (inistring.l Ength () >256) return
false;
for (int i=0; I<inistring.length ()-1; i++) {for
(int j=i+1; j<inistring.length (); j + +) {
if (inistring.c Harat (i) = = Inistring.charat (j)) return
false
;
}
return true;
}
Method Two:
Import java.util.*;
public class Different {public
boolean checkdifferent (String inistring) {
//write code here
if (inistring.l Ength () >256) return
false;
boolean[] Char_set = new boolean[256];
for (int i=0; i<inistring.length (); i++) {
char val = Inistring.charat (i);
if ((Val < 0) | | (val > 255)) Check the input return
false;
if (Char_set[val]) return
false;
Char_set[val] = true;
}
return true;
}
1.2 Original string Flip
Flips a given string (you can use a single procedure variable).
Using array to implement
Import java.util.*;
public class Reverse {public
string reversestring (String inistring) {
//write code here
char[] Strarray = in Istring.tochararray ();
int low = 0, high = inistring.length ()-1;
while (Low < high) {
char tmp = Strarray[low];
Strarray[low] = Strarray[high];
Strarray[high] = tmp;
low++;
high--;
}
String tmpstring = new string (strarray);
Return tmpstring
}
}
Using StringBuilder
Import java.util.*;
public class Reverse {public
string reversestring (String inistring) {
//write code here
StringBuilder SB = n EW StringBuilder (inistring);
int low = 0, high = sb.length ()-1;
While [low < high] {
char tmp = Sb.charat (low);
Sb.setcharat (Low, Sb.charat (high));
Sb.setcharat (High, TMP);
low++;
high--;
}
return sb.tostring ();
}
1.3
Determines whether the character of one of the strings is rearranged and can become another string. Here the big lowercase for different characters, and consider the string key space.
Import java.util.*;
public class Same {public
boolean Checksam (String Stringa, String stringb) {
//optimization-if the length of two strings is unequal, it must not be possible if
(St Ringa.length ()!= stringb.length ()) return
false;
Return Sortmy (Stringa). Equals (Sortmy (STRINGB));
Public String sortmy (string s) {
char[] as = S.tochararray ();
Java.util.Arrays.sort (as);
return new String (AS);
}
1.4 Space Replacement
Space substitution
Import java.util.*;
public class Replacement {public
string Replacespace (string inistring, int length) {
return Inistring.replaceall ("", "%20");
}
1.5 Basic string Compression
Basic string compression
Import java.util.*;
public class Zipper {public
string zipstring (String inistring) {
//write code-here
String mystr = "";
Char last = inistring.charat (0);
int count = 1;
for (int i=1; i<inistring.length (); i++) {
if (Inistring.charat (i) ==last) {
count++;
} else {
mystr + = last + "" + count;
last = Inistring.charat (i);
Count = 1;
}
}
MyStr + + "" + count;
if (Mystr.length () > Inistring.length ()) return
inistring;
else {return
mystr;
}
}
}
1.6D
1.4D
1.3D
1.4D
1.3D
1.4D