-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtask.go
72 lines (58 loc) · 978 Bytes
/
task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
str1, str2, err := getInputData()
if err != nil {
showError(err)
}
fmt.Println(solution(str1, str2))
}
func solution(s, t string) string {
lenS, lenT := len(s), len(t)
if lenS == 0 || s == " " || s == t {
return "True"
}
if lenS > lenT {
return "False"
}
var i, j int
for j < lenT {
if s[i] == t[j] {
i++
if i >= lenS {
return "True"
}
}
j++
}
return "False"
}
func getInputData() (s, t string, err error) {
var input *os.File
input, err = getInputFromFile()
if err != nil {
showError(err)
}
// close file
defer func(input *os.File) {
_ = input.Close()
}(input)
reader := bufio.NewReader(input)
s, _ = reader.ReadString('\n')
t, _ = reader.ReadString('\n')
return
}
func getInputFromFile() (*os.File, error) {
file, err := os.Open("input.txt")
if err != nil {
return nil, err
}
return file, nil
}
func showError(err interface{}) {
panic(err)
}