271. Encode and Decode Strings
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings. Machine 1 (sender) has the function: string encode(vector<string> strs) { // ... your code return encoded_string; } Machine 2 (receiver) has the function: vector<string> decode(string s) { //... your code return strs; } So Machine 1 does: string encoded_string = encode(strs); and Machine 2 does: vector<string> strs2 = decode(encoded_string); strs2 in Machine 2 should be the same as strs in Machine 1. Implement the encode and decode methods. Note: * The string may contain any possible characters out of 256 valid ascii characters. Your algorithm should be generalized enough to work on any possible characters. * Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless. * Do not rely on any library method such as eval or serialize methods. You should implement your own encode/decode algorithm. class Codec{ public String encode(List<String> strs){ StringBuilder sb = new StringBuilder(); for(String s : strs){ sb.append(s.length()).append('/').append(s); } return sb.toString(); } public List<String> decode(String s){ List<String> res = new ArrayList<>(); int i = 0; while(i < s.length()){ int slash = s.indexOf('/', i); int size = Integer.valueOf(s.substring(i, slash)); res.add(s.substring(slash + 1, slash + 1 + size)); i = slash + 1 + size; } return res; } } 521 45/67 217 sb = 3/5215/45/673/217 sb = 3/521 5/45/67 3/217 //521/45/67/217/ The encoded form of strings is length + '/' +str + length + '/' + str. If you say, there is a '/' in a str. Then it is counted for the length of that str but the added slash is not. For "ab/cd", the encoded one should be "5/ab/cd". The decode function will read the length first, then skip the slash between '5' and 'a', starting from the next character and get the substring of that length which is "ab/cd". The method indexOf() is used for finding out the index of the specified character or substring in a particular String. There are 4 variations of this method: int indexOf(int ch): It returns the index of the first occurrence of character ch in a String. int indexOf(int ch, int fromIndex): It returns the index of first occurrence if character ch, starting from the specified index “fromIndex”. int indexOf(String str): Returns the index of string str in a particular String. int indexOf(String str, int fromIndex): Returns the index of string str, starting from the specified index “fromIndex”. All the above variations returns -1 if the specified char/substring is not found in the particular String. public class IndexOfExample{ public static void main(String args[]) { String str1 = new String("This is a BeginnersBook tutorial"); String str2 = new String("Beginners"); String str3 = new String("Book"); String str4 = new String("Books"); System.out.println("Index of B in str1: "+str1.indexOf('B')); System.out.println("Index of B in str1 after 15th char:"+str1.indexOf('B', 15)); System.out.println("Index of B in str1 after 30th char:"+str1.indexOf('B', 30)); System.out.println("Index of string str2 in str1:"+str1.indexOf(str2)); System.out.println("Index of str2 after 15th char"+str1.indexOf(str2, 15)); System.out.println("Index of string str3:"+str1.indexOf(str3)); System.out.println("Index of string str4"+str1.indexOf(str4)); System.out.println("Index of harcoded string:"+str1.indexOf("is")); System.out.println("Index of hardcoded string after 4th char:"+str1.indexOf("is", 4)); } } Output: Index of B in str1: 10 Index of B in str1 after 15th char:19 Index of B in str1 after 30th char:-1 Index of string str2 in str1:10 Index of str2 after 15th char-1 Index of string str3:19 Index of string str4-1 Index of harcoded string:2 Index of hardcoded string after 4th char:5
posted on 2018-11-08 15:37 猪猪🐷 阅读(156) 评论(0) 收藏 举报
浙公网安备 33010602011771号