First Unique Character in a String Test submit

This commit is contained in:
YuCheng Hu 2021-04-27 10:23:57 -04:00
parent dfd7d7e2b1
commit 4398bed9d1
3 changed files with 83 additions and 25 deletions

View File

@ -1,6 +1,10 @@
package com.ossez.toolkits.codebank.common.model.request; package com.ossez.toolkits.codebank.common.model.request;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import java.io.Serializable; import java.io.Serializable;
import java.util.Objects;
/** /**
@ -79,4 +83,20 @@ public class TopicRequest implements Serializable {
public void setCreated_at(String created_at) { public void setCreated_at(String created_at) {
this.created_at = created_at; this.created_at = created_at;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TopicRequest that = (TopicRequest) o;
return new EqualsBuilder().append(title, that.title).append(topic_id, that.topic_id).append(raw, that.raw).append(category, that.category).append(target_recipients, that.target_recipients).append(archetype, that.archetype).append(created_at, that.created_at).isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37).append(title).append(topic_id).append(raw).append(category).append(target_recipients).append(archetype).append(created_at).toHashCode();
}
} }

View File

@ -1,5 +1,7 @@
package com.ossez.toolkits.codebank.tests; package com.ossez.toolkits.codebank.tests;
import com.google.api.gbase.client.NumberUnit;
import com.ossez.toolkits.codebank.common.model.request.TopicRequest;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -25,6 +27,21 @@ public class BlackboardTest {
@Test @Test
public void testMain() { public void testMain() {
logger.debug("TREE TEST"); logger.debug("TREE TEST");
TopicRequest topicRequest = new TopicRequest();
// topicRequest.setTopic_id(11);
logger.debug("HashCode 1 - {}", topicRequest.hashCode());
logger.debug("HashCode AaAaAa - {}", "AaAaAa".hashCode());
logger.debug("HashCode BBAaBB - {}", "BBAaBB".hashCode());
// if((1/2) && false) {
//
// }
// topicRequest = new TopicRequest();
logger.debug("HashCode 2 - {}", topicRequest.hashCode());
String data = "{1,2,3,4,5,#,6,#,#,7,8,#,#}"; String data = "{1,2,3,4,5,#,6,#,#,7,8,#,#}";
this.subLogic(data); this.subLogic(data);

View File

@ -6,6 +6,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
/** /**
* <p> * <p>
@ -17,43 +19,62 @@ import java.util.ArrayList;
* "https://leetcode.com/problems/first-unique-character-in-a-string/">https://leetcode.com/problems/first-unique-character-in-a-string/</a> * "https://leetcode.com/problems/first-unique-character-in-a-string/">https://leetcode.com/problems/first-unique-character-in-a-string/</a>
* </ul> * </ul>
* </p> * </p>
*
* @author YuCheng
* *
* @author YuCheng
*/ */
public class LeetCode0387FirstUniqueCharacterTest { public class LeetCode0387FirstUniqueCharacterTest {
private final static Logger logger = LoggerFactory.getLogger(LeetCode0387FirstUniqueCharacterTest.class); private final static Logger logger = LoggerFactory.getLogger(LeetCode0387FirstUniqueCharacterTest.class);
/** /**
* *
*/ */
@Test @Test
public void testMain() { public void testMain() {
logger.debug("BEGIN"); logger.debug("BEGIN");
String data = "leetcode"; String data = "lovelycomossez";
System.out.println(firstUniqChar(data)); System.out.println(firstUniqChar(data));
} }
/** /**
* Deserialize from array to tree * Deserialize from array to tree
* *
* @param data * @param data
* @return * @return
*/ */
private int firstUniqChar(String data) { private int firstUniqChar(String data) {
// NULL CHECK // NULL CHECK
if (data.equals("")) { if (data.equals("")) {
return -1; return -1;
} }
int retVal = -1;
LinkedHashMap<Character, String> retMap = new LinkedHashMap<Character, String>();
char[] chStr = data.toCharArray();
return 0; for (int i = 0; i < chStr.length; i++) {
if (retMap.get(chStr[i]) != null) {
retMap.put(chStr[i], retMap.get(chStr[i]) + "#" + String.valueOf(i));
} else {
retMap.put(chStr[i], String.valueOf(i));
}
}
} for (Map.Entry me : retMap.entrySet()) {
String val = "" + me.getValue();
if (!val.contains("#")) {
retVal = Integer.valueOf(val);
break;
}
}
return retVal;
}
} }