enh(Go): import the string package on anagram (#838)

pull/835/head
Ayomide AJAYI 2022-09-12 02:44:32 +02:00 committed by GitHub
parent 7f85abfa31
commit daa8be12a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 16 deletions

View File

@ -1,29 +1,23 @@
package main
/**
Problem Statement: Given two strings s and t, find they are anagrams or not
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
**/
/***
Example 1: Input: s="anagram" t="nagaram"
Output: true
Example 2: Input: s="rat" t="car"
Output: false
***/
/***
Time complexity: O(n), n=length of string s
Space complexity: O(1)
**/
package strings
import "fmt"
func main() {
func RunIsAnagram() {
fmt.Println("Enter first string")
var first string
fmt.Scanln(&first)
@ -37,9 +31,10 @@ func main() {
fmt.Println("They are not anagrams")
}
}
func isAnagram(s string, t string) bool {
var count [256]int
//var i int
for i := 0; i < len(s); i++ {
count[s[i]]++
}

View File

@ -1,3 +1,10 @@
/*
Given a string s, return true if we can have a palindrome from the permutation of the input
A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar.
Time: O(n)
Space: O(n)
*/
package strings
import(
@ -5,12 +12,7 @@ import(
"fmt"
)
/*
Given a string s, return true if we can have a palindrome from the permutation of the input
Time: O(n)
Space: O(n)
*/
func canPermutePalindrome(s string) bool {
a := strings.Split(s,"")
dictionary := make(map[string] int)
@ -29,7 +31,7 @@ func canPermutePalindrome(s string) bool {
//You are welcome to play around with the test cases
func runPermutationCheck(){
func RunPermutationCheck(){
input1 := "carerac"
fmt.Printf("%t for input %s \n", canPermutePalindrome(input1), input1) //should print true